The Ultimate Guide to Setting Up a WordPress VPS – Part 2

The WordPress VPS Series

In this tutorial, the second part of our WordPress VPS series, I’m going to walk through setting up a web server using Ubuntu 11.04 Natty, NGiNX, PHP-FPM and MySQL. I use this setup for all of my servers, and this post is my way of remembering the process, and maybe help some others out.

I’m not a Linux administration expert, I’ve just read many tutorials and learned things on my own. The steps within this tutorial are mostly gleaned from other tutorials you can find on the internet, especially my favourite one in relation to WordPress, High performance WordPress from Themesforge.

I’m assuming you will be using a Windows-based PC and creating a VPS at VPS.net. This is a very popular combination which is why I’ve chosen to concentrate on it. The procedure will be very similar if you are using Linux or a Mac, just the part where we connect to the VPS will be slightly different

Setting up Your Host and Ubuntu OS

To open an account at VPS.net head over to their site, and choose the number of nodes you would like to power your server. If you’re not sure, you can also click on the WordPress icon which will give you a recommended amount of nodes for running WordPress sites. If you just want to host a site handling a small to moderate amount of visitors, you might also get by with one or two nodes. Don’t buy more than you need, you can always add more nodes as you go along.

Once you’ve signed up, you will have access to the user panel on the VPS.net site. You can then set up your first server by choosing the hostname for your server e.g. vps.wpmayor.com, and a label for your own reference, e.g. WPMayor VPS. You can also add some backup services and server add ons such as Cpanel at this stage. Next you will need to choose a location for your cloud e.g. EU – UK, a zone e.g. London, and the OS. For OS choose Ubuntu, then you can also choose which version of Ubuntu server you need.

When going for Ubuntu server, you have the choice of either 10.04, which is a Long Term Support (LTS) release, or the later version Ubuntu 11.04. The long-term support (LTS) releases are supported for five years on the server and are ideal for organisations that need more stability for larger deployments. In my case I will be hosting a few WordPress sites, and I don’t really need that level of stability as I will be upgrading servers fairly frequently.

Once you’ve made your choices, you just need to sit back while the good guys at VPS.net set up your server with the specified options, this shouldn’t take more than an hour or two.

Nginx and PHP

In the first part of this guide, we mentioned that we will be using Nginx as our web server instead of Apache, giving us a notable performance boost. It is definitely possible configure Apache in such a way that it can have similar performance capabilities, however that takes a lot of work and complications, and that’s not what we want, right? On the other hand Nginx & PHP-FPM are very fast right out of the box, so we’ll use them. In relation with WordPress, the only thing we have to look out for is the .htaccess files, which are Apache related and so won’t work on Nginx. We will however use a workaround to retain the same functionality, don’t worry, it’s really easy.

Ngnix, unlike Apache, doesn’t actually load PHP. Instead, it hands it off as a proxy to a “php handler” which acts like an Application Server. So Nginx by itself won’t serve PHP files, but just static files.

Off we go then!

1. Connecting to the VPS

To connect to the Linux VPS from Windows, we need to download a piece of software named Putty. You can head over to the Putty website and download the Windows installer. When everything is installed, start the Putty program

If you’re wondering how to change the @vps part (which is called the hostname), we will do that now. Your hostname should be something unique; some people name their systems after planets, others after philosophers, etc. Please note that the system’s hostname has no relationship to websites or email services hosted on it, aside from providing a name for the system itself. Thus, your hostname should not be “www” or anything else too generic.

Issue the following commands to set the hostname, replacing “plato” with the hostname of your choice:

If it exists, edit the file /etc/default/dhcpcd to comment out the “SET_HOSTNAME” directive:

To check what your hostname setting is, just type:

2. Get root privileges

In order to perform all the steps in this tutorial, we need to have root privileges. You can either prepend all commands with the string sudo, or just become root once and for all, using the command:

Ubuntu will then ask you for the root password, input that and we’re ready to continue.

An alternative is to login directly with the root user, although this practice is not considered to be the proper way to do things. The idea is that you should always be using your own user and then only for cases where you’re 100% sure of what you’re doing will you switch over to root.

In case you lost your password, the ‘Overview’ tab within the VPS.net control panel will have your ‘initial root password’ displayed. That’s what you need. Upon logging in the first time, it is advisable to change the root password to something secure but at the same time more memorable for yourself.

To change the root password:

3. Install Nginx

