Installing Nginx With PHP 5.3 And PHP-FPM On Ubuntu Lucid Lynx (10.04) Without Compiling Anything

Want to support HowtoForge? Become a subscriber!
 
Submitted by Sypher (Contact Author) (Forums) on Mon, 2010-06-14 18:52. :: Debian | Ubuntu | nginx | PHP

Installing Nginx With PHP 5.3 And PHP-FPM On Ubuntu Lucid Lynx (10.04) Without Compiling Anything

Version 1.0
Follow me on Twitter

Since Apache is most of the time a memory hungy process, people started to look for different ways to host their website. Apache is clearly not the only webserver available. A few good examples are lighttpd and nginx. In this tutorial I will show you how to install it on your Ubuntu server. This tutorial also applies to Debian, though. There is only a very small difference.

Ready? Let's begin shall we.

 

Step 0 - Preliminary Notes

In order to complete this tutorial, I assume you have installed a base system of Ubuntu Lucid (10.04). How this can be done, can be read in different tutorials. This tutorial only focusses on getting nginx+php running without much hassle.

 

Step 1 - Nginx

Installing nginx is the first step we have to do. This can be easily done by downloading it from the repository.

sudo apt-get install nginx

The default vhost has to be changed in order to work properly.

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

A nice starting point for your config is:

server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;

## Default location
location / {
root /var/www;
index index.php;
}

## Images and static content is treated different
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
expires 30d;
root /var/www;
}

## Parse all .php file in the /var/www directory
location ~ .php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}

## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
}
upstream backend {
server 127.0.0.1:9000;
}
 

Ok, we're done here. Now we'll install the needed files for PHP.

 

Step 2 - Installing PHP

Many sites rely on PHP for providing them dynamic content, whether this is a wiki, forum software, weblog or something entirely different.

There is no need to use DotDeb, since PHP5.3 is now officially supported in Ubuntu Lucid (10.04). Its as easy as

sudo apt-get update

The resulting text should include dotdeb.

Now we'll install PHP. In order to install PHP-FPM, we'll have to add the PPA to it since it is not officially supported.

 add-apt-repository ppa:brianmercer/php

sudo apt-get update && sudo apt-get install php5-fpm

If you are planning to use a database or require specific modules (mcrypt, ldap, snmp etc) you can install them as well.

Ok, so now we have nginx and PHP installed and ready to go.

 

Step 3 - Finalizing

Restart nginx in order to catch up with the config changes we made earlier.

sudo service nginx restart

The restart should have gone without any problems.

After installing php5-fpm, it should have been started. If you did change your php.ini files, you have to restart php5-fpm.

sudo service php5-fpm restart

All right. They should now both be running.

Step 4 - Testing

In order to test if the execution of PHP is working, create an index.php file in /var/www with the following content:

<?php phpinfo(); ?>

Visit your webserver and you should be able to see the generated phpinfo. If not, something went wrong.

 

Step 5 - Troubleshooting & Final notes

If  you did not see the phpinfo, there might be something wrong. In order to track down what went wrong, you can check the nginx error log:

sudo tail /var/log/nginx/error.log

Remember, if you did change your php.ini you have to restart php5-fpm. Restarting nginx isn't' necessary.

In my example config I've enabled the fastcgi error interception. If a serious error occurs (for instance a "cannot redeclare class xyz"), nginx can catch this page and show a "nice" error page that something went wrong. This way, there is less information given out in case something is going seriously wrong.

If you do not like this, you can turn it off.

If php5-fpm is not running, your PHP files cannot be parsed and nginx will show the user an error page.

Well, I guess we're done and you are now able to serve PHP with your new nginx based webserver. Nginx is pretty nice and you can configure a lot. If you need rewrites, be aware that nginx does not work with .htaccess files. You will need to change your vhost settings in order for the rewrites to work.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Mon, 2010-12-06 19:42.

Hi! I found the nginx has its own php5 repository in ppa launchpad at https://launchpad.net/~nginx/+archive/php5

Submitted by Andre (not registered) on Thu, 2010-10-28 17:10.
I did everything in this tutorial, still not working. I an error saying "[warn] 873#0: conflicting server name "localhost" on 0.0.0.0:80, ignored" any suggestion on what I'm doing wrong?
Submitted by Jon L. (not registered) on Tue, 2010-09-07 21:54.

