Blog

install nginx and php-fpm on ubuntu, nginx, php, php-fpm, ubuntu 14.04, ubuntu 16.04, ubuntu 18.04, ubuntu 20.04, linux, server, aws, lemp, lamp, web server

Install Nginx and PHP-FPM on Ubuntu

How to install Nginx and PHP-FPM on Ubuntu? How to install LEMP on ubuntu system? Here’s the article which will help you install Nginx and PHP-FPM or LEMP in less than 2 minutes.
We will be using PHP7.2 FPM version in this post and will see Nginx rules to connect to PHP-FPM through Socket

Let’s start with Nginx installation, Run following commands on Terminal

apt update
apt install nginx #it will install latest nginx version

Similarly, Let’s Install PHP7.2-FPM now, Run following commands on Terminal

apt install -y php7.2-fpm 

Above commands will install php7.2-cli and some other important PHP extensions as well.

You can check the installation of both by checking status of their services

service nginx status
service php7.2-fpm status
install nginx and php-fpm on ubuntu, nginx, php, php-fpm, ubuntu 14.04, ubuntu 16.04, ubuntu 18.04, ubuntu 20.04, linux, server, aws, lemp, lamp, web server
service nginx status
install nginx and php-fpm on ubuntu, nginx, php, php-fpm, ubuntu 14.04, ubuntu 16.04, ubuntu 18.04, ubuntu 20.04, linux, server, aws, lemp, lamp, web server
service php status

Nginx Configuration files reside at /etc/nginx folder
We’ll create our website’s Nginx configuration file there

vi /etc/nginx/sites-available/example.servername.com.conf
server {
    listen       80;
    server_name  example.servername.com;  #can contain IP as well
    root /usr/share/nginx/html/;
    index index.php index.html;
   
    location ~* \favicon.ico$ {
        expires 6d;
    }

    location ~* \.(eot|otf|ttf|woff|woff2)$ {
        expires 6d;
    }

    location ~ \.css {
        add_header  Content-Type    text/css;
        expires 6d;
    }

    location ~* \.js {
        add_header  Content-Type    application/x-javascript;
        expires 6d;
    }
    location ~* \.png|jpeg|jpg {
        add_header  Content-Type    image/png;
        add_header  Content-Type    image/jpeg;
        add_header  Content-Type    image/jpg;
        expires 6d;
    }
    location ~* \.svg {
        add_header  Content-Type    image/svg+xml;
        expires 6d;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

    #location block to integrate PHP-FPM with NGINX

    location ~* \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        try_files $uri =404;
        fastcgi_param  REQUEST_URI      $request_uri;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    }
}

Notes: In the above configuration file, We have added the “expires” variable in each location block, which will suggest the caching time of that object once served on the browser.
We have also added, Content-type header for files that will be added in headers.
Finally, we have integrated PHP-FPM configuration, so every file with extension .php will be served with that block.

For more variants of NGINX configuration example, visit this article link

Save your changes and now we will create link of this configuration file

cd /etc/nginx/sites-enabled && ln -s /etc/nginx/sites-available/example.servername.com.conf example.servername.com.conf

Now, restart Nginx for this changes to get effect.

service nginx restart

If you want to add any additional package from PHP as an eg. MySQL extension to connect to MySQL database, run following command

apt install php7.2-mysql

Now, restart PHP-FPM after that

service php7.2-fpm restart

Let’s Create phpinfo file to check this setup

cd /usr/share/nginx/html && vi info.php
<?php
phpinfo();
?>

Save this file and Go to Browser and hit your IP/Domain

URL/info.php

install nginx and php-fpm on ubuntu, nginx, php, php-fpm, ubuntu 14.04, ubuntu 16.04, ubuntu 18.04, ubuntu 20.04, linux, server, aws, lemp, lamp, web server
url/info.php

Note: Code Directory is /usr/share/nginx/html

Read out other blog on NGINX here

Do Reach out in the comment section if you find this content useful.

Drafted On,
May 1, 20120
DevOps
identicalCloud.com

Nginx FAQ

How do I run nginx on ubuntu?

1. update your package manager
apt-get update
2. Install latest Nginx version
apt-get install nginx
3. Check Nginx status using
service nginx status
4. start Nginx service if status is stopped
service nginx start
5. Change Nginx configuration as needed

Summary
Install Nginx and PHP-FPM on Ubuntu
Article Name
Install Nginx and PHP-FPM on Ubuntu
Description
How to Install Nginx and PHP-FPM on Ubuntu? How to install LEMP on ubuntu system. Here's the article which will help you install Nginx and PHP-FPM or LEMP in less than 2 minutes
Author
Publisher Name
IdenticalCloud

Leave a Comment