Install Nginx using PHP-FPM on Ubuntu 20.04

The
You can download this article in PDF format to support us through the following link.

Download the guide in PDF format

turn off
The

The
The

The purpose of creating this guide is to help users running Ubuntu 20.04 server to install Nginx web server and configure PHP-FPM (FastCGI process manager) Nginx is a free high-performance web server. Nginx is designed to improve speed and scalability, with reverse proxy function and load balancing function for many backend servers using HTTP, TCP and UDP protocols. The website is supported by WordPress and Nginx, and the performance is very good. Compared with Apache, Nginx has a smaller memory footprint and can handle the same number of concurrent connections.

Nginx features

  • Content caching – Cache static and dynamic content
  • Load balancing – Use URIs, cookies, args, etc. for HTTP, TCP, and UDP load balancing of layer 7 request routing.
  • Reverse proxy Multiple protocols: HTTP, gRPC, memcached, PHP‑FPM, SCGI, uwsgi
  • Handle hundreds of thousands of customers at the same time
  • Streaming HTTP video: FLV, HDS, HLS, MP4
  • HTTP / 2 gateway with HTTP / 2 server push support
  • Dual stack RSA / ECC SSL / TLS offload
  • Monitoring plugin: AppDynamics, Datadog, Dynatrace plugin

Step 1: Update Ubuntu

Before you start, you should have a running Ubuntu server that has been updated and upgraded to the latest available packages.

sudo apt update
sudo apt upgrade

Step 2: Install Nginx on Ubuntu 20.04 Linux

After updating the system, continue to install the Nginx package on Ubuntu 20.04 Linux:

sudo apt install nginx

The service should start automatically after installation.

$ systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2020-05-09 19:38:43 UTC; 39s ago
       Docs: man:nginx(8)
   Main PID: 6449 (nginx)
      Tasks: 2 (limit: 2344)
     Memory: 3.8M
     CGroup: /system.slice/nginx.service
             ├─6449 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─6451 nginx: worker process

May 09 19:38:43 ubuntu20 systemd[1]: Starting A high performance web server and a reverse proxy server...
May 09 19:38:43 ubuntu20 systemd[1]: Started A high performance web server and a reverse proxy server.

Please note that you cannot run both Apache and Nginx on the same port. You need to disable the Apache web server or change one of the ports to a non-http standard port.

sudo systemctl disable --now apache2

The UFW firewall can be configured to allow port 80:

sudo ufw allow proto tcp from any to any port 80,443

Step 3: Install PHP-FPM on Ubuntu 20.04

If you plan to use PHP with Nginx, please consider installing the PHP-FPM package.

sudo apt update
sudo apt install php php-cli php-fpm php-json php-pdo php-mysql php-zip php-gd  php-mbstring php-curl php-xml php-pear php-bcmath

PHP-FPM has services that should be run.

$ systemctl status php7.4-fpm.service 
● 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 Sat 2020-05-09 19:50:53 UTC; 2min 26s ago
       Docs: man:php-fpm7.4(8)
   Main PID: 22141 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2344)
     Memory: 9.3M
     CGroup: /system.slice/php7.4-fpm.service
             ├─22141 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─22142 php-fpm: pool www
             └─22143 php-fpm: pool www

May 09 19:50:53 ubuntu20 systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
May 09 19:50:53 ubuntu20 systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

PID and socket files are located in the directory:

$ ls /run/php/
php-fpm.sock  php7.4-fpm.pid  php7.4-fpm.sock

Step 4: Configure PHP-FPM with Nginx on Ubuntu

Edit your Application Nginx configuration file and set fastcgi_pass Partially loaded via FPM socket. See the code snippet below.

$ cat /etc/nginx/php_fastcgi.conf 
# 404
try_files $fastcgi_script_name =404;

# default fastcgi_params
include fastcgi_params;

# fastcgi settings
fastcgi_pass			unix:/run/php/php7.4-fpm.sock;
fastcgi_index			index.php;
fastcgi_buffers			8 16k;
fastcgi_buffer_size		32k;
fastcgi_hide_header             X-Powered-By;
fastcgi_hide_header             X-CF-Powered-By;

Reload Nginx and open your application on the network to confirm that it works. You have successfully installed the Apache web server on an Ubuntu 20.04 Linux computer.

Similar guidelines:

Install Apache Web Server on Ubuntu

Nginx reading books

Nginx HTTP Server-Fourth Edition: Leveraging the power of Nginx to fully utilize your infrastructure and provide faster page services than ever

Practical Apache, PHP-FPM and Nginx reverse proxy: how to build a secure, fast and powerful web server from scratch (Practical Guide Series, Volume 3)Install Nginx using PHP-FPM on Ubuntu 20.04

The
The

The
You can download this article in PDF format to support us through the following link.

Download the guide in PDF format

turn off
The

The
The

Sidebar