Ubuntu PHP Installation

Let’s go through to setup PHP on Ubuntu

DOWNLOAD AND INSTALL PHP

In your CLI window, enter the following command:

sudo apt-get install php-fpm php-mysql

EDIT NGINX TO USE PHP

From the CLI window, enter the following command:

sudo nano /etc/nginx/sites-available/default
  • First, we need to add index.php as the first value of our index directive so that files named index.php are served, if available, when a directory is requested.
  • We can modify the server_name directive to point to our server’s domain name or public IP address.
  • For the actual PHP processing, we just need to uncomment a segment of the file that handles PHP requests by removing the pound symbols (#) from in front of each line. This will be the location ~\.php$ location block, the included fastcgi-php.conf snippet, and the socket associated with php-fpm.
  • We will also uncomment the location block dealing with .htaccess files using the same method. Nginx doesn’t process these files. If any of these files happen to find their way into the document root, they should not be served to visitors.

 

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name server_domain_or_IP;

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

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

    location ~ /\.ht {
        deny all;
    }
}

TEST NGINX CONFIGURATION

In your CLI window, enter the following command:

sudo nginx -t
sudo service nginx restart

This allows us to test the NGINX Configuration. If no errors, proceed.

CREATE TEST PHP FILE

sudo nano /var/www/html/info.php

Then Paste the following:

<?php
phpinfo();

Save and Exit…

Then goto browser and enter:

http://server_domain_or_IP/info.php

If all is well, it will display a PHP info page.

CONTINUE TO NGINX SECURING DOMAIN WITH SSL

Now that NGINX is installed and is now working with PHP, let’s secure your domain with SSL.

SSL