We’ll be installing the nginx-extras PPA which contains the latest version of the Nginx server plugs many other extras and modules. We will need some of these extra features later on when configuring caching on the server.

First step is to install the python-software-properties software which is needed ot use the next command add-apt-repository.

Now that we have that installed, we can proceed with adding the PPA source:

As we mentioned we will install nginx-extras now, and restart the service, ending up with a functioning web server.

Time for a quick check, if you’ve followed all the steps outlined above, you should now be able to point your browser to your server’s IP address, and receive a ‘Welcome to nginx!’ message. Congratulations, you’ve just installed your web server and have it running! Not that difficult was it?

4. Install PHP

For running WordPress sites, we definitely need PHP, and on our server we are going to install PHP-FPM (FastCGI Process Manager). FastCGI is a variation on the earlier Common Gateway Interface (CGI); FastCGI’s main aim is to reduce the overhead associated with interfacing the web server and CGI programs, allowing a server to handle more web page requests at once.

Go ahead and install PHP5-FPM from the Ubuntu repository along with a bunch of other valuable modules we will most likely need later on.

Time to restart PHP and Nginx to make sure everything is working fine.

You will probably get a warning when restarting PHP:

[WARNING] [pool www] pm.start_servers is not set. It’s been set to 20.

It’s nothing to worry about, there is an option (pm.max_children) that controls how many PHP processes are created on the VPS. For now we can set it to 5, in the future you can change it if needed. Open the file found at /etc/php5/fpm/pool.d/www.conf

Within the file change the following settings (you can use CTRL-W to search):

To confirm that everything works now without any error message, restart PHP again:

Perfect, now we can proceed to test whether PHP is really working as expected. We can create a ‘Hello World’ PHP page to test this. The default Nginx web root can be found at /usr/share/nginx/www, so we will create our file there.

Save the file using CTRL-X and click ‘Y’ to accept.

We need to do one more step before checking out our newly created page. We need to tell Nginx to pass requests for files ending in .php to the PHP-FPM backend:

Now you need to uncomment the following lines:

Save that file and restart nginx using the command mentioned earlier.

Now you can go to your browser and type in http://[your_ip_here]/hello.php. You should see the ‘Hello World’ text as well as the system information as output by the phpinfo() function.

Step completed! Now we have Nginx and also PHP installed and ready to go, time to add some database goodness!

5. Install MySQL

This is a very straightforward section, just enter the following commands:

All you have to do during the install process is to set the root password, choose something secure.

Once that’s installed, you can use the MySQL hardening process for securing it further:

The script will guide you through several steps to lockdown the MySQL installation.

The first step is to set the root password. By default a root password isn’t set, so to set it, hit Enter when asked for the current password (meaning blank) and then set the password as directed. Setting the root password ensures that nobody can log into the MySQL root user without the proper authorisation.

By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account. The anonymous user is there just for testing. Type ‘y’ and hit Enter when asked to remove the anonymous user account.

To ensure that the root user can not login over the network (and allow root connections only from the local machine). Type ‘y’ and hit Enter when asked to disallow remote roots.

By default, MySQL comes with a database named ‘test’ that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. To remove it type ‘y’ and hit Enter when asked.

And that is it, if you answered positively to all the steps above your MySQL installation should now be secure.

6. Install phpMyAdmin

This one is pretty straightforward too.

This will open some installation dialog screens, select Apache when given the choice (yes we are using Nginx but this still is the best option in our setup). You will also have to enter the passwords set earlier.

For security purposes, I suggest you run phpMyAdmin on a separate port to the rest of your site. In this way you are separating things and you can also have more flexibility in locking down that port.

To configure this, we will be diving into the Nginx virtual hosts setup right now. In the directory /etc/nginx/ you will find two directories which concern us:

  • sites-available 
    Inside here will be files containing the configurations for each site you want to serve.
  • sites-enabled
    These are symlinks to whichever sites you want to enable. So if you had 5 sites configured and available (i.e. in the sites-available folder), nothing would happen until you enabled them to be served.

Our first step is therefore to create a configuration file for phpMyAdmin in /sites-available:

Enter the following configuration instructions into this file, making sure to select the port you want to run phpMyAdmin on (in the example 8084), and also to enter your IP address for the server_name option:

Now it’s time to enable the new site we have configured by creating a symlink in /sites-enabled back to /sites-available:

Time to restart Nginx, by now you should know the command by heart, but here it is anyway:

