1.创建sevice配置文件

vim /usr/lib/systemd/system/nginx.service

### BEGIN INIT INFO
# Provides: NGINX
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start NGINX daemon at boot time
# Description: Start NGINX daemon at boot time
### END INIT INFO

[Unit]
Description=The nginx web and reverse proxy server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
ExecStartPre=/data/nginx/sbin/nginx -t
ExecStart=/data/nginx/sbin/nginx -c /data/nginx/conf/nginx.conf
ExecReload=/data/nginx/sbin/nginx -s reload
ExecStop=/data/nginx/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target

2.增加执行权限

chmod 744 /usr/lib/systemd/system/nginx.service

3.重载sevice配置

systemctl daemon-reload

4.测试是否成功

systemctl enable nginx
systemctl start nginx