This guide has been created to help users running Ubuntu 22.04|20.04|18.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 Web Server
- 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 Web Server
After the system is updated, proceed to install Nginx package on Ubuntu22.04|20.04 Linux system:
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
sudo systemctl restart nginx
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
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-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
PHP-FPM has the service that should be running.
### Ubuntu 20.04 ###
$ systemctl status php*-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.
### Ubuntu 22.04 ###
$ systemctl status php*-fpm.service
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2022-11-30 22:24:44 EAT; 12s ago
Docs: man:php-fpm8.1(8)
Process: 69709 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 80 (code=exited, status=0/SUCCESS)
Main PID: 69706 (php-fpm8.1)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 9482)
Memory: 9.1M
CPU: 41ms
CGroup: /system.slice/php8.1-fpm.service
├─69706 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
├─69707 php-fpm: pool www
└─69708 php-fpm: pool www
Nov 30 22:24:44 ubuntu22 systemd[1]: Starting The PHP 8.1 FastCGI Process Manager...
Nov 30 22:24:44 ubuntu22 systemd[1]: Started The PHP 8.1 FastCGI Process Manager.
PID and Socket file are located in the directory:
### Ubuntu 20.04 ###
$ ls /run/php/
php-fpm.sock php7.4-fpm.pid php7.4-fpm.sock
### Ubuntu 22.04 ###
$ ls /run/php/
php-fpm.sock php8.1-fpm.pid php8.1-fpm.sock
Step 4: Configure PHP-FPM with Nginx
Edit your Application Nginx configuration file and set fastcgi_pass section to load through FPM socket. See below snippet.
$ sudo vim /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/php-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 Nginx Web server on Ubuntu 22.04|20.04 Linux machine.
Similar guides:
Recommended books to read:
- Best Books to learn Web Development – PHP, HTML, CSS, JavaScript and jQuery
- Best Books To Master Web Design
- Best Books To Learn CSS & CSS3
- Best Books To Learn HTML & HTML5
- Best Apache and Nginx reference Books