Install nginx and php-fpm on Ubuntu/Debian

Written in 2014. Package versions and paths have changed since; the article stays online as an archive.

Recently I was curious about switching from apache2 to nginx and gather some experience. So I setup a fresh Ubuntu 12.04 LTS 64-Bit virtual machine and after I got everything running I decided to share my knowledge with you.

First of all make sure to update your installation.

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

Now install nginx via apt-get and start it. PHP will be compiled from source later on because it is absolutely outdated (5.3.10) in 12.04.

sudo apt-get install -y nginx
sudo service nginx start

Before PHP can be compiled, the following packages have to be installed.

sudo apt-get install -y autoconf libxml2 libxml2-dev libcurl3 libcurl4-gnutls-dev libmagic-dev libcurl4-openssl-dev

You can now go on and use whatever directory you want, but I am used to /tmp for compiling things from source. So let’s switch the directory.

cd /tmp

Compile PHP

Update: As Michael Stucki mentioned in the comments, compiling software is not the only solution for getting the latest version running on an outdated or LTS system. For Debian/Ubuntu there are at least two additional package sources, dotdeb and ondrej, which enable installing php 5.5 using apt-get. If you want to compile php anyway just go on with the next paragraph, else skip the whole section.

Download the latest source and unpack it. Today (February 15, 2014) the latest available version of PHP is 5.5.9. If you are not quite sure if this still is the latest version, please check and adapt yourself.

wget -O php-5.5.9.tar.gz http://de1.php.net/get/php-5.5.9.tar.gz/from/this/mirror
tar xfz php-5.5.9.tar.gz
cd php-5.5.9

It’s time to configure the build. As this article will be relevant for another one about running TYPO3 CMS/NEOS with nginx, some libraries have to be installed first.

sudo apt-get install -y libvpx-dev libjpeg-dev libpng12-dev libxpm-dev libfreetype6-dev

Let’s configure the build. Please do not simply copy/paste! Have a look on what is configured how and what’s actually done. Please notice I do not use opcache here.

./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --with-config-file-path=/etc \
    --without-pear \
    --enable-fpm \
    --with-fpm-user=www-data \
    --with-fpm-group=www-data \
    --disable-opcache \
    --enable-mbstring \
    --enable-mbregex \
    --with-mysqli \
    --with-openssl \
    --with-curl \
    --with-zlib \
    --with-pdo-mysql \
    --enable-zip \
    --with-gd \
    --with-jpeg-dir=/usr \
    --with-png-dir=/usr \
    --enable-soap \
    --with-freetype-dir=/usr/lib \
    --with-xpm-dir=/usr \
    --with-vpx-dir=/usr

Time to use make. If you already have built php and want to build it again, make sure to use make clean first!

make
make test # Recommended, but not necessary
sudo make install

Grats! You just compiled and installed php. Assuming the binary is installed in /usr/bin and the PATH variable is set correctly, you should already be able to use php on the command line.

php -v
# PHP 5.5.9 (cli) (built: Feb 15 2014 18:43:48)
# Copyright (c) 1997-2014 The PHP Group
# Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies

Let’s install php-fpm as a service.

sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
sudo chmod 755 /etc/init.d/php-fpm
sudo update-rc.d php-fpm defaults

Optionally check its status.

service php-fpm status
# php-fpm is stopped

Configure PHP

As php has been compiled with --with-config-file-path=/etc, create a php.ini in /etc and adjust the configuration according to your needs. It might be a good move to copy php.ini-production from the php source.

sudo cp /tmp/php-5.5.9/php.ini-production /etc/php.ini`

Copy the default php-fpm configuration.

sudo cp /etc/php-fpm.conf.default /etc/php-fpm.conf`

The configuration file has to be adjusted as follows.

Afterwards, create the missing log directory.

sudo mkdir -p /var/log/php-fpm`

You are ready to start the service.

service php-fpm start
# Starting php-fpm done

Create an nginx vhost and connect it to php-fpm

The installation might have been a little boring, but now the fun part begins. If you followed the instructions properly you now have both nginx and php-fpm running as a service. Time to create a vhost. (I assume you are using vim)

sudo vim /etc/nginx/sites-available/vhost

It’s content:

server {
    listen 80;
    server_name hostname; # Replace hostname

    root /var/www/root; # Replace root
    index index.php;

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

Symlink the vhost to /etc/nginx/sites-enabled.

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

Create /var/www/root/index.php, update the content as follows and reload nginx.

<?php

phpinfo();

sudo service nginx reload

You should now be able to access your machine using a browser and as a result you should be shown a nicely rendered phpinfo.

It doesn’t work?

Well, I’m feeling sorry for you, but I can’t help you. Please go back and have a look if you missed anything. If you are really sure you didn’t, feel free to leave a reply. But please, do not ever make me responsible for your failure. Thanks!

← All posts