CentOS7 sets the nginx service to start automatically after booting [starting after booting]

Method 1: Use Systemd

1. Create a service unit file

sudo vi /etc/systemd/system/nginx.service

2. Edit the configuration file

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target

[Service]
Type=forking
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

insert image description here

3. Start the nginx service

sudo systemctl enable nginx.service

insert image description here

4. Restart the system to try

reboot

After shutting down, we found that the connection was not available
insert image description here
, and after waiting for a while, the result came out~

Method 2: Use rc.local

1. editrc.local

sudo vi  /etc/rc.d/rc.local

2. Add commands

Add the following line to the file, this will execute the command to start NGINX on system boot:

/usr/sbin/nginx

Save and close the file.

3. Add executable permission to `rc.local·

sudo chmod +x /etc/rc.d/rc.local

Make sure the owner and group of the rc.local file are both root:

sudo chown root:root /etc/rc.d/rc.local

Guess you like

Origin blog.csdn.net/qq_22841387/article/details/131362796