Adding PHP to Nginx Virtual Host Configuration

Author: Al-mamun Sarkar Date: 2020-04-14 08:56:47

Adding PHP to Nginx Virtual Host Configuration. The following codes show how to add PHP to an Nginx Virtual host configuration file.

 

Edit artofcse.local.conf:

vim /etc/nginx/conf.d/artofcse.local.conf

 

Add the following code to artofcse.local.conf for PHP:

location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_intercept_errors on;
}

 

Now, artofcse.local.conf will be as follows:

server {
        listen 80 default_server;
        server_name artofcse.local www.artofcse.local;
        index index.html index.htm index.php;
        root /var/www/artofcse.local;

        access_log /var/log/nginx/artofcse.local.access.log;
        error_log /var/log/nginx/artofcse.local.error.log;

        location / {
                try_files $uri $uri/ =404;
        }

        location /images {
                autoindex on;
                access_log /var/log/nginx/artofcse.local.images.access.log;
                error_log /var/log/nginx/artofcse.local.images.error.log;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                fastcgi_intercept_errors on;
        }

        error_page 404 /404.html;
        location = /404.html {
                internal;
        }

}

 

Create a PHP file inside artofcse.com folder:

vim /var/www/artofcse.com/info.php

 

Add the following PHP code to the file:

phpinfo();

Add PHP starting tag at the starting of the file

 Test Nginx configurations:

nginx -t

If it's ok then restart Nginx:

systemctl reload nginx

 

Now browse http://127.0.0.1/info.php or Your IP address or Domain /info.php