Example content

Sample Nginx PHP FPM Configuration

Why are Nginx, PHP and fastCGI so popular?

Nginx is the most popular http web server in the DevOps community.

And developers love the PHP programming language because it lets them build and deploy interactive websites quickly.

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

This quick tutorial will show you how to quickly setup PHP and Nginx on Ubuntu Linux with the fastCGI Process Manager (PHP-FPM) configured as Nginx’s PHP engine.

How to configure Nginx, PHP and PHP-FPM

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

  1. Update apt-get to ensure access to the latest PHP, FPM, and Nginx packages
  2. Install Nginx on Ubuntu
  3. Install the php-fpm package for Nginx
  4. Change server default configuration file to support PHP in Nginx
  5. Restart the Nginx server configured in PHP
  6. Add PHP file to Nginx html phone book
  7. Test PHP, Nginx and PHP-FPM configuration

Download the latest Nginx and PHP packages

Every software installation in Ubuntu should start with an apt-get quick 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.

This is achieved via 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

Check Running Nginx Server

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

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)

A visual check would be to verify that the Nginx landing page is showing on http://localhost:80 of a web browser.

Install PHP for Nginx with PHP-FPM

To install PHP for Nginx, the PHP-FPM library must be used. You can install PHP-FPM support with another apt-get install ordered:

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

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

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

Why does a basic PHP installation require Apache?

PHP requires one of three dependencies to exist on a machine:

  • libapache2-mod-php
  • php-fpm
  • php-cgi

A simple PHP installation will use the default libapache2-mod-php module, which requires installing the full Apache HTTP Server software suite. To avoid this, install the php-cgi or php-fpm module for Nginx.

Check if PHP-FPM is running

After PHP-FPM setup is complete, check if it is running:

sudo systemctl status php8.1-fpm
● php8.1-fpm.service - PHP 8.1 FastCGI Process Manager FPM for Ubuntu
  Loaded: loaded (/lib/systemd/system/php8.1-fpm.service)
  Active: active (php-fpm running)

Add PHP support to Nginx

With Nginx and PHP-FPM installed, you need to modify the default Nginx configuration file to allow requests with a .php extension to be handled by the PHP FastCGI process manager.

The default Nginx file can be opened with any text editor. This command will open it with Nano:

sudo nano /etc/nginx/sites-available/default

Make the following changes to the Nginx configuration to support PHP and PHP-FPM on the server:

  • Add index.php to index list
  • Uncomment PHP scripts in FastCGI input block
  • Uncomment the line to include snippets/fastcgi-php.conf
  • Uncomment the line to enable fastcgi_pass and php8.1-fpm. sock
  • Uncomment the section to deny all access to Apache .htacccess files

To configure PHP, Nginx, and FTP (fastCGI), updates must be made to the Nginx configuration file.

Enable PHP in Nginx configuration file

The server section of the Nginx, PHP, and PHP-FPM configuration file will look like this when complete. Changes are highlighted in bold:

server {
  # Example PHP Nginx FPM config file
  listen 80 default_server;
  listen [::]:80 default_server;
  root /var/www/html;

  # Add index.php to setup Nginx, PHP & PHP-FPM config
  index index.php index.html index.htm index.nginx-debian.html;

  server_name _;

  location / {
    try_files $uri $uri/ =404;
  }

  # pass PHP scripts on Nginx to FastCGI (PHP-FPM) server
  location ~ .php$ {
    include snippets/fastcgi-php.conf;

    # Nginx php-fpm sock config:
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    # Nginx php-cgi config :
    # Nginx PHP fastcgi_pass 127.0.0.1:9000;
  }

  # deny access to Apache .htaccess on Nginx with PHP, 
  # if Apache and Nginx document roots concur
  location ~ /.ht {
    deny all;
  }
} # End of PHP FPM Nginx config example

How to Validate an Nginx Configuration File

The following command will validate the updated Nginx configuration file to ensure that the changes do not create syntax errors:

sudo nginx -t
nginx php config: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx php-fpm config: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx to enable fastCGI

To enable Nginx’s fastCGI PHP configuration, restart the server:

sudo systemctl restart nginx

Create a PHP page in Nginx

After reboot, PHP is fully enabled on Nginx. To prove it, create a PHP file in Nginx’s /var/www/html folder and test to make sure the page renders correctly on the server.

You may need to change the permissions on the folder with a CHMOD command in order to create a file there:

sudo chmod -R 777 /var/www/html

Then add a new PHP file to the Nginx web hosting directory. The easiest way to do this is to use a quick echo command:

echo "" >> /var/www/html/info.php

This creates the most basic PHP file outside of a “Hello World” example you might create. If you don’t like the echo command, use an editor to create a file named info.php in the /var/www/html folder with the following content:


  phpinfo();
?>

Test Nginx, PHP integration

Once the configuration of Nginx, PHP and the PHP-FPM module is complete, and a new file named info.php added to the web server, simply open a browser to http://localhost/info.php to test the configuration. The PHP info page, attesting to the fact that installing PHP 8.1 on Nginx, will appear.

nginx php fpm install successful

When PHP, FPM and Nginx are fully configured, the server will be able to render PHP pages.

Commands to configure PHP and Nginx in Ubuntu

As a quick review of the PHP and Nginx tutorial, here are all the commands that were used to enable the fastCGI process manager for PHP in Nginx:

  • sudo apt-get update -y
  • sudo apt-get upgrade -y
  • sudo apt-get install nginx -y
  • sudo systemctl status nginx
  • sudo apt-get install php8.1-fpm -y
  • sudo systemctl status php8.1-fpm
  • sudo nano /etc/nginx/sites-available/default
  • sudo nginx -t
  • sudo systemctl restart nginx
  • sudo chmod -R 777 /var/www/html
  • echo "" >> /var/www/html/info.php

And it’s that easy to install and configure fastCGI (FTP), Nginx and PHP support on your web server.


Source link