I'm curious, why do you use an upstream context to pass out to the fastcgi connection?Passing directly to the fastcgi address works fine as well.

Submitted by yves (not registered) on Sat, 2010-08-21 16:59.

Thank you for the clear instructions.

Please update the site when Nginx includes a complete PHP solution (ie something more automatic).

 

Also, a small thing regarding the start-up scripts, they're both S20

S20nginx
S20php5-fpm

I wondered if php5 shouldn't be started first... I know there shouldn't much time difference, but just because accepted requests need php to be ready (like php needs mysql to be ready first).

 

Submitted by Sypher (registered user) on Tue, 2010-08-31 18:00.

It doesn't really matter. This is out-of-the-box behaviour so its best to just leave it like that.


The only problem you might experience is that nginx is started before php-fpm, which means you'll get a 503 error for a few seconds while FPM starts up.

Submitted by Spaniard (not registered) on Sat, 2010-08-07 10:58.

Good article, and it works (on my ubuntu 10.04 VPS). Thanks for the "ppa:brianmercer" thing.

In near future, I would like to see an updated version of this article but using php 5.3.3 with built-in php-fpm.

PS. The 4 lines after "include fastcgi_params" are unnecessary.

Submitted by Sypher (registered user) on Tue, 2010-08-31 18:01.

Since PHP 5.3.3 hasn't been packaged officially or with the built-in FPM this is kind of hard to do just yet.

 

Thanks for the comment, I've changed it.

Submitted by Olav Frengstad (not registered) on Thu, 2010-07-29 00:39.

 I started with a totaly ripped Ubuntu install and for step #2 you might need to do

sudo apt get install python-software-properties

 to be able to add custom repository.

Submitted by Sypher (registered user) on Tue, 2010-08-31 18:02.
Probably only for Desktop systems I guess?
Submitted by server0owner (not registered) on Sun, 2011-10-23 10:16.
No its not only on desktop versions. I got this package missing on several servers running Ubuntu Server 10.04.3 LTS. (Provider: Rapidspeeds.com)
Submitted by Brian Mercer (not registered) on Thu, 2010-07-15 21:53.

Thanks for the write up.  I went through it with a fresh system and found a couple opportunities for improvement.

First, neither nginx or php5-fpm start automatically upon installation.  Your "sudo /etc/init.d/nginx restart" covers starting nginx and "sudo /etc/init.d/php5-fpm restart" would cover starting php5-fpm if people do it, but you say it isn't necessary.

So please add

sudo service nginx restart

and

sudo service php5-fpm restart

Ubuntu is pushing us towards this way of restarting services which is compatible with the new upstart system, but is backwards compatible to the old init files.

Second, there is no need for php5-cgi or php5-cli or php5-suhosin.  You could get away with only:

sudo apt-get install php5-fpm

since that will install php5-common automatically.  Installing php5-cli may be useful, but is not required and of course php5-suhosin is a security measure and advisable, but not necessary.  php5-cgi is what we're abandoning by using fpm, so not used.

Thanks again.

Submitted by Sypher (registered user) on Tue, 2010-08-31 18:03.
Made the changes you suggested. Thanks for packaging PHP-FPM!
Submitted by Patrick (not registered) on Tue, 2010-11-02 08:37.

Great writeup, thanks. Couple of suggestions:

1) As noted above, a minimal Feisty server install does not include 'python-software-properties' or friends (iso-codes python-apt python-gnupginterface python-support unattended-upgrades), so there's no avoiding this. . .

sudo apt get install python-software-properties

 . . . if you want this. . .

add-apt-repository ppa:brianmercer/php

. . . to work.

2)  Many of these howtos are very unclear about what's required to for a trim php installation and all offer different package combinations. Your howto is very clear, but it might be worth highlighting these points of Brian's in the post itself:

  • php-fpm - required, php5-common automatically added by aptitude
  • php5-cgi replaced by php-fpm, so not used
  • php5-cli an optional client tool
  • php5-suhosin an optional security package
Thanks again - appreciate the post.