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

If you purchase through a link on our site, we may earn a commission. Learn more.

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.
Table of Contents
WP Engine High Performance Hosting
BionicWP Hosting

The WordPress VPS Series

  • Part 1 – Introduction & Hardware/Software Selection

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:

[bash]
echo "plato" > /etc/hostname
hostname -F /etc/hostname
[/bash]

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

[bash]#SET_HOSTNAME=’yes'[/bash]

To check what your hostname setting is, just type:

[bash]hostname[/bash]

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:

[bash]sudo su[/bash]

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:

[bash]passwd[/bash]

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.

[bash]apt-get install python-software-properties[/bash]

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

[bash]
nginx=stable
add-apt-repository ppa:nginx/$nginx
apt-get update
[/bash]

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

[bash]
apt-get install nginx-extras
service nginx restart
[/bash]

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.

[bash]apt-get install php5-fpm php5-cgi php5-common php5-suhosin php-apc php5-mysql php5-dev php5-curl php5-gd php5-imagick php5-mcrypt php5-snmp[/bash]

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

[bash]service php5-fpm restart
service nginx restart[/bash]

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

[bash]
cd /etc/php5/fpm/pool.d/
nano www.conf
[/bash]

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

[bash]
pm = static
pm.max_children = 5
[/bash]

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

[bash]service php5-fpm restart[/bash]

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.

[bash]
cd /usr/share/nginx/www
touch hello.php
nano hello.php
<?php echo ‘<h1>Hello World</h1>’;
phpinfo();
?>
[/bash]

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:

[bash]
cd /etc/nginx/sites-available/
nano default
[/bash]

Now you need to uncomment the following lines:

[bash]
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
[/bash]

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:

[bash]apt-get install mysql-server mysql-client[/bash]

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:

[bash]mysql_secure_installation[/bash]

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.

[bash]apt-get install install phpmyadmin[/bash]

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:

[bash]
cd /etc/nginx/sites-available
touch phpmyadmin
nano phpmyadmin
[/bash]

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:

[bash]
server {
listen 8084;
server_name XX.XX.XX.XX;
access_log /var/log/nginx/localhost.access.log;
root /usr/share/phpmyadmin;
index index.php;
location / {
try_files $uri $uri/ @phpmyadmin;
}
location @phpmyadmin {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin/index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_NAME /index.php;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin$fastcgi_script_name;
include fastcgi_params;
}
}
[/bash]

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

[bash]
ln -s /etc/nginx/sites-available/phpmyadmin /etc/nginx/sites-enabled/phpmyadmin
[/bash]

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

[bash]service nginx restart[/bash]

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.


Jean Galea

Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Discover more from our archives ↓

Popular articles ↓

19 Responses

  1. 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.

  2. 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. 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?

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

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

  5. 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. 🙂

  6. 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. 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

  8. 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?

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

  10. 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

Share Your Thoughts

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

Claim Your Free Website Tip 👇

Leave your name, email and website URL below to receive one actionable improvement tip tailored for your website within the next 24 hours.

"They identified areas for improvement that we had not previously considered." - Elliot

By providing your information, you'll also be subscribing to our weekly newsletter packed with exclusive content and insights. You can unsubscribe at any time with just one click.