So, you want to build a WordPress server? One that won’t barf all over itself every time a link is posted to Digg or Reddit? Well, you’ve come to the right place. Building an insanely fast web server isn’t rocket science. It’s actually pretty darn straight forward. I’ve built more web servers than I care to remember, which means I’ve also made more mistakes I care to remember. All in the name of trying to make things faster. If speed is your goal, there’s only a few important key things to remember. If speed is absolutely the most critical variable in a situation, my philosophy is something like this:
- KISS – Keep It Simple Stupid. Seriously… No control panels, plugins, or other bloated garbage.
- Cache, Cache, and Cache some more. Caching is a godsend. Reduces requests, reduces transfer time, therefore reducing load times!
- FAST Server – This should be a given, but no shared servers. Only VPS, VM or bare metal awesomeness.
- Optimization – Optimize, minimize, downsize, and compress everything as much as you can get away with.
- Optionally, using a CDN (Content Delivery Network) can really speed things up; as well as a high quality distributed DNS service. But, not absolutely necessary.
Like I said, we’re talking about building an INSANELY FAST WordPress server here. Obviously if you’re Joe Shmoe from Motown, you can probably get by just fine on a quality managed hosting provider, like Serenity-Networks. But, if you want to build the fastest web server on earth, continue reading.
There are plenty of Linux Distributions out there that will work just fine. I prefer CentOS and Ubuntu. For this guide, I’ll be using Ubuntu 14.04 LTS (Long Term Support). This OS will be supported for many years, with security updates. That’s good, especially for a server. If you don’t have it, go to http://www.ubuntu.org and go to downloads, then server. Be sure to select the 14.04 LTS version.
Now, assuming you have done a minimal install of Ubuntu 14.04, with only OpenSSH selected for packages (for remote SSH connection, obviously), we will be using some commonly known building blocks to turn this into a very fast web server.
- NGINX Web Server – Many THOUSANDS of the fastest sites in the world use it. It’s the best.
- HHVM – This is a very high performance virtual machine that takes care of processing PHP (as well as HACK). Because it turns PHP into machine language, very quickly, it can really speed things up.
- MySQL – Kind of boring, but every site needs a database, so we’ll use this one.
So yea, pretty straight forward. Let’s get on with it. I assume you are at the console of your Ubuntu 14.04 server or you have SSH’d into it. Let’s make sure everything is updated and upgraded.
# sudo apt-get update # sudo apt-get upgrade
Now, lets get a few basics installed that we’ll need for various things.
# sudo apt-get install -y unzip vim git-core curl wget build-essential
By the way, the -y tells apt to just answer yes to everything, because you know you’re going to anyways. It makes things quicker and easier.
Next, we need to install NGINX. This is actually very easy on Ubuntu. There is already a package for it, so we can just tell aptitude to do all the work for you.
# sudo apt-get install -y nginx
So….Yea, that’s pretty uneventful. But, NGINX is now installed. If you browse to the IP address of your server, from a web browser, you should see this.
Now we need to install HHVM. First, let’s make sure the proper apt-repository is installed.
# sudo apt-get install software-properties-common # sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0x5a16e7281be7a449 # sudo add-apt-repository 'deb http://dl.hhvm.com/ubuntu trusty main' # sudo apt-get update
After that’s finished, we can install HHVM. Since there is also an HHVM package, we’ll tell aptitude to install that for us as well. Make sure you ran the last sudo apt-get update, its important.
# sudo apt-get install hhvm
When it’s finished installing, you will see this.
This is pretty handy because it tells us exactly what to do next. First, we’ll install FastCGI and NGINX will restart.
# sudo /usr/share/hhvm/install_fastcgi.sh
We obviously want HHVM to start at boot, so lets go ahead and make that happen.
# sudo update-rc.d hhvm defaults
Let’s go ahead and restart HHVM.
# sudo service hhvm restart
We should now have the ability to execute PHP code from NGINX. Let’s make sure by creating a file with a basic php test.
# cd /usr/share/nginx/html # sudo nano test.php
Enter the following into test.php, then save and exit.
<?php echo "hello world\n";
Now, browse to the the IP address of your server and pull up the test.php file. Open a browser and enter http://xx.xx.xx.xx/test.php
You should see “hello world.” This means NGINX is executing PHP code properly. Now, because we don’t have the PHP CLI (command line interface) installed, a cron job or anything else that would call up PHP from the command line would not work. However, there is a fix for this. One of those commands listed when the HHVM installation was finished solves this problem. Go ahead and run that command.
# sudo /usr/bin/update-alternatives --install /usr/bin/php php /usr/bin/hhvm 60
Let’s make sure PHP can be called up from the command line now by running this command.
# php -v
If all is well, you will see this.
HHVM and NGINX are basically ready to go out of the gate and very little configuration is needed. Keep in mind that the NGINX public html directory is the directory we put the test.php file in earlier.
The last component of our insanely fast server is the MySQL database server. So, lets go ahead and install it.
# sudo apt-get install mysql-server
You will be prompted to enter a password when you see the screen below. Choose a secure password. You will need this to configure WordPress.
Now, we need to download WordPress. First we’ll switch to our NGINX public HTML directory, then we’ll download and unzip WordPress.
# cd /usr/share/nginx/html # sudo wget http://wordpress.org/latest.zip # sudo unzip latest.zip
Now, we will move the contents of the extracted WordPress folder into the root of the public HTML directory, and cleanup everything left behind.
# sudo mv wordpress/* . # sudo rmdir wordpress # sudo rm latest.zip # sudo rm index.html
Next, we need to create a database for the WordPress site. To do that, we need to login to the MySQL CLI.
mysql -u root -p
Enter the MySQL root password you created at the pink screen earlier. You should now be at a mysql> prompt. We are going to add a user, set that user’s password, and give it permissions. After that we’ll refresh everything and exit.
mysql> create database databasename; mysql> create user [email protected]; mysql> set password for [email protected] = password("yourpassword"); mysql> grant all privileges on wp.* to [email protected] identified by 'yourpassword'; mysql> flush privileges; mysql> quit
Now you have created a database, a user for that database, and given that user permissions to do just about anything to that database. Next, we will create a wordpress configuration file, using the included template. Then, were going to open it so we can edit the database connection info.
# sudo cp wp-config-sample.php wp-config.php # sudo nano wp-config.php
There are only 3 variables you need to change. DB_NAME, DB_USER, and DB_PASSWORD. These need to match the database name, mysql username and password you specified just a minute ago.
After you’ve made the appropriate changes, save and exit. (CTRL>X, then Y, then Enter)
The last thing we need to take care of is the NGINX configuration. In particular, we need to tell NGINX to look for index.php, instead of index.html, and we need to change how files are loaded. To do that, we need to edit the main NGINX configuration file, which is located at /etc/nginx/sites-available/default.
# sudo nano /etc/nginx/sites-available/default
Locate this portion of the config file. It’s right at the top. It is in this area we will make both changes.
You want to add index.php so it looks like this.
Then you will change the location line to this.
try_files $uri $uri/ /index.php?q=$uri&$args;
So, it should look like this.
Once those changes are made, save and exit. (CTRL>X, then Y, then enter)
All we need to do now is restart NGINX.
# sudo service nginx restart
After you’ve restarted NGINX, you’re ready to open up your new WordPress site and go through the final configuration! Don’t worry, the rest will be done in a web browser. Simply open up the IP of your server in your browser via http://xx.xx.xx.xx/ and you should see the WordPress installation page. It looks like this.
Just fill in all the info and click install. You will then see this page.
Now, you can click Log In and you’ll be redirected to the wp-login admin page! Go ahead and log in to make sure everything is working correctly. If so, you should see this.
You’re all done now. If you run into any problems, feel free to post in the comments and I’ll give you a hand. Thanks!