How to Setup Your Own Nginx Powered WordPress Server
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.
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.
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.
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 on this list just hit OK.
To get from the selection list to OK hit the tab key on your keyboard.
Our best pro WP tools in one bundle
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 set up 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.
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 set up 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”
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!