How to Configure Nginx to Start Automatically on System Boot in Ubuntu


2 views

In Ubuntu systems (including Hardy Heron), services are typically managed through init scripts located in /etc/init.d/. However, modern Ubuntu versions use systemd as the default init system. The traditional method of placing scripts in /etc/init.d/ still works, but there are more robust approaches available.

For your custom Nginx installation at /opt/nginx/sbin/nginx, create a proper init script:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $local_fs $network $syslog
# Required-Stop:     $local_fs $network $syslog
# 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=/opt/nginx/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

set -e

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --exec $DAEMON
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --exec $DAEMON
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --exec $DAEMON
    sleep 1
    start-stop-daemon --start --quiet --exec $DAEMON
    echo "$NAME."
    ;;
  reload)
    echo -n "Reloading $DESC configuration: "
    start-stop-daemon --stop --signal HUP --quiet --exec $DAEMON
    echo "$NAME."
    ;;
  *)
    echo "Usage: $NAME {start|stop|restart|reload|force-reload}" >&2
    exit 1
    ;;
esac

exit 0

Save this as /etc/init.d/nginx, then make it executable and register it:

sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults

For systems with systemd, create a service file:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

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

[Install]
WantedBy=multi-user.target

Save this as /etc/systemd/system/nginx.service, then enable it:

sudo systemctl daemon-reload
sudo systemctl enable nginx

After setting up either method, test the automatic startup:

sudo reboot
# After reboot
ps aux | grep nginx

If Nginx fails to start automatically:

  1. Check permissions: Ensure the nginx binary is executable (chmod +x /opt/nginx/sbin/nginx)
  2. Verify paths: The init script or systemd unit must point to the correct binary location
  3. Check error logs: cat /opt/nginx/logs/error.log
  4. Test the configuration: /opt/nginx/sbin/nginx -t

For temporary solutions, you can use cron's @reboot feature:

sudo crontab -e
# Add this line:
@reboot /opt/nginx/sbin/nginx

However, this method isn't recommended for production systems as it lacks proper service management capabilities.


When managing web servers, having services automatically start on system boot is crucial for maintaining uptime. Many Ubuntu/Debian administrators face this exact issue with Nginx - while you can manually start it using sudo /opt/nginx/sbin/nginx, the service doesn't persist across reboots.

Ubuntu's init system expects services to follow specific conventions. While /etc/init.d/ was traditionally used, modern systems prefer systemd units. The issue occurs because:

  • The custom Nginx installation path isn't registered with the init system
  • No proper service file exists for systemd to manage the process

The most reliable solution is creating a custom systemd unit file. Here's how to implement it:


# Create the service file
sudo nano /etc/systemd/system/nginx.service

# Add this content:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

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

[Install]
WantedBy=multi-user.target

After creating the service file, run these commands:


# Reload systemd to recognize the new service
sudo systemctl daemon-reload

# Enable Nginx to start on boot
sudo systemctl enable nginx

# Start the service immediately
sudo systemctl start nginx

# Verify status
sudo systemctl status nginx

For systems not using systemd, you can create a traditional init script:


sudo nano /etc/init.d/nginx

#!/bin/sh
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# 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=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx

test -x $DAEMON || exit 0

case "$1" in
    start)
        echo -n "Starting $NAME: "
        $DAEMON
        echo "."
        ;;
    stop)
        echo -n "Stopping $NAME: "
        $DAEMON -s quit
        echo "."
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    reload)
        echo -n "Reloading $NAME configuration: "
        $DAEMON -s reload
        echo "."
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
        ;;
esac

exit 0

Then make it executable and register it:


sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults
  • Ensure your Nginx binary path is correct in all scripts
  • Verify permissions on both the binary and configuration files
  • Check logs at /opt/nginx/logs/error.log for startup errors
  • Test configuration with /opt/nginx/sbin/nginx -t before implementing

After implementing either solution, reboot your server and verify Nginx is running:


sudo reboot
ps aux | grep nginx
curl -I localhost