Why not just stick with shared hosts?
Shared hosting can only take you so far when you are trying to build a website. At a certain point you are going to realize that your website just isn’t loading quickly enough for you and you want a bit more power behind it. In an age where site speed makes all the difference you have probably already tried to squeeze every bit of optimization out of your site with caching plugins. It would be great to have a dedicated server, but that would get a bit expensive ($100+/month).
One common option is to use a VPS, or virtual private server. Essentially this is taking a dedicated server and running multiple completely independent operating systems off it. This gives you “dedicated-like” hardware without the costs of a dedicated server. You have a very wide selection to choose from as far as hosts and options go, although my own personal preference is Rackspace Cloud.
Today I’m going to concentrate on setting up your own VPS running Nginx (pronounced en•gine x) as your web server on Ubuntu (pronounced oo•boon•tu) which is your base OS, or operating system.
What is Nginx?
Nginx is a web server software that allows your server to serve files. Nginx is fairly new to the web server work, relative to other popular web servers. If you look at the graph below you can see Apache as the “top dog” going all the way back to before 1995, while Nginx just starts showing up in March 2007.
Market Share for Top Servers Across All Domains (December 2011)
But even though it is a fairly new software Nginx already has over 8% market share, and powers 11.6% of the world’s active sites. For more information you can visit nginx.com and nginx.org.
Getting Started
If you are new to managing your own VPS, then you should read up on SSH (secure shell). Depending on your local OS, there are a wide variety of options for SSH. For Windows users I recommend PuTTY, which I will be using during this tutorial. All other OS typically have a built in method; OS X and Linux have terminal and on Chrome OS you can press Control+Alt+T for your terminal.

I won’t get into much detail with how apt-get works but, unlike Windows, where you download installers and then run them to install software, Ubuntu offers an Advanced Packaging Tool, or apt. Within apt on Ubuntu you have lists of software available to install, you can easily install these using simple commands through command line.
Featured Plugin - WordPress Wiki Plugin
1. Add and Update your Lists
The first apt command is used to update your software lists. If you do not update your list you may end up installing an old version of software or the software might not be available. In this case we have to update our list so we can install python-software-properties. This software will allow us to easily add other lists from Launchpad.net.
Enter the commands below. You’ll notice the first command is apt-get not apt. Anything after the pound sign is a comment simply explaining more about what the command does.
apt-get update # updates your available software list apt-get upgrade # upgrades all your currently installed software apt-get install python-software-properties # installs python-software-properties add-apt-repository ppa:nginx/stable # adds a list for nginx add-apt-repository ppa:nginx/php5 # adds a list for php5 add-apt-repository ppa:nijel/phpmyadmin # adds a list for phpmyadmin apt-get update # once we have new lists added we have to update them
2. Install Nginx & PHP
Now that all your lists are up-to-date with the required software, you need to install Nginx and PHP5.
apt-get install nginx php5-fpm php5-cli php5-curl php5-gd php5-mcrypt php5-mysql
3. Install MySQL and phpMyAdmin
Unless you have a remote database host (if you have to ask you don’t have one), you need to install mysql-server. I also recommend phpMyAdmin since that is what you are probably most familiar with.
apt-get install mysql-server phpmyadmin
During this process you will be prompted for a few passwords and asked if you want to auto-configure your web server.
First of all, set your root MySQL password. You will also be asked to confirm your password.

Now, auto-configure phpMyAdmin for a web server. Since Nginx is not available in this list just hit OK.
To get from the selection list to OK hit the tab key on your keyboard.

phpMyAdmin will then configure your MySQL server so it can access and manage it.
Choose “Yes”, you do want to configure database for phpmyadmin.

phpMyAdmin will also need its own user to access the MySQL server. It will generate the username “phpmyadmin” itself but you have the option of choosing a password. First it will ask you for your root password, then you have the option of creating a password or just hitting enter with the box blank to generate a random one.

