How to Install PHP-FPM with Nginx on Ubuntu

Here is how you can install PHP-FPM with Nginx on Ubuntu. In particular on Vultr’s instance i.e. Ubuntu 18.04 LTS / 19.10 / 20.04 LTS.

Prerequisites

  • Have an Ubuntu 18.04 x64 / 19.10 x64 / 20.04 x64 instance.
  • Logged in as a root with sudo privileges.
  • Installed Nginx

Step 1: Add Ondrej PHP PPA repository

PHP can be installed using Ondřej Surý PPA, so install the software-properties-common package, add the ondrej PPA and update your sources using the following commands:

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ondrej/php
sudo apt update

Step 2: Install PHP 7.3-FPM/PHP 7.4-FPM For Nginx

Install PHP 7.3-FPM on Ubuntu using the following command:

sudo apt install php7.3-fpm
or
sudo apt install php7.4-fpm

Step 3: Check the PHP installation

You can use php -v the command to check the PHP version installed on your server and output will be

PHP 7.3.11-1+ubuntu19.10.1+deb.sury.org+6 (cli) (built: Oct 28 2019 21:34:37) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.11-1+ubuntu19.10.1+deb.sury.org+6, Copyright (c) 1999-2018, by Zend Technologies

In case if you have installed PHP 7.4-FPM, following will be output:

PHP 7.4.28 (cli) (built: Feb 17 2022 16:06:35) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.28, Copyright (c), by Zend Technologies

Step 4: Install PHP Extensions

Install the most commonly used PHP extensions using the following command for PHP 7.3:

sudo apt install php7.3-common php7.3-zip php7.3-curl php7.3-xml php7.3-xmlrpc php7.3-json php7.3-mysql php7.3-pdo php7.3-gd php7.3-imagick php7.3-ldap php7.3-imap php7.3-mbstring php7.3-intl php7.3-cli php7.3-tidy php7.3-bcmath php7.3-opcache

or for PHP 7.4, run the following one.

sudo apt install php7.4-common php7.4-zip php7.4-curl php7.4-xml php7.4-xmlrpc php7.4-json php7.4-mysql php7.4-pdo php7.4-gd php7.4-imagick php7.4-ldap php7.4-imap php7.4-mbstring php7.4-intl php7.4-cli php7.4-tidy php7.4-bcmath php7.4-opcache

You can confirm the installed version of any PHP extension using apt policy command i.e. apt policy php7.3-cli and output will be

php7.3-cli:
  Installed: 7.3.11-1+ubuntu19.10.1+deb.sury.org+6
  Candidate: 7.3.11-1+ubuntu19.10.1+deb.sury.org+6
  Version table:
 *** 7.3.11-1+ubuntu19.10.1+deb.sury.org+6 500
        500 http://ppa.launchpad.net/ondrej/php/ubuntu eoan/main amd64 Packages
        100 /var/lib/dpkg/status
     7.3.11-0ubuntu0.19.10.1 500
        500 http://archive.ubuntu.com/ubuntu eoan-updates/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu eoan-security/main amd64 Packages
     7.3.8-1 500
        500 http://archive.ubuntu.com/ubuntu eoan/main amd64 Packages

Now, PHP 7.3-FPM has been installed on your Ubuntu server.

Step 5: Configure PHP 7.3-FPM/PHP 7.4-FPM

The default PHP configuration file is at /etc/php/7.3/fpm/php.ini or /etc/php/7.4/fpm/php.ini and if we want to modify the default PHP configuration, open file by using the following command:

sudo nano /etc/php/7.3/fpm/php.ini
or
sudo nano /etc/php/7.4/fpm/php.ini

Make the changes on the following below lines in opened file and save. Below are recommended values being great settings to apply in your environments.

max_execution_time = 180
max_input_time = 360
max_input_vars = 5000
memory_limit = 256M
cgi.fix_pathinfo = 0
file_uploads = On
post_max_size = 192M
upload_max_filesize = 96M
allow_url_fopen = On

After making the change above, save the file and close out.

Step 6: Restart PHP-FPM and Nginx

After installing PHP and related modules, all you have to do is restart PHP-FPM and Nginx to reload PHP configurations.

To restart Nginx and PHP-FPM, run the commands below

sudo php-fpm7.3 -t
sudo systemctl restart php7.3-fpm
sudo systemctl restart nginx

or

sudo php-fpm7.4 -t
sudo systemctl restart php7.4-fpm
sudo systemctl restart nginx

Conclusion

Now you have learned how to install and configure PHP-FPM with Nginx on your Ubuntu server.

If you have a question or facing any problems, feel free to leave a comment.

Related Post

How to Install Nginx on Ubuntu

Here is how you can install Nginx on Ubuntu. In particular on Vultr’s instance i.e. Ubuntu 16.04 LTS / 18.04 LTS / 20.04 LTS / 22.04 LTS.

How To Create a Sudo User on Ubuntu

Here is how you can create a sudo user on Ubuntu. In particular on Vultr’s instance i.e. Ubuntu 16.04 LTS / 18.04 LTS / 18.10 / 22.04 LTS.

3 Replies

  1. Jadson

    Where is the part of Nginx?

  2. PixelsPress

    Hi Jadson,
    Thank you for highlighting the missing part. Added the link in Prerequisites of this article.

  3. danialnajarzade

    Thank You!

Leave a Reply