How to set up PHP with Nginx, APC and PostgreSQL?

17,682

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;
  }
}
Share:
17,682
Jonas
Author by

Jonas

Updated on September 17, 2022

Comments

  • Jonas
    Jonas over 1 year

    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;
        }
    
    }
    
    • Jorge Castro
      Jorge Castro over 13 years
      Please make this an answer and then accept it, that way we can vote on both of them together as the combined solution.
    • Jonas
      Jonas over 13 years
      @Jorge: This is not an answer, I'm just showing my configuration file that doesn't work. Peter Smit posted the correct solution combined with comments and I have accepted it as an answer.
    • Jorge Castro
      Jorge Castro over 13 years
      Ah my mistake, rock on!
  • Jonas
    Jonas over 13 years
    I have done this now, and I created a simple test.php file, but it doesn't work. My browser says "broken link" and in Nginx error log there is only an entry about the "favicon.ico". I guess there is something with my configuration file, but I have really no idea and it's pretty hard to find the error.
  • Peter Smit
    Peter Smit over 13 years
    @Jonas It could be that nginx and php5-fpm are not started yet. In that case, sudo service nginx start and sudo service php5-fpm start should do the job. Off course, look out for any error they might give.
  • Jonas
    Jonas over 13 years
    I have updated the question with my configuration file for Nginx, there seems to be something wrong with the PHP setup. After adding your lines about favicon to my configuration file I no longer gets any errors in the Nginx error log. Nginx works with .html files but it doesn't work for .php files.
  • Peter Smit
    Peter Smit over 13 years
    @Jonas, it sounds that the php5-fpm is not running. Can you check with nmap localhost whether it is listening on port 9000? You can btw remove the location / setting and put the root command and index command straight in your server config. Also, you don't need fastcgi_index.
  • Peter Smit
    Peter Smit over 13 years
    @Jonas A page that helped me to make 'clean' configuration files is wiki.nginx.org/Pitfalls , but don't ignore the warning about implementing changes that you do not understand.