Configure PHP for Nginx on a Ubuntu VM within Azure

In the next step, on our way to run WordPress with Ngnix on Azure with Ubuntu, we have to set up PHP.

To start, just turn on your Ubuntu VM from Step 1, login and follow the steps below.


  1. Install and Setup Nginx on Azure with a Ubuntu VM
  2. PHP with Configuration
  3. MySQL
  4. Setup WordPress
  5. Configure Nginx and WordPress

Please be aware that the steps I describe in this article may cost you money. Please see the Azure pricing page first.

Install PHP

First we want to check for the newest updates and install them, so our system is up to date.

$ sudo apt-get update

Update your Ubuntu VM on Azure

$ sudo apt-get upgrade -y

The upgrade process can take a little while, so there might be room to crab a fresh coffee at this point.

After the coffee break we want to install PHP.

$ sudo apt-get install php5-common php5-cli php5-fpm -y

That is basically it, we are finished with installing PHP, now we want to configure Nginx.

Configure Nginx

For this demo we will just change the default settings of Nginx to serve PHP. For this, we open the Nginx config with an editor of our choice, I will use nano here.

$ sudo nano /etc/nginx/sites-available/default

Configure Nginx and PHP on your Ubuntu VM on Azure
Now we follow the instructions in the default config and add index.php to the list (inside the server section). It should look like this snippet:

# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

Next we have to uncomment a few lines, which will look like this then:

# pass the PHP scripts to FastCGI server listening on
location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php5-cgi alone:
        # fastcgi_pass;
        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;

Hit CTRL + X, then Y and ENTER. After we saved the file, just restart the services:

$ sudo service php5-fpm restart
$ sudo service nginx restart

Test your configuration

To test our configuration, we want to add a simple PHP file to our web directory. In the config above you can find a root setting:

root /var/www/html;

In that folder we want to add a new index.php file:

$ sudo touch /var/www/html/index.php

Open the file in an editor and paste a simple PHP command:

$ sudo nano /var/www/html/index.php

Save the file, like done before. Now you should see something like in the screenshot below, when you open a browser and navigate to your hostname +
Running Nginx and PHP on your Ubuntu VM on Azure

You may also like...