9

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;
    }

}
3
  • Please make this an answer and then accept it, that way we can vote on both of them together as the combined solution. Dec 8, 2010 at 17:44
  • @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.
    – Jonas
    Dec 8, 2010 at 17:48
  • Ah my mistake, rock on! Dec 8, 2010 at 18:11

1 Answer 1

8

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;
  }
}
7
  • 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.
    – Jonas
    Oct 25, 2010 at 17:08
  • @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.
    – Peter Smit
    Oct 25, 2010 at 17:56
  • @Jonas Your error log should not contain favicon.ico, at least not if you copied the empty_gif part from my solution. Also, don't forget to restart always after change (the sudo service nginx reload). If all else fails, maybe you could add your configuration file to your question.
    – Peter Smit
    Oct 25, 2010 at 18:17
  • 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.
    – Jonas
    Oct 26, 2010 at 0:00
  • 1
    @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.
    – Peter Smit
    Oct 26, 2010 at 12:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .