安装 Nginx

yum 安装 nginx 非常简单,就输入一条命令即可。

sudo yum -y install nginx   # 安装 nginx
sudo yum remove nginx # 卸载 nginx

使用 yum 进行 Nginx 安装时,Nginx 配置文件在 /etc/nginx 目录下。

使用 wget 进行 Nginx 安装时,Nginx 配置文件在 /usr/local/nginx/conf 目录下。



具体的命令

vim /etc/nginx/nginx.conf      # 查看nginx配置内容

sudo systemctl enable nginx # 设置开机启动
sudo systemctl disable nginx # 关闭开机启动

sudo systemctl start nginx.service # 启动 nginx 服务
sudo systemctl stop nginx.service # 停止 nginx 服务


sudo systemctl status nginx # 查看状态


sudo systemctl reload nginx # 重新加载配置,一般是在修改过 nginx 配置文件时使用。
sudo systemctl restart nginx # 重启 nginx 服务



防火墙设置

如果你的服务器开启了防火墙,则需要同时打开 80(HTTP)和 443(HTTPS)端口

firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --zone=public --add-port=80/tcp --permanent
sudo firewall-cmd --reload




如果后续安装了Apache,会与Nginx冲突,导致Nginx关闭。查看Nginx运行状态时会出现红字报错:

Failed to start The nginx HTTP and reverse proxy server.

如果想恢复,需要先执行下面两条命令关闭Apache:

systemctl stop httpd
systemctl disable httpd

然后重启Nginx:

sudo systemctl restart nginx