Installing Nginx and PHP5-FPM

PHP
This article is a continuation of a previous article that outlined basic server setup of a Linode VPS running Ubuntu 14.04. If you missed it, please read the previous article. This article assumes you have a Linode VPS already setup and running with some common packages already installed so I won’t go into any of that stuff.

So we have a basic server configured now we need to install the most important aspect, the web server itself. I greatly prefer Nginx to Apache because of it’s much lower memory footprint / requirements. In the past I have used spawn-fcgi to run php as fastcgi to serve pages. I’ve since abandoned it for php5-fpm which is much simpler to get running and a lot less manual labor. If you prefer spawn-fcgi you can refer to my old blog at rubynginx.com for that guide.

Install the Nginx Web Server

First things first lets install the web server.

In Ubuntu 14.04 Nginx will automatically start upon installation. You can test it now to see if it’s running by inputting the domain or ipaddress of the server into your web browser. If for some reason you don’t have a domain or don’t know your ip address you can quickly get it by typing this on the command line:

Another option is to curl one of the various ip address websites like so:

You should see a Welcome to Nginx web page in your browser if everything went according to plan.

Install any databases you want to use

The most common databases would be either MySQL or PostgreSQL, install both of them if you like or only the ones you want to use

Now run a few configuration commands to complete the process

You’ll be asked to enter the mysql root password that you specified during installation.

After this process is finished MySQL should be ready to go.

Install PHP5

Now we need to install PHP to complete our web stack. Since Nginx does not have native PHP support we will need to run PHP as fastcgi via php5-fpm, which stands for “fastcgi process manager”. We will tell Nginx to pass PHP requests to this software for processing.

Now you need to configure the php processor.

We are looking for a line “cgi.fix_pathinfo” this line should be commented out with a semi-colon and set to 1. We need to uncomment this and edit it to 0 like so:

While I am in this file I also like to turn short_tags on. What this does is allows you to simply use the following code

Instead of the longer method of

It’s not required but some older PHP software may have been designed using short tags in which case you may need to turn them back on. Look for this line and make sure it’s set as “On”

Save and close the file then restart php5-fpm

Memcached

Lets tweak the pool setting on memcached to make it more efficient.

Now you want to find the line that begins with -m and modify it from 64 to 128

Save the file and close it.

Enable Gzip Compression

Basic Gzip compression should be on by default but we want to uncomment a bunch of lines in the config to fully enable it.

You are looking for a big block of settings that starts with “gzip on;” we want to uncomment all the lines to match below:

Save the file and that is finished.

Enable Mcrypt in PHP5-FPM

In recent versions of ubuntu mcrypt is broken out of the box as they changed the path to the mcrypt.ini fix it by running the following

Now restart nginx

Folder Structure and Nginx Virtual Hosts

Website folder structure

Now that we have all the packages installed we need to set up our folder structure that will house all of our domains. This is a pretty subjective topic and feel free to modify it to your liking. I’ll just supply an example folder setup that you can use if you so desire.

Under each domain folder I place four difference folders, this is the intended purpose of the different folders

  • public
    The web root for the production domain, all web accessible production files will go here.
  • dev
    The web root for the development version of the website. You can setup a sub domain such as dev. to point here.
  • private
    You should put any items that you do not want to be web accessible here. You can still access them through PHP to utilize in your web applications. Files here could be shared between production and dev, or in the case of ExtJS the main app code can be housed here.
  • logs
    The nginx error and access logs should be placed here.
  • backup
    If you want to run any sort of backups for a specific domain you can place the backup archives here.

First make the domains folder in your home directory

Then you can create the folder structure for each domain with a one liner pretty easily like this

Then you should make the logs folder writable

Nginx Virtual Hosts

Each domain that you want Ngnix to host will need it’s own virtual host file. These are all located at “/etc/nginx/sites-available/“. When you installed Nginx it automatically created a “default” virtual host. You can either delete this virtual host or use it for your primary domain. There are a few changes you’ll need to make to it first.

You will need to change the “root” line to point to the domain’s path for the site you wish to serve

There is a line that lists the various index file types you’ll need to add index.php like so

You need to modify the server_name line to match your desired domain name

Below the server_name line add these two lines to setup the logging to our proper folders

If you are going to be using wordpress pretty urls on the domain you will need to add this block below the log lines

The final block you will need to add below the previous one is what sends the php traffic to php-fpm. Some form of this should already exist by default but commented out. Simply uncomment the required lines.

Now for each new domain you wish to add you’ll need to add a new file to the sites-available folder. Here’s a full sample virtual host configuration.

NOTE I made the primary website www. in this example and the top server block redirects all traffic to the regular domain to www. This is not required, you can remove this if you wish and change the server_name in the primary block to remove the www.

After you save this file you will need to link it to the sites-enabled directory and restart nginx

The website should now be live. If you want to test it to make sure php is working you can create a new file in the domains public folder and output the results of phpinfo().

Now paste in the following code and save the file:

Now if you visit your domain at http://<domain>/info.php you should see the phpinfo page. Thats it for now. have fun!

Posted in Server Admin Tagged with: , , , , , , , , , , , , , , , , , , , , , , , , , , ,
0 comments on “Installing Nginx and PHP5-FPM
1 Pings/Trackbacks for "Installing Nginx and PHP5-FPM"
  1. […] If you don’t need python and ruby and just need to install php5 + nginx you can view my newer updated guide for Ubuntu 14.04 at my new blog by clicking here. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*