Before displaying a dynamic website with the NginX web server, you need to install and configure PHP5-FPM. It’s easy when you know exactly what to do and not so easy when you don’t. Fortunately, there are a lot of web pages available when you search for them and unfortunately, a lot of those pages aren’t correct.
It isn’t enough to install PHP5-FPM only. You need to install supporting packages as well.
Installing PHP5 and PHP5-FPM
If you haven’t done so already, use apt-get install python-software-properties so you can easily add other sources to the repository list (insert sudo before the commands if you’re not logged in as root). After that, use these commands to add another source for PHP5 and PHP5-FPM (and then install the packages) because the official Ubuntu repository is usually outdated:
apt-get install php5-common php5-mysql php5-xmlrpc php5-cgi php5-curl php5-gd php5-cli php5-fpm php-apc php5-dev php5-mcrypt php5-tidy
Other packages may come up to be installed along with all of these and you should go ahead and allow them. After the installation is complete, you should use apt-get dist-upgrade to make sure all the packages on your server are up-to-date.
Configuring PHP5 and PHP5-FPM
Some web pages you’ve probably already seen will tell you to edit specific “php.ini” files. It’s a good idea to edit all of them. This is where they are and you can use nano to edit them:
In my NginX configuration article, I set client_max_body_size 20m. In each php.ini file, you need to set upload_max_filesize = 20M and post_max_size = 20M to match it. 20 MB is a good size to start with because most files uploaded using PHP routines are usually smaller.
You should set cgi.fix_pathinfo=0 in each php.ini file as well. Your scripts will use the script file name instead of the translated path. While you’re editing each file, make sure the date.timezone is set to your time zone or UTC because the later versions of PHP5 require it to be set in the php.ini file (where with older versions, it only needed to be set in the scripts using date functions).
The file that needs to be edited for PHP5-FPM is /etc/php5/fpm/pool.d/www.conf. In that file, make sure the listen directive is listen = /var/run/php5-fpm.sock, the same socket as the upstream server in the NginX configuration file. The other lines to check are the lines that begin with pm, the first one being pm = dynamic. You’ll want to adjust the numbers to make your website fast while not using too many resources.
The fastcgi_params File
This file at /etc/nginx/fastcgi_params needs only one line edited. Change the parameter for the SCRIPT_FILENAME to $document_root$fastcgi_script_name; and save it. The reason I didn’t mention this with the NginX configuration is because PHP5-FPM must be installed before the parameters mean anything.
As with the article I wrote about the NginX configuration options, I didn’t go through and explain every detail about this installation and the configuration directives. It’s best to become familiar with all of this at the online source: php.net. This is one application where the online documentation is always kept up-to-date.