yum安装
直接yum install nginx -y
即可,如果不行可先添加epel源:
yum install epel-release -y
关于nginx的后续运用,如负载均衡,反向代理(可参考:https://www.cnblogs.com/sillage/p/14898533.html ),有时间再整理更新
源码安装
安装依赖
直接复制以下命令,一键安装所需要的依赖:
yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
注:对上述安装的依赖的说明
- gcc安装,nginx源码编译需要
- PCRE pcre-devel 安装,nginx 的 http 模块使用 pcre 来解析正则表达式
- zlib安装,nginx 使用zlib对http包的内容进行gzip
- OpenSSL 安装,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)
nginx安装
下载nginx安装包:
wget http://nginx.org/download/nginx-1.9.9.tar.gz
解压:
tar zxf nginx-1.9.9.tar.gz
随后进入解压后的目录进行源码安装:
cd nginx-1.9.9
./configure
make
make install
到此就安装完成了。
启动、停止nginx
一般默认都在下面这个目录会有一个可执行的nginx文件(也可以使用whereis nginx
进行查找):
cd /usr/local/nginx/sbin/
./nginx | 启动 |
---|---|
./nginx -s stop | 停止 |
./nginx -s quit | nginx进程处理完任务再进行停止 |
./nginx -s reload | 重新加载配置文件 |
设置nginx开机自启
cd /lib/systemd/system/
vim nginx.service
添加内容如下:
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
接下来就可以像其他服务一样用systemctl进行管理了
systemctl enable nginx | 设置nginx开机自启 |
---|---|
systemctl start nginx | 启动nginx |
systemctl stop nginx | 停止nginx |
注:使用命令时如果有报错,可能是有服务占用了80端口,lsof -i:80
查看占用80端口的服务
参考
部分内容参考总结自: