Performing functionality and efficient code writing is one of the best and most enjoyable parts about writing code. It doesn’t matter if your code is the fastest one ever; your server needs to be running to its maximum performance for your site to deliver as fast as it can. Nginx and HHVM are a big help here. Set them up and your website can perform faster than you would have imagined, even faster than most servers answer a request.
Speed Up Your WordPress Site with Ngnix + HHVM Server :
HHVM (HipHop Virtual Machine) :
HHVM is designed to execute programs that are written in Hackand PHP. This virtual machine uses (JIT) Just in time algorithmic compilation approach to achieve far better performance as well as maintaining the flexibility for development that PHP provides.
This should also be noted that HHVM is not a 100% compatible with all the PHP language because if you’re working with complex and out of bound functions then you may run into a few problems. In all of my practice and working with WordPress I never came across such a thing but it is something that should not be neglected.
We will be setting up Nginx and HHVM fastcgi from the very start by the help of this article. After that we will be installing and setting up WordPress. If you keep up and follow this article you can have a super-fast server setup in an hours’ time.
This article is written for 12.04LTS and 14.04LTS versions of Ubuntu. The steps that are mentioned in this article will also work on other versions and with some additional touch-ups and changes they will work on any and all systems. I for one prefer Ubuntu and this article is written while going through on a Ubuntu LTS installation.
I’m going to perform these steps on a Digital Ocean VPS droplet. If you want, you can use any VPS server or Dedicated Server of your choice.
We will need to install everything so we will start with a brand new installation. The first thing we need to do is update all the built in software. After updating the built in software, we will install some useful small tools we will want later on.
$ sudo apt-get update $ sudo apt-get install -y unzip vim git-core curl wget build-essential
We’re using the ‘-y’ flag for ‘apt-get-install’. This flag will tell apt that we want ‘Yes’ to be the answer for every question asked whether we want a package to be installed or not. We need to be sure that we install all the packages from trusted sources that are already added in Ubuntu because we are running as root (sudo).
After all that is sorted out, we shift our attention to installing Nginx. It is possible to run HHVM with apache but that is not what we want. Our goal is to speed up and optimize the performance. For that purpose we will use Nginx. Nginx has three major functionalities; it’s a reverse proxy server as well as a normally functional web server. Other than that it also provides us with a good load balancing and caching.
HHVM and Nginx are created for solely one purpose. That purpose is to fasten and speed up everything that out web server does to make the final HTML pages that WordPress generates.
If you have a new Ubuntu Installation then install Nginx on it is as easy as pie. All we do is, we tell ‘apt’ where Nginx can be found and afterwards we can just do ‘apt-get-install’ like we did in a previous step.
$ sudo add-apt-repository -y ppa:nginx/mainline
After this is done, we want to get the Nginx package we told ‘apt’ about. We should be ready to install Nginx.
$ sudo apt-get install -y Nginx
And Nginx has now been installed on your system. Now we have to configure it but we’ll come to that in just a moment.
We’re about ready to install HHVM.
Up till now everything is working out just fine. The lengthy part might be that where the server digests all the packages for the ‘apt-get-update’ every time.
When we have installed HHVM we get this message:
This is quite helpful because it helps us to do our next steps.
Firstly what we’ll do is that we will run the first of the commands they tell us to follow and that is to restart Nginx and then install FastCGI by using HHVM.
$ sudo /usr/share/hhvm/install_fastcgi.sh
Now we want to run HHVM after every boot. So we give Ubuntu the following command:
$ sudo update-rc.d hhvm defaults
We will restart HHVM just for the sake of betterment by:
$ sudo service hhvm restart
Now our HHVM is running. On every startup our server makes HHVM will automatically start to run. To verify that HHVM is installed we will make a test PHP file and see if HHVM can properly run it.
At this stage we can start configuration for our website. But before that can be done there is one more command that was given to us by the HHVM installation results screen which was to have HHVM “act like” PHP over the command line.
$ sudo /usr/bin/update-alternatives --install /usr/bin/php php /usr/bin/hhvm 60
This comes In real handy because this lets us use PHP command line tools like PHPUnit, WPCLI, Composer and other such similar things seeming just as if we were running PPHP and not HHVM.
To give it a quick test, do this:
$ php –v
You should see something like this:
The best thing about HHVM and Nginx is that we don’t need to configure it a lot to start working. We haven’t really done anything but it is still up and running. The default location where our files are supposed to be is “/usr/share/nginx/html”. Notice that the html file is already there. Making an alias is quite helpful. By using an alias one can get to the exact location without punching in the address.
$ echo "www="cd /usr/share/nginx/html"" >> ~/.bashrc
Now an alias is created. We need not to nothing more than just type “www” and go to our file directory.
Using WordPress requires us to install MySQL and not just PHP files. This part is a bit tricky because this part has more configurations than we have done before altogether. When we run the following line of code we will get a screen. This screen lets us set our root password and is known as the password screen:
$ sudo apt-get install mysql-server
So now we’re finally ready to install WordPress. Nginx will be set up simultaneously to work with it.
We will go to our Nginx HTML folder and select a copy of WordPress. We need to unzip this copy of WordPress now.
To make things a bit cleaner we need to do this as well:
Now to set up the MySQL database;
$ mysql -u root -pYOURPASSWORD
Listen to what I’m about to say now. And keep in mind the concept of Inception. We’re inside the MySQL prompt which is inside our SSH prompt which is inside our terminal program.
Write down the following steps as they’ll be needed when configure wp-config.php later on.
- Create a data base
- Add a user
- Set a password for that user
- Give that user permissions
- Refresh MySQL privileges
- Exit the MySQL prompt
Now we need to create wp-config.php. We’re going to use Vim to edit the file but you could have used Nano on the server as well. Alternatively you could SFTP in and edit the file.
$ cp wp-config-sample.php wp-config.php $ vi wp-config.php
If you want to type For Vi you need to press “I” to go in insert mode. When you want to save and quit, just press the “Esc” button and then “:wq”. Don’t worry, I know it’s a bit confusing:
Now at this stage we are going to set up Nginx so that it may run WordPress. We’ll be editing the etc/nginx/sites-available/default.
$ sudo vi /etc/nginx/sites-available/default
We need to edit two things in the configuration:
We need to change Nginx. That way Nginx will also look for PHP files.
Now we want to change how our files are loaded.
Now all that is left to do is restart Nginx and then to start the WordPress installation.
$ sudo service nginx restart
You’re all set up. You should be able to visit your website through your browser. You have successfully installed WordPress Nginx configuration with HHVM on Ubuntu.