How to setup PHP on Nginx with fastCGI (PHP-FPM) example – TheServerSide.com

Why is Nginx, PHP and fastCGI so popular?

Nginx is the DevOps community’s most beloved http web server. And developers love the PHP programming language because it enables them to quickly build and deploy interactive websites.

As such, it’s no wonder that so many sys admins need to configure Nginx, PHP and PHP-FPM on both Linux and Windows servers.

This quick tutorial shows you how to setup PHP and Nginx on Ubuntu Linux with the fastCGI process manager (PHP-FPM) configured as Nginx’s PHP engine.

How to setup Nginx, PHP and PHP-FPM

To setup and configure fastCGI (FPM), PHP, and Nginx on Ubuntu Linux, follow these steps:

  1. Perform an apt-get update to ensure access to the latest packages.
  2. Install Nginx on Ubuntu.
  3. Install the php-fpm for Nginx package.
  4. Edit the server’s default config file to support PHP in Nginx.
  5. Restart the PHP configured Nginx server.
  6. Add a PHP file to Nginx’s html directory.
  7. Test the PHP, Nginx and PHP-FPM configuration.

Download the latest Nginx and PHP packages

Every software install in Ubuntu should start with a quick apt-get update and possibly an apt-get upgrade command.

sudo apt-get update -y
Reading package lists... Done
sudo apt-get upgrade -y
Calculating upgrade... Done

Install Nginx on Ubuntu

To install PHP on Nginx, you must first install Nginx, which you can achieve through a simple apt-get install command:

sudo apt-get install nginx -y
The following Nginx packages will be installed:
libnginx-mod-http-geoip2 nginx-common nginx-core
Setting up nginx 1.18.0-6 ubuntu... Done

Verify the running Nginx server

To verify the successful installation and configuration of Nginx on Ubuntu, query the HTTP server’s status:

sudo systemctl status nginx
nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled;)
   Active: active (Nginx running)

You can visually verify the Nginx landing page displays on http://localhost:80 of a web browser.

Install PHP for Nginx with PHP-FPM

To install PHP for Nginx, use the PHP-FPM library. You can install PHP-FPM support with another apt-get install command:

sudo apt-get install php8.1-fpm -y

In this instance, we have installed version 8.1 of the PHP and PHP-FPM packages.

A common mistake is to install the PHP, not PHP-FPM package. The problem with this approach is that unlike PHP-FPM, the PHP package installs the Apache HTTP server and its httpd process, which conflicts with Nginx.

Why does a …….

Source: https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Nginx-PHP-FPM-config-example

Leave a Reply

Your email address will not be published. Required fields are marked *