Ubuntu Server Quick Setup: Nginx, PHP5-fpm, and MySQL5

I choose to host websites with Nginx over Apache. It all started when I was originally using Apache, and was in the process of creating a Python script that converts data, the Python script did not run on the webserver, it ran from my local machine. However, it did make upwards of 10-20 requests per second.

I was finding that, my average single request to the API was taking upwards of 2 seconds. That doesn't sound long, but when you need to process 200,000 records, 2seconds can become a lifetime over the total session.

I started investigating why Apache, or possibly PHP, was so slow. I came across this article posted by Nginx themselves, it details how many times Apache scans directories every time a request is made.

In a nutshell, Apache scans every directory in the web root folder looking for .htaccess files. So for every connection I opened, it would scan all folders for .htaccess, every time. If you have 6 folders in root, then it scans all 6 folders. This is a functionality of Apache, because it allows per-folder overrides using .htaccess files, it has to scan for them.

Nginx works differently. It stores it's folder options in the main configuration file, so when Nginx recieves a connection it only opens that one configuration file.

Installation

Ensure your system is up to date:

sudo apt-get update && sudo apt-get upgrade

In one fell swoop we will install Nginx, MySQL, PHP5, and PHP's connector to MySQL.

sudo apt-get install nginx mysql-server php5-fpm php5-mysql

Configuration

I personally host my websites out of a folder named wwwroot in my home folder. This requires PHP to have read/write access to my home folder.

The following instructions assume a few things.

  1. Where ever you see "username", replace it with your login name.
  2. Your webroot for your website, is in a folder /home/username/wwwroot folder.
  3. I wrote this article 2013-07-23, the line numbers maybe off by a bit as things can change in the future, ensure the lines match with what I'm telling you to change.

PHP5 FPM

I prefer to not edit the default files, make a copy of www.config to your own username.conf

sudo cp /etc/php5/fpm/pool.d/www.conf /etc/php5/fpm/pool.d/username.conf

Use your favorite editor, I typically use nano

sudo nano /etc/php5/fpm/pool.d/www.conf

Configure our copied configuration file

line 4: [www] to [username]

Whom do we want PHP to write as, if we're writing to our home folder, it needs to be our username, same with the group option.

L22: "user = www-data" change to "user = username"
L23: "group = www-data" change to "group = username"

We're configuring PHP-FPM and Nginx to connect through Sockets rather than TCP, it's slightly faster.

L33: "listen = 127.0.0.1:9000" change to "listen = /var/run/php5-fpm-username.sock"

If you're using nano:
CTRL + X to exit.
Y to say Yes to save.

sudo service php5-fpm restart

Nginx

I prefer to not edit the default files, make a copy of default to your own username

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/username

Nginx stores configuration files in the sites-available folder. But the actual sites that are enabled and running are located in sites-enabled.
This is setup using a symlink. Because we don't want the default configuration to be running, we're going to remove the symlink.

sudo rm /etc/nginx/sites-enabled/default

Now lets add a symlink to our own custom file into the sites-enabled folder.
FYI, you MUST supply the full path to both locations for a symlink. Trying to use shortcuts such as cd'ing into the folder and using a relative path, does not work as intended.

sudo ln -s /etc/nginx/sites-available/username /etc/nginx/sites-enabled/username

Let's edit that confguration file

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

Point the base directory we will be serving from, to our home folder.

line 24: "root /usr/share/nginx/www;" change to "/home/username/wwwroot;"

Add index.php to the index system.

line 25: "index index.html index.htm;" change to "index index.php index.html index.htm;"

Because our default file is typically going to be index.php, we'll change the fallback parameter to use index.php instead of .html

line 33: within "location / { " section, change "try_files $uri $uri/ index.html;" to "try_files $uri $uri/ index.php;"

PHP is actually handled by another location flag farther down the configuration file.
Uncomment the following lines:

location ~ \.php$ {
   fastcgi_split_path_info ^(.+\.php)(/.+)$;
   fastcgi_pass unix:/var/run/php5-fpm-username.sock;
   fastcgi_index index.php;
   include fastcgi_params;
}

If you're using Nano:
CTRL + X to exit.
Y for Yes to Save.

sudo service nginx restart

In my next posting. I will be going over, domains, and subdomains. In the near future as well, hidden URL's and phpMyAdmin not being in the root folder.

Print