Nginx, php-fpm and MySQL under Ubuntu/Debian

Due to the popularity of the original guide on my old site, I thought it may be worth going over this topic again. Throughout this guide you will need to enter commands via terminal/cli, as such I will assume that you have root user (or sudo) permissions.

Since the last guide that I wrote, the Ubuntu repos have been updated to include PHP5.3. This is great news as PHP-FPM is bundled with this package.

Optional: If you are using an old version of Ubuntu (older than 11.04) or Debian (older than 6) then I would recommend adding the .dotdeb repositories to Aptitude. Now, please bear in mind that I have encountered problems with their repository, in particular version dependency when upgrading MySQL, if you’re on a recent version of Ubuntu/Debian you don’t need this. You can find instructions on their website, alternatively you can simply bash the following into terminal/cli:

echo -e "deb http://php53.dotdeb.org oldstable all \n deb-src http://php53.dotdeb.org oldstable all"
> /etc/apt/sources.list.d/dotdeb.list

Followed by:

wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | sudo apt-key add -

Now to reload the aptitude’s repository cache:

apt-get update

The .dotdeb repository has miscellaneous packages used for other apps too, so we should ensure everything is up to date.

apt-get upgrade

And we’re done, now on to the rest of the guide.

Nginx

To start off with we’re going to install Nginx:

apt-get install nginx

Fairly simple, right? Well yeah, all we need to do now is to tell configure Nginx to your environment:

nano /etc/nginx/sites-available/default

In all honesty, the majority of this file is untidy and isn’t strictly needed. Unfortunately explaining all the options myself would take forever, as such take a look at Nginx’s docs if you need more help.

Personally I would suggest deleting the contents of this file and replacing with my nice template:

server {
        server_name motomotomd.co.uk; # Change to your domain name
        root /home/matt/sites/moto/htdocs; # Change to the location of your site
        index index.html index.htm index.php; # Feel free to add any file extensions

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
        }

        #Pass .php files off to php-fpm
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

Now to restart Nginx to load the changes we’ve made:

service nginx restart

Your pages should load now, unless they’re php obviously, as we haven’t installed it yet.

MySQL

Nothing too difficult to do here:
apt-get install mysql-server phpmyadmin

(phpmyadmin isn’t required, but I use it all the time, feel free to remove it).

I normally use phpmyadmin to create my users because I’m lazy. You will need to log in the first time using root user details.

And erm… that’s it.

PHP

Nothing too difficult here either:

apt-get install php5-fpm

Now I can’t be sure what packages you need, but if you do need additional packages just install them like normal, for example GD Library:

apt-get install php5-gd

php-fpm runs like a service/daemon on your machine and can be controlled as so:

service php5-fpm restart

I have noticed that at times you’d expect for php-fpm to be automatically restarted, such as when installing additional php packages, it doesn’t, so make a note of the above.

That’s it, you should now be ready to go. Obviously this guide isn’t a complete noobie guide and does expect you to know what you’re doing from here. If you need any more info, please feel free to get in touch (using the form above) and I’ll write you a guide or add to this one.