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.

sudo apt-get install nginx-full

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:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

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

curl http://icanhazip.com

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

sudo apt-get install postgresql postgresql-client php5-pgsql libpq-dev
sudo apt-get install mysql-server mysql-client libmysqlclient-dev php5-mysql libmysqld-dev

Now run a few configuration commands to complete the process

sudo mysql_install_db
sudo mysql_secure_installation

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.

sudo apt-get install php5-fpm php5-mysql memcached php5-gd php-pear php-apc php5-cli php5-common php5-curl php5-mcrypt php5-cgi php5-memcached

Now you need to configure the php processor.

sudo nano /etc/php5/fpm/php.ini

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:

cgi.fix_pathinfo=0

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”

short_open_tag = On

Save and close the file then restart php5-fpm

sudo service php5-fpm restart

Memcached

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

sudo nano /etc/memcached.conf

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

-m 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.

sudo nano /etc/nginx/nginx.conf

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

gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

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

sudo ln -s /etc/php5/mods-available/mcrypt.ini /etc/php5/fpm/conf.d/mcrypt.ini
sudo php5enmod mcrypt
sudo service php5-fpm restart

Now restart nginx
sudo service nginx restart

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.

/home
/myuser
/domains
/domain1.com
/public
/dev
/private
/logs
/backup

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

cd ~
mkdir domains

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

mkdir -p domains//{public,private,dev,logs,backup}

Then you should make the logs folder writable

sudo chmod 777 ~/domains//logs

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.

sudo nano /etc/nginx/sites-available/default

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

root /home//domains//public;

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

index index.php index.html index.htm;

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

server_name ;

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

access_log /home//domains//logs/access.log;
error_log /home//domains//logs/error.log;

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

location / {
# this sends all non-existing file or directory requests to index.php
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}

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.

location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;

fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

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.

server {
listen 80;
server_name ;
return 301 $scheme://www.$request_uri;
}

server {
listen 80;

root /home//domains//public;
index index.php index.html index.htm;

server_name www.;

access_log /home//domains//logs/access.log;
error_log /home//domains//logs/error.log;

location / {
# this sends all non-existing file or directory requests to index.php
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;

fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}
}

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

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
sudo service nginx restart

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().

nano ~/domains//public/info.php

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 *

*