11 C
Texas

How To Install Nginx With PHP-FPM on Ubuntu 20.04

In this tutorial you are going to learn that how to install nginx with PHP-FPM(FastCGI Process Manager) on Ubuntu 20.04. Nginx is most popular web server having high speed & scalability providing the Reverse Proxy as well as load balancing to various backend servers. The protocols used by the Nginx are HTTP, TCP & UDP. Nginx is preffered over Apache as it have the low memory footprint & handle the requests in more easier way. So, let’s have a short introduction on features of Nginx and then we’ll move towards the installation of Nginx with PHP-FPM on our Ubuntu 20.04.

Some Important Features of Nginx

  • Content Cache: Cache static & dynamic content
  • Load Balancing: HTTP, TCP, & 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 at the same time
  • 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

So, follow the below steps for an easy installation.

Step 1: Update & Upgrade Your System

Fire the below command in your terminal to fetch the latest updates before going to install Nginx with PHP-FPM on Ubuntu 20.04

sudo apt update & sudo apt upgrade

Step 2: Installing Nginx on Ubuntu 20.04

- Advertisement -

Once the system is updated, run the given command to install the Nginx on Ubutnu 20.04.

sudo apt install nginx

Verify the installation by running

sudo systemctl status nginx

Output:

sabir@Ubuntu20:~$ sudo 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>
Active: active (running) since Thu 2020-06-18 07:18:21 PKT; 3s ago
Docs: man:nginx(8)
Process: 14321 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_pro>
Process: 14322 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; >
Main PID: 14323 (nginx)
Tasks: 2 (limit: 1078)
Memory: 4.2M
CGroup: /system.slice/nginx.service
├─14323 nginx: master process /usr/sbin/nginx -g daemon on; maste>
└─14324 nginx: worker process
جون 18 07:18:21 Ubuntu20 systemd[1]: Starting A high performance web server an>
جون 18 07:18:21 Ubuntu20 systemd[1]: Started A high performance web server and>
lines 1-15/15 (END)

As you’ve to select once server Apache or Nginx. So, to use Nginx either you’ve to change the Port or disable the Apache web server. To disable Apache, run the below command.

sudo systemctl disable --now apache2

And then allow the firewall access on port 80.

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

Step 3: Installing PHP-FPM

Now, as we’ve already installed Nginx. To use PHP with Nginx fire the below command.

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

Once the installation finished, verify the status of PHP-FPM services by running the below command.

sudo systemctl status php7.4-fpm.service

Output:

sabir@Ubuntu20:~$ sudo 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 p>
Active: active (running) since Thu 2020-06-18 07:20:10 PKT; 10s ago
Docs: man:php-fpm7.4(8)
Process: 18829 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /r>
Main PID: 18815 (php-fpm7.4)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0re>
Tasks: 3 (limit: 1078)
Memory: 11.1M
CGroup: /system.slice/php7.4-fpm.service
├─18815 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
├─18827 php-fpm: pool www
└─18828 php-fpm: pool www
جون 18 07:20:10 Ubuntu20 systemd[1]: Starting The PHP 7.4 FastCGI Process Mana>
جون 18 07:20:10 Ubuntu20 systemd[1]: Started The PHP 7.4 FastCGI Process Manag>
lines 1-16/16 (END)

You can find the PID & Socket files under the directory.

sabir@Ubuntu20:~$ ls /run/php/
php7.4-fpm.pid php7.4-fpm.sock php-fpm.sock

Step 4: Configuring PHP-FPM with Nginx On Ubutnu 20.04

Now, to configure PHP-FPM with Nginx edit your application Nginx config file & set fastcgi_pass section to load through the FPM socket. Look at the 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;

After it reload the Nginx & open your applications on the web to make sure they are working as expected. So, this is how you can install Nginx web server with PHP-FPM on Ubutnu 20.04.

So, hare this post and join our Telegram Channel. Also, buy us a coffee.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article