If your website is running a little slowly under Apache, it might be worth trying a different web server. nginx is designed with performance in mind and might make your website a little bit quicker.
This guide is for Ubuntu, but you should be able to find similar commands on other platforms. Our LAMP Preinstall image will work with this guide.
Setting up nginx
First things first, you'll want to set up nginx. If you do this bit before you remove Apache, you'll minimise any downtime on your site. Let's start by installing nginx on your server. Run the following commands to do so:
sudo apt-get update sudo apt-get install nginx
We also need to install a special PHP handler. Apache processes PHP files using a plugin, but nginx passes that off to a separate application to keep itself fast. Anyway, that application is called PHP-FPM, and can be installed as follows:
sudo apt-get install php5-fpm
Because you've already got PHP set up on your server, all your existing PHP modules will still be set up. All the FPM wrapper does is allow nginx to work with PHP.
Now we have nginx and PHP installed, we'll need to set them up. For now, we'll set it up on a different port to Apache, so we can keep your site up and running. Edit the configuration file for the default site as follows:
sudo nano /etc/nginx/sites-available/default
In that configuration file, there'll be a line that says
listen
. Change it to say the following:
listen 8000;
We'll use port 8000 in this guide, but you can set it to pretty much anything you want except ports 80 or 443.
Then just start the server up to check that it works:
sudo service nginx start
Then just open up your server's port 8000 in a web browser and see if the nginx default page is displayed.
http://example.lsvps.co:8000
If you see a page that says "Welcome to nginx!" you're up and running.
Configuring nginx
Now you have the system set up, you'll want to make sure your nginx configuration matches what you've already set up with Apache. There are a few things you need to know while making the switch:
- .htaccess files aren't used by nginx, so they won't be read while serving your pages. You'll want to translate these into your nginx virtual host configuration files.
- For the above reason, the Apache directive AllowOverride has no equivalent in nginx.
- If you use the ServerAdmin directive, that's not supported in nginx.
- Make sure you set the same web root for your nginx site as for your Apache one.
The incredibly helpful Alexei Shabalin, developer of Winginx, has created a handy configuration file converter you can use to change your Apache configuration into a nginx-compatible file.
Unfortunately, this converter cannot handle URL rewrites, so you will need to translate those manually. It's fairly straightfoward to do that; there's an article on the nginx blog that might help you.
You also need to include a block in your virtual host file that says the following:
location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; }
This will tell nginx to pass off any PHP pages to PHP-FPM for processing. We also recommend including the following block to deny access to any .htaccess files you have on your site:
location ~/\.ht { deny all; }
Finally, at the top of the file, change the
listen
directive back to port 80. You'll also need to configure PHP-FPM a little to get it ready to serve your site.
Configuring PHP-FPM
There are just a few tweaks you need to make to your configuration files, but they're all fairly straightforward. Open up your php.ini file for editing as follows:
sudo nano /etc/php5/fpm/php.ini
Firstly, let's make sure PHP doesn't try to fix any incorrect path names. It'll stop your server from exposing any sensitive data in the event someone goes looking for it.
cgi.fix_pathinfo=0
If you've made any other changes to your php.ini file, you'll want to replicate them here too. Then you can save and exit that file. Now you need to configure the connection between nginx and PHP-FPM by opening another file:
sudo nano /etc/php5/fpm/pool.d/www.conf
Find the line that says
listen
and change it to match what you put in your nginx file:
listen = /var/run/php5-fpm.sock
Making the switch
At last, you can go live with your new nginx site by running the following command:
sudo service apache2 stop && sudo service nginx reload
That will stop Apache and start nginx up. If you've copied over all your configuration correctly, there shouldn't be any downtime and your site should be working right away.
Finally, you can run the following command to remove Apache from your system.
sudo apt-get remove apache2 apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common libapache2-mod-auth-mysql libapache2-mod-php5
Troubleshooting
For most fairly simple sites this should work without a hitch. However, if your site is a little bit more complicated, there could be some problems switching over.
The most common problem encountered while switching is that not all of the custom rewrites you have set up have been translated into new nginx configuration. Remember that .htaccess files don't work under nginx, so you'll need to transfer all your rewrite rules into your virtual host configuration file.
Another potential problem you might run into is what happens if you're using a special Apache module on your site that doesn't have an equivalent in nginx, or has one that's differently set up. Configuring these is outside the scope of this tutorial, but it's important to remember that you might have to find an alternative in nginx or set up your server differently to cope with your requests.
Hopefully, however, your switchover will be entirely worry-free and your site will be even faster under nginx. If you encounter any problems with your VPS during your switch, please do let us know.