How to Install and set up PHP and Nginx (LEMP) on Ubuntu 20.04

In this guide we are going to install and set up PHP and Nginx in Ubuntu 20.04. We will also set up a virtual host to serve a simple php info page.

PHP is a general-purpose scripting language geared towards web development. It is one of the popular programming languages for the web. Popular tools such as WordPress are coded using php. Big companies like Facebook also uses php heavily.

Nginx is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. It started out as a web server designed for maximum performance and stability. Nginx has grown in popularity since its release due to its light-weight resource utilization and its ability to scale easily on minimal hardware. Nginx excels at serving static content quickly and is designed to pass dynamic requests off to other software that is better suited for those purposes.

# Table of Content

  1. Ensuring that the server is up to date
  2. Installing PHP on Ubuntu 20.04
  3. Installing Nginx on Ubuntu 20.04
  4. Installing php-fpm in Ubuntu 20.04
  5. Creating a simple site and configuring virtual host
  6. Testing the configuration

# 1. Ensuring that the server is up to date

Before proceeding, it is always a good practice to ensure that the server is up to date. Use these commands to achieve this

sudo apt update
sudo apt upgrade -y

# 2. Installing PHP on Ubuntu 20.04

PHP is available in the default Ubuntu 20.04 repos. The latest version of PHP as of writing this blog is PHP 7.4.

Install PHP and some common packages using this command:

sudo apt install -y \
    php \
    php-common \

Check the installed version of PHP

$ apt-cache policy php
  Installed: 2:7.4+75
  Candidate: 2:7.4+75
  Version table:
 *** 2:7.4+75 500
        500 focal/main amd64 Packages
        100 /var/lib/dpkg/status

Check the installed php version using this command:

$ php -v
PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

# 3. Installing Nginx on Ubuntu 20.04

We will use Nginx to serve the PHP content. Nginx, as a stable high-performance web server and with a very low consumption of resources, is the perfect match for PHP-FPM. Nginx has an asynchronous architecture that is much more scalable, based on events.

Nginx is available in the default Ubuntu repos. Install it using this command:

sudo apt install -y nginx

Confirm the installed nginx

$ apt-cache policy nginx
  Installed: 1.18.0-0ubuntu1.2
  Candidate: 1.18.0-0ubuntu1.2
  Version table:
 *** 1.18.0-0ubuntu1.2 500
        500 focal-updates/main amd64 Packages
        500 focal-security/main amd64 Packages
        100 /var/lib/dpkg/status
     1.17.10-0ubuntu1 500
        500 focal/main amd64 Packages

# 4. Installing PHP FPM in Ubuntu 20.04

PHP-FPM (an acronym of FastCGI Process Manager) is a hugely-popular alternative PHP (Hypertext Processor) FastCGI implementation. PHP-FPM is the most popular alternative implementation of PHP FastCGI. It has additional features which are really useful for high-traffic websites. When using Nginx with PHP-FPM, performance at the level of memory consumption is improved.

PHP runs as a separated service when using PHP-FPM. By using this PHP version as language interpreter, requests are processed through a TCP/IP socket; so that the Nginx web server only handles the HTTP requests and PHP-FPM interprets the PHP code. The fact of having two separate services is key for increasing efficiency.

Install php-fpm using this command:

sudo apt install -y php-fpm

The service will be started by default. Check its status using this command:

$ sudo systemctl status php7.4-fpm
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-12-14 20:52:23 UTC; 9s ago
       Docs: man:php-fpm7.4(8)
    Process: 40390 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/po>
   Main PID: 40387 (php-fpm7.4)
     Status: "Ready to handle connections"
      Tasks: 3 (limit: 4631)
     Memory: 6.7M
     CGroup: /system.slice/php7.4-fpm.service
             ├─40387 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─40388 php-fpm: pool www
             └─40389 php-fpm: pool www

Dec 14 20:52:23 ip-10-2-40-248 systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
Dec 14 20:52:23 ip-10-2-40-248 systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

To enable php-fpm on boot, use this command:

sudo systemctl status php7.4-fpm

# 5. Creating a simple site and configuring virtual host

Nginx allows you to serve multiple sites using virtual hosts. The concept of virtual hosts allows more than one Web site on one system or Web server. The servers are differentiated by their host name. Visitors to the Web site are routed by host name or IP address to the correct virtual host. Virtual hosting allows companies sharing one server to each have their own domain names.

In this example, we will use the domain for the virtual host but be free to use the domain of your choice. Please note that the DNS for the domainto be used should already be configured to point to the IP address of the server. Confirm that with:

dig -t A

# Creating a directory and adding sample content

The default page of nginx is configured as a default virtual host serving content from /var/www/html. It’s a best practice to create the directory of your site in the /var/www directory since its a best practice to serve from there.

Lets create a directory for our site with the following command:

sudo mkdir /var/www/

Lets create a simple index.php page to serve from our site. You will require vim installed for this to work, if not use this command:

sudo apt install -y vim

Edit the file:

sudo vim /var/www/

Add content to the file:

<?php phpinfo(); ?>

# Creating virtual host (Server blocks)

An Nginx server blocks  can be used to encapsulate configuration details and host more than one domain on a single server. We are going to create a virtual host for our site (

Nginx virtual hosts configurations are stored in the directory /etc/nginx/conf.d/. Lets create a config for our site:

sudo vim /etc/nginx/conf.d/

Add the following content

server {
    listen 80;
    server_tokens off;
    client_max_body_size 100M;

    access_log /var/log/nginx/;
    error_log  /var/log/nginx/;

    root /var/www/;
    index index.php;

    if ($host !~* ^($) {
        return 444;

    location / {
        proxy_read_timeout 600;
        proxy_connect_timeout 600;
        proxy_send_timeout 600;
        try_files $uri $uri/ /index.php?$args;

    location ~ \.php$ {
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/run/php/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Let us restart the nginx and php-fpm service for the configuration to apply

sudo systemctl restart php7.4-fpm
sudo systemctl restart nginx

# 6. Testing the configuration

To test the configuration, open your browser and load the domain you set up. In my case it is You should see the php info page.

If you haven’t yet configured your DNS settings or simply do not want the site to go live yet, you can modify your computer’s hosts file. To do this, use the following command in your local computer’s CLI

sudo vim /etc/hosts

Then, add the IP address of your actual server followed by the domain name you are configuring, for example:

# Virtual Hosts Local Test

Now you should be able to save the file and access it from within a web browser.

# Conclusion

Nginx virtual hosts or server blocks are a great way to add additional websites to the same origin server. The number of configuration possibilities for a given site are nearly endless when you start modifying the virtual host configuration file to suit your the specific needs of your site.

In this guide we learnt how to set up a virtual host to serve php content in Ubuntu 20.04.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy