Ubuntu – How to set up PHP with Nginx, APC and PostgreSQL

aptinstallationnginxPHPserver

I use Ubuntu Server 10.10 and I would like to set up a web server environment with NginX, PHP 5.3.3, PostgreSQL and preferably APC and PHP Suhosin.

I have already set up PostgreSQL with apt-get install postgresql and Nginx with apt-get install nginx.

But how do I set up PHP for these? Can I do this using apt-get install or do I have to download the sources and compile it? I would prefer to do it using apt-get.

I would likte to use PHP-FPM for Nginx. Most of the tutorials I have found on Internet are old and compile the PHP, but this is not recommended for production servers.

How do I easiest set up PHP with Nginx, APC and PostgreSQL? or at least PHP-FPM + Nginx?


UPDATE

I have now installed a fresh Ubuntu Server 10.10 and executed the command Peter suggested with php5-suhosin added. After that Nginx works fine, then I edit the generated confiugration file to be as below. After reloading the new config file, Nginx still works fine using a index.html file, but when I add a index.php file it stop to work. I guess that this has to do with PHP-FPM, the APC or something PHP-related. But it could be the configuration file for PHP-FPM as well.

Here is the configuration file for Nginx that I'm using, most of it is generated by default. I have skipped comments.

server {

    listen 80;
    listen [::]:80 default ipv6only=on;

    server_name localhost;

    access_log /var/log/nginx/localhost.access.log;

    location /favicon.ico {
        empty_gif;
    }

    location / {
        root     /var/www;
        index    index.php index.html index.htm;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

}

Best Answer

Starting from Ubuntu 10.10 this is trivial with the new php5-fpm package

The following packages do everything you need

  • nginx - the webserver
  • php5-fpm - Fast-CGI php server
  • php-apc - The APC package for php
  • php5-pgsql - PostgreSQL module for PHP
  • postgresql - The PostgreSQL database server

All together sudo apt-get install nginx php5-fpm php-apc php5-pgsql postgresql

Also I suggest to check whether apache2 is installed. If so, delete it with an sudo apt-get remove apache2 to avoid apache and nginx competing for port 80.

Note also that xdebug standard also wants to use port 9000, just like php5-fpm. So if you use xdebug, change that port for example to 9001

And as bonus an example nginx configuration (place it in /etc/nginx/sites-available and symlink it into /etc/nginx/sites-enabled)

server {
  listen 80;
  server_name site.com;
  access_log /data/log/www/site.com/access.log;
  error_log /data/log/www/site.com/error.log;

  root /data/www_data/site.com/public;
  index index.php;

  location = /favicon.ico {
    empty_gif;
    #return 204;
  }

  location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass 127.0.0.1:9000;
  }
}
Related Question