How to set nginx to start automatically on boot in linux?

To set Nginx to start automatically on boot in a Linux system, follow these steps: 1. Open the main configuration file for Nginx. This file is typically located at /etc/nginx/nginx.conf in most Linux distributions. Use a text editor (such as vi or nano) to open the file. 2. Find and edit the following line:

  user www-data;

  worker_processes auto;

Change the user line to a user with appropriate permissions, such as root, so that Nginx can run with administrator privileges at startup. Save and close the file. Check the system’s Init system. Currently, there are two main Init systems: SysV and Systemd. Execute the following command to determine which Init system your system is using:

  ps --no-headers -o comm 1

If the output is “init”, the system will use the SysV Init system; if the output is “systemd”, the system will use the Systemd Init system. Perform the following actions for different Init systems:

a. For SysV Init system: Create a startup script file /etc/init.d/nginx, and open the file with a text editor. Copy the following content into the file:

       #! /bin/sh

        ### BEGIN INIT INFO

        # Provides:          nginx

        # Required-Start:    $all

        # Required-Stop:     $all

        # Default-Start:     2 3 4 5

        # Default-Stop:      0 1 6

        # Short-Description: starts the nginx web server

        # Description:       starts nginx using start-stop-daemon

        ### END INIT INFO

        

        PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

        DAEMON=/usr/sbin/nginx

        NAME=nginx

        DESC=nginx

        

        test -x $DAEMON || exit 0

        

        set -e

        

        . /lib/init/vars.sh

        . /lib/lsb/init-functions

        

        case "$1" in

          start)

              echo -n "Starting $DESC: "

              start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \

                  --exec $DAEMON -- $DAEMON_OPTS || true

              echo "$NAME."

              ;;

          stop)

              echo -n "Stopping $DESC: "

              start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \

                  --exec $DAEMON || true

              echo "$NAME."

              ;;

          restart|force-reload)

              echo -n "Restarting $DESC: "

              start-stop-daemon --stop --quiet --pidfile \

                  /var/run/$NAME.pid --exec $DAEMON || true

              sleep 1

              start-stop-daemon --start --quiet --pidfile \

                  /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true

              echo "$NAME."

              ;;

          reload)

              echo -n "Reloading $DESC configuration: "

              start-stop-daemon --stop --signal HUP --quiet --pidfile \

                  /var/run/$NAME.pid --exec $DAEMON || true

              echo "$NAME."

              ;;

          *)

              N=/etc/init.d/$NAME

              echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2

              exit 1

              ;;

        esac

        

        exit 0

Save and close the file. Set the script file to be executable.

       sudo chmod +x /etc/init.d/nginx

Add Nginx to the system startup.

       sudo update-rc.d nginx defaults

For Systemd Init systems: create an Nginx service file `/etc/systemd/system/nginx.service` and open the file using a text editor. Copy the following content into the file:

       [Unit]

        Description=Nginx

        After=network.target

        

        [Service]

        ExecStart=/usr/sbin/nginx

        ExecReload=/usr/sbin/nginx -s reload

        ExecStop=/usr/sbin/nginx -s stop

        PrivateTmp=true

        

        [Install]

        WantedBy=multi-user.target

Save and close the file. Restart the systemd configuration files:

       sudo systemctl daemon-reload

Install Nginx on the system boot.

广告
Closing in 10 seconds
bannerAds