nginx安装启动脚本配置环境变量遇到问题解决办法
1.注册系统服务配置启动脚本
vim usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
注意!:不要添加注释,添加注释或者文件末尾有空行会导致后面配置开机自动启动命令失败
输入wq无法保存文件时,提示无法保存文件,解决办法:
末端命令模式下,输入命令 :
w ! sudo tee %
继续在末端命令模式下,输入命令 :q! 强制退出,此时文本已经保存。
2.加载systemctl命令
systemctl daemon-reload
systemctl start nginx.service
如果启动失败,检查80端口是否被占用
[root@Centos7-1 /]# 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 1794/nginx: master
杀死进程重新启动
kill 1794
3.配置Nginx在启动时启动
将enable选项与systemctl 命令配合使用以启用Nginx:
sudo systemctl enable nginx
报错提示,解决办法删除掉nginx.service文件中的注释内容和末尾行空行,被这个问题折磨很久,总算解决了~
Failed to execute operation: Invalid argument
将disable 选项与systemctl 命令一起使用可禁用Nginx:
sudo systemctl disable nginx