Fix 502 Bad Gateway Error after Reboot on Virtualmin-Nginx Server

Websites hosted on Virtualmin-Nginx servers started to give 502 Bad Gateway errors after the server reboot. This is only happening on fresh Virtualmin installs or recently added websites. This is a bug in Webmin v1.810. It has been reported to developers, but haven’t been fixed yet.

There’s something you can do to fix this temporarily while waiting for developers of Virtualmin to come up with a permanent solution. The bug is reported to the developers by jg and also came up with this temporarily workaround. I’ve adapted it to Ubuntu. I’ve tested this workaround on both Ubuntu 14.04 LTS with PHP 5.6 and Nginx 1.4.6 and Ubuntu 16.04 LTS with PHP 7 and Nginx 1.10 and got it working on both with few changes to file paths.

You can read more about the issue on bug report page, jg did a great job explain it. And a big thank for coming up this solution.

With the recent update of Webmin, the site specific init file does not get created automatically. This simple workaround is to create it manually. The file that does not get created is /etc/init.d/php-fcgi-example-com. Let’s create it.

Login to your server as root and create a new file. Please replace example-com with your domain name. And note that it’s a Hyphen (-) not a dot (.) between domain and extension.

nano /etc/init.d/php-fcgi-example-com

You need to paste some code to this file. But before you do, you need to make few changes to it. And you should also select the code for your PHP version from the below.

  • Replace example with your administrative user name for the server (not root). You can copy this code to a text editor and use search and replace function. There are 6 occurrences to be replaced. And if your domain extension isn’t .com, you should change it too. There are 2 occurrences of that
  • And replace 11111111111111 with your server’s sock file number. You can find it by going to Webmin > Servers > Nginx webserver > Edit configuration files and then selecting config file for your domain. Scroll down and you’ll see the reference to the sock file like below. There are 3 occurrences of this.

php-nginx-socket-file

With the changes being made, paste following code to the file you just created.

#!/bin/sh
### BEGIN INIT INFO
# Provides: php-fcgi-example-com
# Required-Start: $network $syslog
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop:
# Description: Start Nginx PHP fcgi server for example.com
### END INIT INFO

case "$1" in
'start')
	su example -c PHP_FCGI_CHILDREN\=4\ PHPRC\=\/home\/example\/etc\/php7\.0\ \/usr\/bin\/php\-loop\.pl\ \/usr\/bin\/php\-cgi\ \-b\ \/var\/php\-nginx\/11111111111111\.sock\/socket\ \>\>\/home\/example\/logs\/php\.log\ 2\>\&1\ \<\/dev\/null\ \&\ echo\ \$\!\ \>\/var\/php\-nginx\/11111111111111\.php\.pid
	RETVAL=$?
	;;
'stop')
	su example -c kill\ \`cat\ \/var\/php\-nginx\/11111111111111\.php\.pid\` ; sleep 1
	RETVAL=$?
	;;
'restart')
	$0 stop ; $0 start
	RETVAL=$?
	;;
*)
	echo "Usage: $0 { start | stop }"
	RETVAL=1
	;;
esac
exit $RETVAL
#!/bin/sh
### BEGIN INIT INFO
# Provides: php-fcgi-example-com
# Required-Start: $network $syslog
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop:
# Description: Start Nginx PHP fcgi server for example.com
### END INIT INFO

case "$1" in
'start')
	su example -c PHP_FCGI_CHILDREN\=4\ PHPRC\=\/home\/example\/etc\/php5\ \/usr\/bin\/php\-loop\.pl\ \/usr\/bin\/php5\-cgi\ \-b\ \/var\/php\-nginx\/11111111111111\.sock\/socket\ \>\>\/home\/example\/logs\/php\.log\ 2\>\&1\ \<\/dev\/null\ \&\ echo\ \$\!\ \>\/var\/php\-nginx\/11111111111111\.php\.pid
	RETVAL=$?
	;;
'stop')
	su example -c kill\ \`cat\ \/var\/php\-nginx\/11111111111111\.php\.pid\` ; sleep 1
	RETVAL=$?
	;;
'restart')
	$0 stop ; $0 start
	RETVAL=$?
	;;
*)
	echo "Usage: $0 { start | stop }"
	RETVAL=1
	;;
esac
exit $RETVAL

Save the file and exit the nano text editor. Change permissions of the file to 755,

chmod 755 /etc/init.d/php-fcgi-example-com

Now you need to make it start on reboot. You can do this by going to Webmin > System > Bootup and Shutdown. Tick the init file you just created from the list and click Start on Boot from the bottom. Reboot the server and 502 Bad Gateway error will disappear. But the bad news is you’ll have to keep doing this for every website you create until developers fix it 🙁

Tharindu

Hey!! I'm Tharindu. I'm from Sri Lanka. I'm a part time freelancer and this is my blog where I write about everything I think might be useful to readers. If you read a tutorial here and want to hire me, contact me here.

Related Articles

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button