安装 nginx 所需环境
- gcc 安装:
shell> yum install gcc-c++
- PCRE pcre-devel 安装:
shell> yum install -y pcre pcre-devel
- zlib 安装:
shell> yum install -y zlib zlib-devel
- OpenSSL 安装
shell> yum install -y openssl openssl-devel
下载 nginx
下载地址:http://nginx.org/en/download.html
开始安装
将下载好的文件 nginx-1.15.1.tar.gz 上传到 CentOS 服务器 /home/service 目录
解压文件 nginx-1.15.1.tar.gz
shell> cd /home/service
shell> tar -xzvf nginx-1.15.1.tar.gz
- 安装(依次执行如下命令):
shell> cd /home/service/nginx-1.15.1
shell> ./configure --prefix=/home/service/nginx
shell> make
shell> make install
- 启动 nginx
shell> /home/service/nginx/sbin/nginx
- 检测 nginx 配置文件是否正确
shell> /home/service/nginx/sbin/nginx -t
- 重启 nginx 服务
shell> /home/service/nginx/sbin/nginx -s reload
- 配置文件所在目录
/home/service/nginx/conf/nginx.conf
- 创建软链接
shell> ln -s /home/service/nginx/conf/nginx.conf /home/conf/nginx/
- 开放 80 端口
shell> firewall-cmd --zone=public --add-port=6379/tcp --permanent
shell> firewall-cmd --reload
nginx 配置开机启动
- 在系统服务目录里创建nginx.service文件
shell> vi /lib/systemd/system/nginx.service
- 文件内容如下(注意替换目录)
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/home/service/nginx/sbin/nginx
ExecReload=/home/service/nginx/sbin/nginx -s reload
ExecStop=/home/service/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- 设置开机启动
shell> systemctl enable nginx.service
- 其他命令
1> 启动nginx服务
shell> systemctl start nginx.service
2> 设置开机自启动
shell> systemctl enable nginx.service
3> 停止开机自启动
shell> systemctl disable nginx.service
4> 查看服务当前状态
shell> systemctl status nginx.service
5> 重新启动服务
shell> systemctl restart nginx.service
6> 查看所有已启动的服务
shell> systemctl list-units --type=service
参考网址:https://www.cnblogs.com/piscesLoveCc/p/5867900.html