在 CentOS7 上安装和启动 nginx
1. 安装 nginx
参考:这里的东西
添加存储库
# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
安装
# yum install nginx
版本确认
# nginx -v
nginx version: nginx/1.13.6
2. 自动启动设定
# systemctl enable nginx
ln -s '/usr/lib/systemd/system/nginx.service' '/etc/systemd/system/multi-user.target.wants/nginx.service'
3. 启动
# systemctl start nginx
Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.
报错了。
尝试使用systemctl status nginx.service 进行确认。
# systemctl status nginx.service
nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled)
Active: failed (Result: exit-code) since Fri 2017-10-13 13:16:35 JST; 2min 11s ago
Docs: http://nginx.org/en/docs/
Process: 8579 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Process: 8576 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Oct 13 13:16:32 dti-vps-srv717 nginx[8576]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Oct 13 13:16:32 dti-vps-srv717 nginx[8579]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 13 13:16:33 dti-vps-srv717 nginx[8579]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 13 13:16:33 dti-vps-srv717 nginx[8579]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 13 13:16:34 dti-vps-srv717 nginx[8579]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 13 13:16:34 dti-vps-srv717 nginx[8579]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 13 13:16:35 dti-vps-srv717 nginx[8579]: nginx: [emerg] still could not bind()
Oct 13 13:16:35 dti-vps-srv717 systemd[1]: nginx.service: control process exited, code=exited status=1
Oct 13 13:16:35 dti-vps-srv717 systemd[1]: Failed to start nginx - high performance web server.
Oct 13 13:16:35 dti-vps-srv717 systemd[1]: Unit nginx.service entered failed state.
似乎已经被使用了80端口。
使用lsof命令查找正在监听的进程。
# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 365 root 4u IPv6 557661 0t0 TCP *:http (LISTEN)
httpd 3799 apache 4u IPv6 557661 0t0 TCP *:http (LISTEN)
httpd 3824 apache 4u IPv6 557661 0t0 TCP *:http (LISTEN)
httpd 7338 apache 4u IPv6 557661 0t0 TCP *:http (LISTEN)
httpd 7352 apache 4u IPv6 557661 0t0 TCP *:http (LISTEN)
httpd 7368 apache 4u IPv6 557661 0t0 TCP *:http (LISTEN)
我决定停止Apache,启动nginx。
Apache 停止
# systemctl stop httpd.service
nginx 起動
# systemctl start nginx
確認
# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 8606 root 6u IPv4 1575352318 0t0 TCP *:http (LISTEN)
nginx 8607 nginx 6u IPv4 1575352318 0t0 TCP *:http (LISTEN)
4. 确认表达
正常地出现欢迎画面