Setting up nginx + php-fpm on Debian/Ubuntu

Nginx is an HTTP server. Compared to apache, it is more resilient and capable of handling a larger number of connections. It is mainly used on production servers, although it is rarely configured for local development, even though it is no more difficult than configuring apache + php.

But there are still differences. Let's start with the fact that php can work with nginx in fastCGI mode, while with apache, it can work both in fastCGI mode and as an Apache module. Additionally, you will need to separately configure rewrite, basic authentication, etc. (.htaccess - an Apache feature).

But making it work in the simplest case is not difficult =)

So, you need to install Nginx, PHP (if not already installed), and php-fpm:

aptitude install nginx php5 php5-cli php5-fpm

Nginx and php-fpm should be added to the startup and start, but if it didn't happen, then

service nginx start
service php5-fpm start

You can check if they are added to the startup using, for example, rcconf.

(Although neither nginx nor php-fpm is configured yet, you will still need to restart them in the future).

Nginx Configuration

The nginx itself should work with the default configuration

nano /etc/nginx/nginx.conf


user www-data;

determines under which user Nginx will run.

You should also check if the configurations of modules and hosts are included. There should be lines like these (usually at the end):

include             /etc/nginx/conf.d/*.conf;
include             /etc/nginx/sites-enabled/*;

Now you need to create a host configuration:

nano /etc/nginx/sites-available/your_site.conf
server {
    listen   80;
    root /usr/share/nginx/www/default;
    index index.html index.htm;

access_log /var/log/nginx/site.log; error_log /var/log/nginx/site.log;

# Further PHP configuration location ~ .php$ { fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } }

(You can see it in /etc/nginx/sites-available.default)

This way, you configured the work with php-fpm using a socket.

Now, you need to enable the configuration:

ln -s /etc/nginx/sites-available/your_site.conf /etc/nginx/sites-enabled/your_site.conf


service nginx reload

And voila, everything should work - you have configured the nginx server. P.S. Don't forget to adjust /etc/hosts if necessary.