Running Nginx
The following describes two methods of starting the Nginx server: Start the Nginx server using the script command and start the Nginx server as a service. The latter method is more convenient and recommended.
- Start Nginx.Method 1: Use a script to start Nginx.
1/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
For Nginx 1.14.2 or Nginx 1.19.3 installed using the mirror site RPM package on CentOS 7.6 or CentOS 8.1, run the following command to start Nginx:
/usr/sbin/nginx -c /etc/nginx/nginx.conf /usr/sbin/nginx -s reload
Method 2: Start Nginx as a service. Add Nginx to the Service list, and then start the service using a command. Service can start, stop, and restart applications, or view application status. You do not need to manually go to the application directory to perform operations. In addition, Service can be used to implement automatic startup of applications. That is, applications automatically start upon OS startup.- Open the /etc/init.d/nginx file.
1 2
rm -rf /etc/init.d/nginx vim /etc/init.d/nginx
- Press i to enter the insert mode and add the following content to the file:
For Nginx 1.14.2 or Nginx 1.19.3 installed using the mirror site RPM package on CentOS 7.6 or CentOS 8.1, modify the script as follows:
- Replace /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf with /usr/sbin/nginx -c /etc/nginx/nginx.conf.
- Replace /usr/local/nginx/sbin/nginx -s quit with /usr/sbin/nginx -s quit.
#!/bin/bash # chkconfig: 2345 10 90 # description: nginx case "$1" in 'start') /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf echo "$0_start"; ;; 'stop') /usr/local/nginx/sbin/nginx -s quit echo "$0_stop"; ;; esac - Press Esc, type :wq!, and press Enter to save the file and exit.
- Modify the permission on the /etc/init.d/nginx file.
1 2
chmod 777 /etc/init.d/nginx ll /etc/init.d/nginx
- Add Nginx to the chkconfig management list.
1chkconfig --add /etc/init.d/nginx
- Enable Nginx to automatically start upon OS startup.
1chkconfig nginx on
- Start Nginx.
1service nginx start
- Open the /etc/init.d/nginx file.
- Check the Nginx process.
1ps -ef | grep nginx

Parent topic: Running and Verifying Nginx