How to add PHP-FPM support for NGINX sites

1640351662 How to add PHP FPM support for NGINX sites

If you are building an NGINX site, you may need PHP-FPM support enabled. Jack Wallen shows you how.

If you have decided to switch from Apache to NGINX, one of the questions you may be asking is how to support PHP Express Process Manager (PHP-FPM). For those who may not be familiar with it, PHP-FPM is a FastCGI handling tool for PHP scripts and applications, which makes it possible for a website to handle higher loads. PHP-FPM is much faster than traditional multi-user PHP-based PHP environments and also allows multiple applications using different versions of PHP.

I want to walk you through the process of installing PHP-FPM and then enabling it in your NGINX sites. It's not as simple as Apache, but it should not be a challenge for any IT pro.

SEE: Linux service control commands (Premium TechRepublic)

Index

    What you need

    The only things you need for this are a running instance of NGINX and a user with sudo privileges. I will be appearing on my favorite server, Ubuntu 20.04, but this process can be modified to work on any platform that supports PHP and PHP-FPM.

    How to install PHP-FPM

    The first thing we need to do is install PHP-FPM, and a few more things. Log in to your Ubuntu Server instance and, from the last window, issue the command:

    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 -y

    The above command downloads and installs all necessary dependencies.

    Once PHP-FPM is installed, you then need to find out which version of PHP is installed. Do so with the command:

    php --version

    You can then check the PHP-FPM status with the command:

    sudo systemctl status php7.X-fpm

    Where X is the version number of PHP.

    If you find PHP-FPM is not running (it should be), start with the command:

    sudo systemctl start php7.X-fpm

    Where X is the version number of PHP.

    Finally, enable PHP-FPM with the command:

    sudo systemctl enable php7.x-fpm

    Again, where X is the version number of PHP.

    READ  SD-WAN may be the future of the hybrid workplace, a study suggests

    How to set up PHP-FPM

    Now we need to make some configuration changes, so that NGINX knows about PHP-FPM. The first is to edit the default NGINX configuration file. Open that file with the command:

    sudo nano /etc/nginx/nginx.conf

    In that file, look for the line:

    #server_tokens off;

    Uncomment that line until it looks like:

    server_tokens off;

    Save and close the file.

    Next, we need to edit the default virtualhost configuration. Open that file with the command:

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

    In that file, look for the section below:

    # pass PHP scripts to FastCGI server

    That category must look like:

    location ~ .php$ {
    
                    include snippets/fastcgi-php.conf;
    
            #
    
            #       # With php-fpm (or other unix sockets):
    
                    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    
            #       # With php-cgi (or other tcp sockets):
    
            #       fastcgi_pass 127.0.0.1:9000;
    
            }

    In other words, uncomment (delete the # character) lines:

    • place ~ .php $ {
    • import cookies / fastcgi-php.conf;
    • fastcgi_pass unix: /var/run/php/php7.4-fpm.sock;
    • }

    Save and close the file.

    Restart NGINX with the command:

    sudo systemctl restart nginx

    How to test the installation

    Create a test file with the command:

    sudo nano /var/www/html/info.php

    In that file, include the following contents:

    <?php
    phpinfo();
    ?>

    Save and close the file.

    Open a web browser and specify it http: //SERVER/info.php (where SERVER is the IP address of the host server). You should see, near the top of the PHP info page, FPM / FastCGI listed for the Server API and, under cgi-fcgi, php-fpm is active (Figure A.).

    Figure A.

    PHP-FPM is installed and active for NGINX.

    And there you have it, you have NGINX installed and running with PHP-FPM support. Remember, when building your virtualhost configuration files, you need to make sure you include PHP support in those. For that, you can use the / etc / nginx / sites-available / default file as an example.

    Congratulations, enjoy that PHP / NGINX goodness.

    Subscribe to TechRepublic's How To Make Tech Work on YouTube for all the latest tech tips for business benefits from Jack Wallen.

    See also

    nginxhero.jpg

    Image: NGINX

    Related Posts

    Deja una respuesta

    Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

    Subir

    We use cookies to ensure that we give the best user experience on our website. If you continue to use this site we will assume that you agree. More information