Rate This Document
Findability
Accuracy
Completeness
Readability

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.

  1. 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.
    1. Open the /etc/init.d/nginx file.
      1
      2
      rm -rf /etc/init.d/nginx
      vim /etc/init.d/nginx
      
    2. 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:

      1. Replace /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf with /usr/sbin/nginx -c /etc/nginx/nginx.conf.
      2. 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
    3. Press Esc, type :wq!, and press Enter to save the file and exit.
    4. Modify the permission on the /etc/init.d/nginx file.
      1
      2
      chmod 777 /etc/init.d/nginx
      ll /etc/init.d/nginx
      
    5. Add Nginx to the chkconfig management list.
      1
      chkconfig --add /etc/init.d/nginx
      
    6. Enable Nginx to automatically start upon OS startup.
      1
      chkconfig nginx on
      
    7. Start Nginx.
      1
      service nginx start
      
  2. Check the Nginx process.
    1
    ps -ef | grep nginx