上篇文章是对mysql的安装,接着上篇文章,这篇文章安装nginx服务
添加yum源
默认情况Centos7中无Nginx的源,最近发现Nginx官网提供了Centos的源地址。因此可以如下执行命令添加源:
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
安装Nginx
- 通过yum search nginx看看是否已经添加源成功。如果成功则执行下列命令安装Nginx。
yum install -y nginx
- 启动Nginx并设置开机自动运行
systemctl start nginx.service
systemctl enable nginx.service
- 开启nginx服务后可以直接通过ip访问
http://ip 默认80端口
若无法访问 ,需要在iptable上添加80端口,如果用的阿里云或腾讯云还需要添加安全组
CentOS中iptables防火墙 开放80端口方法
- 开放端口
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
- 保存配置
service iptables save
- 重启防火墙
service iptables restart
- 查看配置
service iptables status
nginx相关路径
- /etc/nginx/ 安装路径
- /usr/share/nginx 部署路径
- cat /var/log/nginx/error.log 错误日志
问题总结
- systemctl start nginx启动报错
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
执行以下命令查看服务状态
systemctl status nginx
nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 四 2019-05-30 14:34:48 CST; 1min 35s ago
Docs: http://nginx.org/en/docs/
Process: 6051 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Main PID: 1542 (code=killed, signal=KILL)
5月 30 14:34:45 iz2ze3bc56k6c8wubzxn9kz nginx[6051]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
5月 30 14:34:46 iz2ze3bc56k6c8wubzxn9kz nginx[6051]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
5月 30 14:34:46 iz2ze3bc56k6c8wubzxn9kz nginx[6051]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
5月 30 14:34:47 iz2ze3bc56k6c8wubzxn9kz nginx[6051]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
5月 30 14:34:47 iz2ze3bc56k6c8wubzxn9kz nginx[6051]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
5月 30 14:34:48 iz2ze3bc56k6c8wubzxn9kz systemd[1]: nginx.service: control process exited, code=exited status=1
5月 30 14:34:48 iz2ze3bc56k6c8wubzxn9kz nginx[6051]: nginx: [emerg] still could not bind()
5月 30 14:34:48 iz2ze3bc56k6c8wubzxn9kz systemd[1]: Failed to start nginx - high performance web server.
5月 30 14:34:48 iz2ze3bc56k6c8wubzxn9kz systemd[1]: Unit nginx.service entered failed state.
5月 30 14:34:48 iz2ze3bc56k6c8wubzxn9kz systemd[1]: nginx.service failed.
可以看到以上错误是因为端口被占用的原因
查看nginx服务
ps -ef | grep nginx
杀死进程
pkill -9 nginx
再启动就可以了
systemctl start nginx