4. Start Nginx and PHP
Now that all your software is installed you need to start the service. Unlike Apache you need to start PHP as its own service. Nginx cannot natively process PHP files so you are going to tell it to pass those files off to PHP-FPM’s service to process instead.
service nginx start service php5-fpm start
5. Add another User to Ubuntu
I know I said you are not going to setup security, but this next step does touch on this. You do not ever want your websites or services to be running as root. This is dangerous and can really hurt you, so we are going to add a new user to the server.
Enter the next command and follow the prompts to create the user.
adduser www
6. Download WordPress
Now that your new user is created, start another SSH session with your VPS and this time login as www instead of root. In order to configure Nginx for your websites you need to setup a few files.
mkdir -p logs/wordpress # creates the directory logs, with wordpress inside wget http://wordpress.org/latest.tar.gz # download the latest wordpress install tar -zxvf latest.tar.gz # extract the archive files
7. Create your Virtual Host
At this point we are done with your session as www, so you can exit that. In your root session you need to change directory (cd) to the “sites-available” Nginx folder. From here we’ll delete the default Nginx page from the “sites-enabled” directory by using the “rm” command.
Next download a premade WordPress+phpMyAdmin configuration file from my GitHub repo using wget.
Finally, create a symlink for your configuration in “sites-enabled” using the “ln –s” command. When you reload Nginx’s configurations it will stop serving the default page and now start serving your WordPress site you just downloaded.
Here are the commands:
cd /etc/nginx/sites-available rm ../sites-enabled/default wget https://raw.github.com/patrickgarman/LEMP-Setup/master/wordpress ln -s ../sites-available/wordpress ../sites-enabled/wordpress service nginx reload
8. Configure PHP
There is one last configuration change you need to make sure your server runs smoothly. Configure PHP5-FPM to run as the www user you created before.
To do this you will use Nano to edit your PHP5-FPM to run the service as www. When in nano you will need to use your arrow keys to scroll down to around line 53-54 (you can use control+C to find your position).
When you see where you set the user and group, change it from “www-data” to “www”
nano /etc/php5/fpm/pool.d/www.conf

To save your changes, press control+X and follow the prompts.
Restart PHP5-FPM using the command below and you are ready to install WordPress!
service php5-fpm restart
9. Create your User/Database in phpMyAdmin
To install WordPress you need to create a database.
Open up your web browser and browse to http://www.yourdomain.com/phpmyadmin. You should arrive at the phpMyAdmin login screen where you can login as your root MySQL user with the password you created earlier.
Now follow the steps below to create your new user and database.
- Click “Privileges” and click “Add a new User”

- Type in a username and a password, feel free to have one generated by phpMyAdmin then choose “Local” for host, this will lock the user down to only connecting from this server

- Under “Database” choose “Create database with the same name and grant all privileges” then click “Create User”
10. Install WordPress
Browse to http://www.youdomain.com/ and install WordPress as normal. Your database name and username will be the same as the username you just created.

Congratulations! You have installed WordPress!

