Migrating a Virtualmin Server from Apache to Nginx

A long while ago, I installed virtualmin to manage my web server. At that time, the only web server available was Apache 2. Recently, I’ve been running some tests on Nginx, and feel it could be much faster, especially for cached pages on my site. This post covers how to migrate your existing Virtualmin Apache servers over to Nginx. It’s not the easiest task, and will involve some down time for your servers.

[GARD]

Background

Unfortunately, it’s not possible to run a mix of both Apache and Nginx web servers using the Virtualmin administration panel. If you want to migrate to Nginx, you’re going to have to go the whole hog and migrate all of your websites.

I want to be able to run WordPress sites with my new Nginx server, and so this guide will be aimed at making this possible.

The migration process is destructive to Apache configuration files and associated log files. You’d be well advised to take a backup of your system before going any further!

Installing Nginx for Use with Virtualmin

This is covered well on Virtualmin’s website. You’ll not be able to disable Apache in the Features and Plugins section yet, as it’s still being used by your servers, but do enable Nginx to make the feature available to your sites.

Migrating a Single Server

The default Virtalmin Nginx configuration is actually quite good. My main problem is that I’m trying to run WordPress sites, and the pretty permalinks involving post dates or category name do not work with this setup.

The easiest way of fixing this is via the console. Open an SSH terminal to your server and cd to the /etc/nginx/sites-available/ directory. As root, open each of your virtual host configuration files in turn (this is infuriatingly slow if you’ve got lots of virtual hosts… Maybe someone can suggest an automated method??)

Within each config file, you’ll see a location block like this:

location ~ \.php$ {
  try_files $uri =404;
  fastcgi_pass unix:/var/php-nginx/{{LONGNUMBER}}.sock/socket;
}

Just before it, insert the following:

location / {
  try_files $uri $uri/ /index.php?$args;
}

[GARD]

Migrating Servers with Alias Servers

Unfortunately, when you try and disable the Apache website in a server that contains alias servers, Virtualmin won’t let you as it detects the alias servers are still configured to use Apache. Changing the alias servers from Apache to Nginx first doesn’t work either, as they can’t detect a parent server running Nginx. The only was I could find to do this was to disable Apache in all the alias servers first, then change the top-level server from Apache to Nginx, then go back over all the alias servers and enable Nginx. Quite long winded…

Booting Your Shiny New Nginx Powered Websites

You’re probably going to want to add a service status monitor to Virtualmin for your new Nginx server. Click Webmin then Others > System and Server Status. Then Add a Monitor of Type Nginx Webserver.

Back to Virtualmin > System Information, and you should now see your Nginx server in the status panel. Click the X in the Actions for the Apache Webserver to shut this down. Then click the Play button in the actions for the Nginx Webserver. If all has gone well, your sites should now be powered by Nginx!

Make Nginx Your Default Webserver

Now that your sites are all running Nginx, it’s time to set it as the default web server. This means that it is the web server that will be used if you restart your machine, and also the default web server for any new sites you host.

[GARD]

Make Newly Hosted Sites Use Nginx

Firstly, let’s make Nginx the default web server for any new sites that you host. From within Virtualmin, click System Settings then Features and Plugins. Make sure both checkboxes for both Nginx SSL Website & Nginx Website are ticked, one checkbox will make the feature available (you’ll probably have done this already during installation), the other makes it the default service.

For Apache Website and SSL website, do the opposite. Make sure neither of the checkboxes associated with these features is ticked.

Click Save at the bottom of the screen.

Make Nginx the Webserver that is Started on Boot

Click Webmin then click System and Bootup and Shutdown. Select the Apache2 check box and then at the bottom of the screen click Disable on Boot.

Then, select the nginx checkbox, and at the bottom of the screen click Start on Boot.

What’s Next?

As it stands, your new nginx web server probably isn’t really that much better than your old Apache one. It might be a little more memory efficient. The real benefit of nginx is serving static content. Using a cache plugin on your WordPress site means that static HTML files can be served, bypassing PHP completely. I’d seriously recommend reading the sections about W3 Total Cache and WP Super Cache on the nginx page of the WordPress site.

As ever your comments and thoughts on all of this are welcome below.


COMMENTS