If everything has worked fine for you, you should see the phpMyAdmin login screen when you navigate to http://yourserverip:8084/

That’s all for today, stay tuned for the next part of the series, where we will set up WordPress and some caching for blazing fast performance.

If you enjoyed this post, make sure to subscribe to WPMayor’s RSS feed.


About Jean Galea

Jean Galea is a WordPress developer, entrepreneur and padel player. He is the founder of WP Mayor, the plugins WP RSS Aggregator and EDD Bookings, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Related Articles

19 Responses

  1. Cliff Kunz
    Cliff Kunz June 1, 2012 at 09:54 | | Reply

    Nice tutorial! This will be my weekend project 🙂

  2. Alex
    Alex July 12, 2012 at 10:18 | | Reply

    why Install phpmyadmin if you have root access?
    tunneling an local sql client over SSH would improve security.

    Next I would advice an update mechanisme in the OS part

  3. harry
    harry February 5, 2013 at 22:36 | | Reply

    I get a 502 bad gateway when visiting http://myipaddress:8084 after setting up phpmyadmin by following the instructions. Normal php pages (e.g. http://myipaddress/hello.php) work fine. Any ideas?

  4. Harry
    Harry February 27, 2013 at 21:14 | | Reply

    The cause of the problem is this line in the config for the vhost: fastcgi_pass 127.0.0.1:9000;
    I find that when I replace it with fastcgi_pass unix:/var/run/php5-fpm.sock; it works

    I do try serverfault quite a bit for help with this kinda thing . Is the unix board more active?

  5. Andrei Chira
    Andrei Chira March 11, 2013 at 13:44 | | Reply

    I get an error after installing and restarting nginx:
    nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
    nginx: configuration file /etc/nginx/nginx.conf test failed

    1. Andrei Chira
      Andrei Chira March 11, 2013 at 20:22 | | Reply

      it was an ipv6 issue. resolved.

  6. ssword
    ssword April 4, 2013 at 22:45 | | Reply

    Thanks for the great tutorials, you did a great job.
    Just a hint for 12.10 Ubuntu users, in the distribute, the command add-apt-repository is not longer in python-software-properties, instead, it is in software-properties-common, so you need to do “apt-get install software-properties-common”, other than “apt-get install python-software-properties”.

  7. alisanta
    alisanta May 24, 2013 at 11:48 | | Reply

    what a great tutorial … totally follow your steps !

  8. Frank
    Frank May 27, 2013 at 12:49 | | Reply

    When will there be a part 4?

  9. Rickson
    Rickson May 31, 2013 at 07:53 | | Reply

    Hi,

    Great article. Do you have some tip to configure the firewall? I mean It’s secure install only the nginx/mysql/etc. without a firewall?

    Thank you very much and keep the great work. 🙂

  10. hqmgr
    hqmgr July 10, 2013 at 19:32 | | Reply

    best free alternative cpanel/whm to use on vps with NGiNX,? also what about centOS 64x is not good for vps with wp?

  11. dhruv
    dhruv October 5, 2013 at 17:33 | | Reply

    the default nginx path for me is /usr/share/nginx/html instead of /usr/share/nginx/www ….. would that make any difference ?

  12. vps notes
    vps notes February 6, 2014 at 01:26 | | Reply

    great tutorial. but i really want to ask what plugin was used to show the code snippets above?

  13. Charlie Pryor (@CharliePryor)
    Charlie Pryor (@CharliePryor) March 2, 2014 at 21:40 | | Reply

    I get to the part where you have to create the “Hello World” page, and I get this after entering the command:

    cd /usr/share/nginx/www

    It gives me:

    -bash: cd: /usr/share/nginx/www/: No such file or directory

    What have I done wrong here? I’m following everything step by step without a problem so far.

    1. Charlie Pryor (@CharliePryor)
      Charlie Pryor (@CharliePryor) March 2, 2014 at 22:32 | | Reply

      nevermind… for more recent “up to date” tutorials on this, and as a reference to others here, the “www” from this command is actually “html” now.

      Now though, I’ve made it to the last step in the Nginx section here, and I’m getting 502 Bad Gateway when I look at the hello.php file. Any help with this?

  14. satta
    satta March 1, 2015 at 10:41 | | Reply

    Hey i know this is a old guide but can you please tell me how to setup nginx+php-fpm+varnish in a google cloud VM. And how much CPU and Ram recommended for the setup.

Leave a Reply