Add PHP website in Nginx Server in Ubuntu

To add a PHP website in Nginx Server you need to install php-fpm on your server. Here we are providing step by step guide to add and configure PHP website on nginx server.

Step 1.

If you have not installed php-fpm then install it by following command in your Linux machine.

sudo apt-get update
sudo apt-get install php-fpm

After install php-fpm start it by running following command.

/etc/init.d/php7.0-fpm restart

if your php version is different then change php7.0 to your installed version.


Step 2.

Now create a directory for your website inside /var/www/

sudo mkdir /var/www/your_website

Add permission to php for update files if required. (Require for file writing like upload file and sessions)

sudo chown -Rf www-data.www-data /var/www/your_website


Step 3.

Add your website in Nginx. 

sudo touch /erc/nginx/sites-available/your_domain

Now open this file in edit mode.

sudo nano /erc/nginx/sites-available/your_domain

Add the following configuration in this file.

server {
    	listen 80;
    	listen [::]:80;
    	root /var/www/your_website;
    	index index.php index.html index.htm;
	    location / {
    		if ($request_method = OPTIONS ) {
      			add_header "Access-Control-Allow-Origin"  *;
      			add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
      			add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, 
                Content-Type, Accept";
      			return 200;
            try_files $uri $uri/ /index.php?$query_string;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;

        location ~ /\.ht {
                deny all;

        location ~ /.well-known {
                allow all;

	    location ~ /\.ht {
        	deny all;

Change php version in this line fastcgi_pass unix:/run/php/php7.0-fpm.sock;   to your installed php version. Now save this file by ctrl+o then exit by ctrl+x


Step 4.

Create a symlink in /etc/nginx/sites-enabled to activate your website.

sudo ln -s /etc/nginx/sites-available/your_website /etc/nginx/sites-enabled/


Step 5.

Reload your nginx server.

sudo service nginx reload


All is done.

To test your configuration add a file in your website.

sudo touch /var/www/your_website/index.php

sudo nano /var/www/your_website/index.php

Paste following in an index.php file.

<?php phpinfo(); ?>

Now save by ctrl+o and exit by ctrl+x.

Check your website is showing php information.

If you do not update your DNS to point your domain with your server then update by login to your domain providers websites like Bigrock, Godaddy or anything else. Select your domain and go inside the DNS setting and update A-Record there. Set your server IP Address in A Record or for @ in your domain.