phpBB Heb

Main Menu

  • Home
  • Internet Forum
  • PHP Scripting Language
  • Open Source Software
  • Online Communities
  • Commerce

phpBB Heb

Header Banner

phpBB Heb

  • Home
  • Internet Forum
  • PHP Scripting Language
  • Open Source Software
  • Online Communities
  • Commerce
PHP Scripting Language
Home›PHP Scripting Language›How to configure PHP on Nginx with fastCGI sample (PHP-FPM)

How to configure PHP on Nginx with fastCGI sample (PHP-FPM)

By George T. Sprague
May 27, 2022
0
0

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 so many sysadmins need to configure Nginx, PHP, and PHP-FPM on Linux and Windows servers.

This quick tutorial shows you how to configure 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 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 a quick apt-get update and possibly a apt-get upgrade ordered.

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 ordered:

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)

You can visually 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, use the PHP-FPM library. 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 installs the Apache HTTP server and its httpd process, which conflicts 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 uses 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. This allows the PHP FastCGI Process Manager to handle requests that have a .php extension.

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 the index list.
  • Uncomment PHP scripts in the 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 .htaccess files.

To configure PHP, Nginx and FTP (fastCGI), you need to update 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 commits the updated Nginx configuration file to ensure the changes don’t 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

To enable the Nginx PHP fastCGI 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 /var/www/html folder and test to make sure the page displays correctly on the server.

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

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 perform a quick echo ordered:

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

With Nginx setup, PHP and PHP-FPM module completed, and a new file named info.php added to the web server, just 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

Let’s quickly review this PHP and Nginx tutorial. Here are all the commands we 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.

Related posts:

  1. Pay what you want to earn expert Python training courses with this bundle
  2. What is headless commerce and why is it prevalent in the world of e-commerce?
  3. How to write your first PHP code
  4. PHP Project Says A Security Issue Is Likely Due To A Main Database Leak

Recent Posts

  • A high country paradise for sockeye salmon | Alaska Science Forum
  • How to Choose the Best Tech Stack for Your Startup in 2022
  • Open Source to commercial software, the process from project to product
  • 5G services expected to be rolled out within a month, says MoS Telecom
  • 3 ways every business can get started with an open source software strategy

Archives

  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020

Categories

  • Commerce
  • Internet Forum
  • Online Communities
  • Open Source Software
  • PHP Scripting Language
  • Terms and Conditions
  • Privacy Policy