This guide has been created to help users running Ubuntu 20.04 server to install Nginx web server and configure PHP-FPM (FastCGI Process Manager). Nginx is a high performance web server that’s free to use. Nginx is designed for speed and scalability with capabilities of reverse proxy and load balancing to a number of backend servers both with HTTP, TCP and UDP protocols. This website is powered by WordPress and Nginx and the performance is really good. Nginx has small memory footprint as compared to Apache, handling same number of concurrent connections.

Features of Nginx

  • Content Cache – Cache static and dynamic content
  • Load Balancing – HTTP, TCP, and UDP load balancing with Layer 7 request routing using URI, cookie, args, and more.
  • Reverse proxy multiple protocols: HTTP, gRPC, memcached, PHP‑FPM, SCGI, uwsgi
  • Handle hundreds of thousands of clients simultaneously
  • Stream HTTP video: FLV, HDS, HLS, MP4
  • HTTP/2 gateway with HTTP/2 server push support
  • Dual‑stack RSA/ECC SSL/TLS offload
  • Monitoring plugins: AppDynamics, Datadog, Dynatrace plug‑ins

Step 1: Update Ubuntu

Before you begin, 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 the system is updated, proceed to install Nginx package on Ubuntu 20.04 Linux:

sudo apt install nginx

The service should be started 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.

Notice that you cannot run both Apache and Nginx on same port. You’ll need to disable Apache web server or change port of one of them to not http standard port.

sudo systemctl disable --now apache2

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’re planning on using PHP with Nginx, consider installing 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 the service that should be running.

$ 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 file 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 section to load through FPM socket. See below snippet.

$ 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 web to confirm it is working as expected. You have successfully installed Apache Web server on Ubuntu 20.04 Linux machine.

Similar guides:

Install Apache Web Server on Ubuntu

Nginx Books to read

Nginx HTTP Server – Fourth Edition: Harness the power of Nginx to make the most of your infrastructure and serve pages faster than ever beforeInstall Nginx With PHP-FPM on Ubuntu 20.04 How To Linux Tutorials nginx ubuntu Web Hosting

Practical Apache, PHP-FPM & Nginx Reverse Proxy: How to Build a Secure, Fast and Powerful Webserver from scratch (Practical Guide Series Book 3)Install Nginx With PHP-FPM on Ubuntu 20.04 How To Linux Tutorials nginx ubuntu Web Hosting