Installing Nginx on Ubuntu

October 26, 2014 By 0 Comments

Before We Begin

Ok, so before we begin the install, lets cover a couple of the basics: Nginx is an open source webserver and alternative to the popular Apache server. There are some differences between the two; of notable interest would be .htaccess files. Because these aren't supported in Nginx, you'll need to be a little more involved in website configuration. Once you've got the configs down though, you'll find it to be a more resource-friendly beast. It is also assumed that you have already configured your firewall to allow the relevant ports to be forwarded to your Ubuntu server as required, and that you aren't running another web server on the same machine.

Updating Ubuntu

It is always useful to start in an up to date environment. In this case, I'm using a new t2.Micro instance on Amazon Web Services, and the first thing I want to do is make sure it's up to date:

Installing Nginx

Now that Ubuntu is updated, we can focus on the first part of our server. We're going to first install Nginx, then we'll set it up to use PHP and MySQL as well. If you just want to server basic HTML files without the PHP support, this first step will be sufficient for you.

At this point, if you connect to your server, you'll get the default Nginx landing page:

Nginx Default Page

If you can see a page like the one above, congrats! You have a working web server. At this point, you can either jump straight to configuring static sites, or you can continue to setup the server to handle PHP and MySQL driven sites.

Setting Up MySQL

MySQL is going to be our database of choice, and is another popular open source solution with a lot of support online. First we want to install it in the usual manner:

The above installs the MySQL Server, which will automatically start the setup process. You should make sure you choose a strong password for your MySQL root user. Ideally, it will be a password that you don't use elsewhere. I like to use LastPass to generate passwords that are approx 50 characters for this sort of thing, using all character types. Once MySQL is installed, we need to have it create its system management database:

Next, MySQL comes with a script to secure the installation and remove some test-specific items that you won't need in a production setup:

This process will first prompt you for the root password. If you didn't set one a moment ago, just hit enter. You get the chance to change your root password first,  so if you didn't set one before, you should do so now. Read each step it prompts you with, and answer accordingly. The safest option is to answer with "Yes" to each. You can hit "y", or just hit enter for this; it's the default option. This turns off remote login for root, and removes anonymous users. It also cleans up and removes a test database that comes with MySQL by default. We'll set up users for each site when we get round to setting up websites, so for now, MySQL setup is complete. 🙂

Setting Up PHP

We're almost there now! We just need to install and configure PHP and we're good to go!

You'll notice we aren't installing the php5 package, but rather php5-fpm above. The fpm package is the FastCGI Process Manager for PHP, and will be how we connect nginx to it. As you might expect, php5-mysql is the adapter that allows PHP to communicate with the MySQL database. You may wish to change some of PHP's settings at this point. I tend to make a few tweaks to items such as the max file upload size, and maximum memory per script.

If you made any changes, you'll neeed to restart PHP too apply them:

Great! That's PHP installed, but now we need to allow our sites to use it. For better or worse, this is controlled on a site-by-site basis in the nginx site configs. I prefer it this way, as I can give PHP only to the domains that need it for security.

Adding PHP to a Website

There are a couple of ways to add PHP support to a site now that it's setup. You can either add the configs directly to each site, or (and my preference) you can create a common file and and reference that from all the configs that require it. The first step is to create a directory called common that'll hold these config snippets. Note: Some users like to call this directory "global". Either is fine, just don't use both! I prefer using "common" because not all sites may use these snippets. Global implies all will be affected.

Next, we'll need to add a file to provide the PHP support:

And into that file, you'll want to add this location block:

Now would be a good time to setup our default site with PHP. To do this, simply modify the deafult file in sites-available.

The file will have a good number of comments explaining the various sections of it, but ultimately you'll want to replace it with this:

To apply the changes, simply reload the nginx configs:

 Testing It All Works

Finally, we're at the last step, all we need to do now is make sure everything's running as expected. To do this, we'll add a phpinfo file to the default site, and load it in the browser:

And as you might expect, all this file contains is the phpinfo function:

Once saved, just try accessing it. http://Your-Server-Address/phpinfo.php You should get a screen like this: phpinfo   Once you can see that, then you know PHP is working! We can remove the file again for security:

We'll cover setting up individual sites and users in another post, but for now, you've successfully completed setting up nginx with PHP and MySQL. Take some time to go get a cup of tea while you celebrate. 🙂