Creating VHost on Nginx using PHP-FPM

Hello everyone, today I’m here to show you how to create a virtual host on Nginx Web Server, using PHP-FPM to processa PHP pages.

Nginx today is one of the most famous Web server available, just behind from the Apache Web Server, it also can be used as reverse Proxy. Nginx is lightweight and easy to configure, today I won’t explain all options, because will be just a quick view of it. In this case I’m using Ubuntu to install and configure the stuffs.

Installing Nginx and PHP-FPM

First of all, let’s install the Nginx. We can do it with the following command:

sudo apt-get install nginx -y

For Red-Hat based systems, you can use YUM:

sudo yum install nginx -y

And now, install the PHP-FPM with the following command:

apt-get install php7.0-fpm -y

And for Red-Hat based:

yum install php-fpm php-common -y

Configuring Nginx VHost

The VHost on Nginx is available under /etc/nginx/sites-available, but only the enable Vhosts are under /etc/nginx/sites-enabled. Here I will show you an example of a VHost configuration file:

server {
    listen 80;
    root /home/server/Web/diario-praga;
    index index.html index.htm index.php;
    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/diario-praga-error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;

    location ~ /\.ht {
        deny all;

Just a brief explanation about the configuration:

  • server – Holds the VHost configuration
  • listen – Which port this VHost is listening
  • server_name – For which names this VHost will answer
  • root – The path of your website or WebApplication
  • index – Which files Nginx should look when acessing the  root path.
  • chartset – I think this is talkative, charset of the server.
  • location – Sets configuration depending on a request URI
    • On our cause we’re using the try_files directive, which checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context.
  • access_log – Turning off the log of access on the server
  • error_log – Defining a custom path for error  logs
  • sendfile – You can check about it here:
  • client_max_body_size – Sets the maximum allowed size of the client request body, specified in the “Content-Length” request header field.

Now, the most important part of the configuration, is the location ~ \.php$, because here we will define the configuration for PHP processing. Just a brief explanation about main the parameters:

  • fastcgi_pass – Sets the address of a FastCGI server. The address can be specified as a domain name or IP address, and a port. On our case we are using Unix socket, which can be faster.
  • fastcgi_index – Sets the index file.
  • include fastcgi_params – Include the Nginx FastCGI params to your VHost
  • fastcgi_param – Add a new param to be passed to PHP, this param will be available on $_SERVER variable.

The other parameters you can check on Nginx website:

And the last location, is to not consider .htaccess file, if have one.

Finishing up

Now, if you created the configuration file under /etc/nginx/sites-available, you need to create a symbolic link into /etc/nginx/sites-enabled to Enable your new Virtual Host. And you can do taht with the following command:

ln -s /etc/nginx/sites-available/my-vhost.conf /etc/nginx/sites-enabled/my-vhost.conf

And restart the Nginx, to apply our changes:

service nginx restart

I think that is it people, if you have any questions, or something else to add, just write us a comment!


Share it!

Leave a Reply

Your email address will not be published. Required fields are marked *