Install Nginx on Ubuntu Server with PHP-FPM
In this tutorial we will learn How to Install Nginx on Ubuntu Server 16.04. We will also install the PHP programming language to serve dynamic content from our Ubuntu Nginx web server.
Install Nginx using apt get command
First, Update the apt source list, Then install nginx on Ubuntu with apt-get command.
Open the Ubuntu Terminal and Type:
sudo apt-get update
sudo apt-get install nginx
Run systemctl command to make sure that nginx is properly installed and running.
systemctl status nginx
To check the nginx version we installed, Type:
nginx -v
Finally Try to Access the Nginx Web Server using a Web Browser. Open your Web Browser and type, IP Address or domain name of your Ubuntu Server.
You should see the default nginx welcome page as above image shows.
Install PHP FPM Package
To server PHP websites with nginx, we need to install the PHP_FPM(FastCGI Process Manager) on our Ubuntu Server.
sudo apt-get install php7.0-fpm
Configure PHP-FPM and Nginx
First, open the /etc/php/7.0/fpm/php.ini file, find the line ;cgi.fix_pathinfo=1. Then, uncomment the line(Remove semicolon) and change the value to 0.
cgi.fix_pathinfo=0
Next, we need to Configure Nginx to use the PHP processor. Open the configuration file for default site.
vim /etc/nginx/sites-available/default
Find the line start with “index” and add index.php as first option.
index index.php index.html index.htm index.nginx-debian.html;
Then, add the following location directive.
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
The Configuration file should be similar to as shown below.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
Save the configuration file, then restart php7.0-fpm and nginx service using systemctl command.
systemctl restart php7.0-fpm.service
systemctl restart nginx.service
To test the php configuration, Create an index.php file inside the Documentroot.
vim /var/www/html/index.php
Add the phpinfo() function.
<?php phpinfo(); ?>
Then, open the web browser and view the index.php file. In the address bar Type:
http://ip-address-of-server/index.php
You should see the web page containing information about the PHP installed on your Ubuntu Server.
So, that is how we install nginx on Ubuntu Server 16.04 with php-fpm process. In the next tutorial we will learn how to setup virtual hosts in Ubuntu nginx Web Server to serve multiple websites.