我安裝好nginx后,發現不能使用systemctl start nginx或者systemctl stop?nginx來控制啟停
解決方法如下
首先要建一個nginx.pid的文件
一般是建在?/var/run/這個路徑下面
sudo touch /var/run/nginx.pid
添加權限
sudo chmod 644 /var/run/nginx.pid
可以進入到該路徑查看是否正確生成
然后找到nginx.conf的文件
我的是在/usr/local/nginx/conf這個路徑
編輯文件
vim?nginx.conf
將pid?的路徑編輯成剛剛我們的路徑,如下圖
然后我們要新建一個nginx.service的文件
sudo vi /etc/systemd/system/nginx.service
然后往里面編輯內容,主要如下:
[Unit]
Description=nginx - high performance web server
After=network.target[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
PIDFile=/var/run/nginx.pid
PrivateTmp=true[Install]
WantedBy=multi-user.target
然后保存
在創建完服務單元文件后,需要通知 systemd 并加載該服務單元文件。執行以下命令:
sudo systemctl daemon-reload
現在你可以啟動 nginx
服務了:
sudo systemctl start nginx
要停止 nginx
服務,可以使用以下命令:
sudo systemctl stop nginx
?還可以使用 systemctl restart nginx
來重啟 nginx
服務,systemctl reload nginx
來重新加載 nginx
配置文件