CentOS7使用yum安装Nginx
添加Nginx到YUM源
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
安装Nginx
sudo yum install -y nginx
启动Nginx
sudo systemctl start nginx.service
如果一切正常,打开浏览器应该能访问到默认的Welcome页面,出现打开不了的情况,那多半是因为网络防火墙没有放开80端口。下面是解决办法(如果没遇到不能访问默认页面可自行往下查看设置开机自启部分)
查看Nginx是否已经运行成功
[root@localhost home]# ps aux|grep nginx
root 36612 0.0 0.0 46464 972 ? Ss 19:49 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 36613 0.0 0.1 46856 1604 ? S 19:49 0:00 nginx: worker process
root 39654 0.0 0.0 112824 980 pts/0 S+ 19:53 0:00 grep --color=auto nginx
查看端口80是否分配给Nginx
[root@localhost \]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 36612/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1091/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1272/master
tcp6 0 0 :::22 :::* LISTEN 1091/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1272/master
解决办法
第一步,对80端口进行防火墙配置:
[root@localhost home]# firewall-cmd --zone=public --add-port=80/tcp --permanent
第二步,重启防火墙服务:
systemctl restart firewalld.service
设置Nginx开机自启
sudo systemctl enable nginx.service
Nginx配置信息
- 网站文件存放默认目录
/usr/share/nginx/html
- 网站默认站点配置
/etc/nginx/conf.d/default.conf
- 自定义Nginx站点配置文件存放目录
/etc/nginx/conf.d/
- Nginx全局配置
/etc/nginx/nginx.conf
- Nginx启动
nginx -c nginx.conf
- Nginx快速停止
nginx -s stop