Enabling WordPress Rewrite Rules by Default With Virtualmin / Nginx

Virtualmin is a fantastic (and free!) alternative to cPanel. It gives you a nice web interface to easily add hosting for many sites on a single VPS or dedicated server.

By default, Virtualmin will setup Apache for your webserver, which is Ok, but not great. Thankfully, Nginx is available as a supported add-on. However, in the default setup, WordPress permalinks do not work correctly.

There are quite a few tutorials around on how to fix this, and they all amount to:

  1. Setup the Virtual host through the wizard
  2. Jump into Webmin
  3. Browse to the Nginx config screen
  4. Bring up the config file for the Virtual host you just created
  5. Manually paste in some lines of code and hope that you didn’t mess anything up.

I’d like to eliminate the list down to a single item.

This does involve some file editing on the command line, but it only happens once. After that, all of the hosts you add will be automatically setup for pretty permalinks.

Step 1:

Login as root, or sudo and edit the following file in your favorite editor:

/usr/share/webmin/virtualmin-nginx/virtual_feature.pl

Jump down to line 247, and you’ll see the following:

my $ploc = { 'name' => 'location',
                'words' => [ '~', '\.php$' ],
                'type' => 1,
                'members' => [
                { 'name' => 'try_files',
                    'words' => [ '$uri', '=404' ],
                },
                { 'name' => 'fastcgi_pass',
                    'words' => [ $port =~ /^\d+$/ ?
                                'localhost:'.$port :
                                'unix:'.$port ],
                },
                ],
            };
&save_directive($server, [ ], [ $ploc ]);

Right below that (before the “&flush_config_file_lines();” line), paste in the following:

my $wordpress = { 'name' => 'location',
                'words' => [ '/' ],
                'type' => 1,
                'members' => [
                { 'name' => 'try_files',
                    'words' => [ '$uri', '$uri/', '/index.php?$args' ],
                },
                ],
            };
&save_directive($server, [ ], [ $wordpress ]);

Save and exit.

Now try to create a new virtual host. If you’re not presented with any error messages congradulations! Every site you add now will automatically be setup for pretty WordPress permalinks!