Nginx with php-fpm (on Debian)

Nginx installation

sudo apt-get install nginx

Basic server commands

sudo service nginx start
sudo service nginx stop
sudo service nginx restart

Configurations

The main configuration file located in /etc/nginx/nginx.conf

And, keep all the virtual host files inside the sites-available folder located at /etc/nginx/. Should make symlink with sites-enabled folder.

ln -s /etc/nginx/sites-available/test  /etc/nginx/sites-enabled/test 

where test is the virtual host config file.

Sample virtual host with PHP supports:

server {
    listen 80;
    server_name 192.168.18.129;
    rewrite_log on;
    #access_log /var/log/nginx/local.access.log;
    #error_log /var/log/nginx/local.error.log;

    root /var/www;

    gzip on;
    gzip_min_length 1000;

    location / {
autoindex on;
        index index.php index.html;
    }
 
    # different location directive
    location /laravel {
        alias /var/www/laravel/public;
        index index.php;

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
    }

    # prevent access to hidden files
    location ~ /\. {
        access_log      off;
        log_not_found   off;
        deny            all;
    }

    # do not log assets
    location ~* \.(jpg|jpeg|gif|png|css|js|ico)$ {
        access_log      off;
        log_not_found   off;
        expires         360d;
    }

    location ~ \.php$ {
        #fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
}

NOTE: Keep in mind to create virtual host file on sites-available and symlink with sites-enabled

ln -s /etc/nginx/sites-available/FILE /etc/nginx/sites-enabled/FILE

We can test the nginx configuration file status before start the server by,

sudo nginx -t

Reload nginx: nginx -s reload

Install php-fpm (php 5.3)

apt-get install php5-fpm

Install php-fpm (php 5.5)

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php5

Update apt-cache...

sudo apt-get update

sudo apt-get install php5-common php5-mysqlnd php5-xmlrpc php5-curl php5-gd php5-cli 

php5-fpm php-pear php5-dev php5-imap php5-mcrypt

Check php version

php -v

General tips

# Check how many CPU processors in the system: cat /proc/cpuinfo | grep processor

# Create a load balancing: change nginx.conf file as bellow,

upstream_web_backend {
   #ip_hash;
  server xx.xx.xx.xx;
server yy.yy.yy.yy;
}

server {

listen 80;

location / {

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://web_backend;
}
}

Comments

Popular posts from this blog

Kubernetes for Micro Services

Laravel 5.3

Node-RED