Webfaction - Compiling & Installing A Custom Nginx Instance

Sometimes you need to compile your own versions of software and at first it can seem a little daunting and confusing. The truth is that sometimes is it, but fortunately compiling Nginx on webfaction’s servers is quite straightforward.

Download The Latest Nginx Source

The first thing you need to do is download the nginx source and extract it to a local directory. Personally I like to keep all of my source code in ~/src/ folder so I can recompile at a later point in time. At the time of writing the most recent stable version of nginx was 1.2.1. Make sure that if you’re reading this in the future that you check that you have the most recent version.

Configure The Source

Now you’re ready to configure your installation and this means that you have to run the configure script with a collection of arguments and there’s no getting away from the fact that the compile for nginx can take a lot. Because we’re going to be installing nginx on a shared server we have to provide a custom values for a lot of the available arguments so that our copy of nginx knows to install itself into our space on the server –

Your Path

You need to add the /home/you/local/sbin directory to your path so that we can type nginx and have our shell be able to reach our nginx install. To do this you need to edit your ~/.bashrc file and modify your path as follows –

export PATH=/home/you/local/sbin:$PATH

When you’ve done that you’ll need to reload your bash environment –

source ~/.bashrc

Now if you do an echo of your $PATH environmental variable you’ll notice that your custom nginx install path is included in the output

The Control Panel

You have to do two things in the webfaction control panel to enabled all incoming requests to be handled by your custom nginx install.

Create A Custom Application

You now have to login to the control panel and create a custom application listening on a port. This is because the main apache instance (running on port 80), will proxy all incoming traffic to your custom nginx instance, but before it can do that, you need to create a port for your nginx instance to listen on.

After you press the “add application” button a new screen will load that will show you the port number that has been assigned to you. In the below example you can see that my app is in port 20799.

Whatever your port is, make a note of it because you’ll need it very soon.

Point Your Domain At Your Nginx Instance

If you go to the domains/websites > domains part of the control panel and click through to the edit screen you’ll see something like the below screenshot. However, in the below example I’ve already added the nginx application and mounted it on the root of the URL. You should do the same.

So now that you’ve created your custom app and configured your application to your domain, you can move onto the config file.

The Configuration File

Before creating the new config file you should copy the original config file to a backup — just to be safe.

mv ~/local/etc/nginx.conf ~/local/etc/nginx.conf.backup

Now that the old config file is safe, I like to replace the old file with a brand new file because I’m a fan of revolution over evolution. The config file I use is very barebones –

If you get any errors when testing the config double check that you’ve created all of the directories from the Compile & Install section above. If you don’t get any errors then you should be able to start nginx –

nginx

Now open up a web browser and crack in your domain. You should be greeted with something like this –

What’s next?

Now you’re free to start creating virtualhosts. With the config that I added above I’m able to place all of my virtual hosts in ~/local/etc/nginx/mydomain.conf and have that location automatically included whenever I reload or restart nginx.

Bonus: Starting, Stopping & Reloading.

If you’re needing to know how to start, stop or reload your custom nginx install then here’s how to do each –