If I’m not missing something, except for the path /phpmyadmin, your config at https://raw.github.com/patrickgarman/LEMP-Setup/master/wordpress seems to be missing the protection from this exploit: http://forum.nginx.org/read.php?2,88845,88996. That is, unless you set cgi.fix_pathinfo=0 in php.ini.
Probably didn’t push my latest commit into GitHub when I was last editing the config. I’ll double check that and push it up ASAP. Thanks for the catch!
Any considerations when doing this for a multisite install with domain mapping?
;) I’m sure that could be arranged, and it’s on my own list of “how to ideas”
My own site is running off my WP Multisite setup on Ubuntu running Nginx. Everything works, even individual sitemaps!
http://www.patrickgarman.com/sitemap.xml
+1 for a multisite with domainmapping guide :)
Hope to see more of these useful Linux-VPS/Wordpress articles in the future!
By using the default configuration of php-fpm with 128 MB of RAM for each process and pm_start_server at more than 10, a VPS can easily touch its maximum memory. People who read this kind of tutorials are probably new to VPS and can usually go for a starter VPS package that offers only 256 MB of RAM (or 512 MB). So, it is advisable to start php-fpm’s process manager to create static number of processes and let the pm_max_children to 4 or 8 depending on how much memory one is going to set as memory_limit in their php.ini file (or the php_admin_value[memory_limit] value in php-fpm configuration file.
There are plenty of configuration changes that can be made for all aspects of the server, however those will come later. This was primarily aimed at those new to VPS and Nginx, and more to just get them online.
As with WP Multisite, check back regularly for more in depth optimizations. :)
Actually I had this problem last week with a OpenVZ VPS with 256/512 MB. I just forgot to adjust the number of processes. And the trouble is the VPS will get inaccessible if all memory is used. I had a SSH session but the shell couldn’t spawn commands anymore, because there was no memory left.
Hello,
Thank you for a no nonesense guide for installing nginx! Man, seemed so mystical, but you have definately taken that problem away! WOuld love to see how to setup Multisite with alias files on a subdomain setup.
Thanks.
Check back regularly :)
I plan to install Nginx myself hopefully soon enough when our online tool becomes popular. I have bookmarked this article and will use it during the installation.
So what does our tool do?
Http://www.freewpinstaller.com is an online tool that allows a user to install a wordpress website instalntly in less than 3 minutes.
i got 404 error when using pretty url. installed nginx compatibility plugin, but still doesn’t work. so i try add this code that make things works.
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
I understand how to install it on a fresh and unmanaged VPS. But I would like to know if I need to install the PHP again on an existed and managed VPS
Can you please let me know details briefly how to install NGINX on an exited VPS with PHP installed already ?
Thanks !
Great post! One of the most comprehensive tutorial how to install Nginx plus all other necessary software to run WordPress Multisite on VPS. We are considering switching from Apache to Nginx server and as a design studio we don’t have a server administrator in our team. This article helped us to get into the Nginx subject and to ask the right questions when looking for a profi to do the job for us.
Thanks a lot!
Milena Böhm
I DESIGN STUDIO
Hi,
Thanks for the great tutorial .. i was trying to install this on my new VPS … but when i tried to do add-apt-repository ppa:nginx/php5 … i got this error
Error: can’t find signing_key_fingerprint at https://launchpad.net/api/1.0/~nginx/+archive/php5
can you update me on whats wrong … and how to go about from here as i dont have much knowledge about this
thanks and waiting for your reply
Seems like Nginx decided to stop maintaining their own PHP5 ppa. I’ll look today to see what a good replacement ppa would be and update
https://answers.launchpad.net/nginx/+question/189867
Hi,
Dude were you able to get anything for that issue? Or is there any other way you can guide me on doing that …
What version of Ubuntu are you running?
i’m using 10.04 .. 32Bit version
hi dude .. any update regarding the repository .. i have to re-install everything on the new server .. so if you can reply fast and guide in doing it will be really great .. waiting for your response ..
is it a same process when a website is already running ? and has lots of database already? do I need to disable the plugins first before doing this?
I get the following errors in my log files: unix:/tmp/php-cgi.socket failed (2: No such file or directory). How can I fix this?
Where can i find the wordpress directory such as wp-content, etc. so I can upload some files onto it ? The whereis the find command did not work for some reason.
Scratch that. Now I have another problem. After moving my old apache WordPress onto this new server. All my posts did not work. I’m guessing this is a permalink issue with .htaccess ? Since nginx does not work with .htacess… Any thoughts ?
Installing Nginx was easy for the servers , thanks .
Patrick,
I am trying to follow your instructions which have been very helpful except I think your premade WordPress PHP configuration file seems to be unavailable, can you advise me where else I can find this file?