Nginx安装好后,每次启动或者重启需要进入到/usr/local/nginx下操作,比较麻烦,于是把Nginx添加到系统服务中方便管理:

# 添加nginx.service
# vim /lib/systemd/system/nginx.service

[Unit]
Description=nginx service
After=network.target 

[Service] 
Type=forking
# 路径对应安装路径
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true 

[Install] 
WantedBy=multi-user.target

添加完成后,即可使用systemctl start nginx等进行管理,例如添加开机自启动:

systemctl enable nginx.service