Start nginx on boot - Ubuntu - the solution


I always wondered why nginx didn’t started on boot. Well, it actually did, but too soon. So I needed to change the order. Doing this is not so obvious. That’s why I’m posting the solution I found.

The system is Ubuntu 18.04 with Virtualmin and nginx module.

1- First, remove apache to start;
Type in the command line:
sudo update-rc.d -f apache2 remove

2- Change the order of nginx to start;
Access /etc/init.d/nginx;
Webmin > System > Bootup and Shutdown -> Select ‘nginx’ ;
Remove the original 2 lines

Required-Start: $local_fs $remote_fs $network $syslog $named

Required-Stop: $local_fs $remote_fs $network $syslog $named

And add this 2 lines

Required-Start: $remote_fs $network

Required-Stop: $remote_fs $network

That´s it. Now nginx will start and work after a system reboot/shudown.

Thank you! I had the issue and your workaround worked.

Also, if nginx is running too soon, it seems a little strange that removing $named would allow the system to boot up properly, since I presume it would start up even earlier?

Just wondering, did you only experience this issue on Ubuntu 18.04? I only had this issue from 18.04 onwards.

Right now the solution pointed up is not working anymore.

Googling… I found a solution (which is more a tweak). You can see link

Which says:
The only way that I’ve found to solve it is enabling auto restart on nginx process:

Adding this to ‘[Service]’ section of ‘/etc/systemd/system/’ solves the problem.

# Avoid problem with nginx not starting right after a reboot Restart=always RestartSec=5

If you try to edit the file via Virtualmin’s Filemanager, you won’t be able, because it is a symbolic link. You should do via command line:

sudo nano /etc/systemd/system/

I actually found a simpler solution. The problem was that nginx is slated to run before the IPv6 interface is up, causing it to fail. I made the following changes which seems to work fine:

nano /lib/systemd/system/nginx.service
Change to
Run systemctl disable nginx.service followed by systemctl enable nginx.service

1 Like