Install PHP

Unlike Apache, Nginx does not contain native PHP processing. For that we have to install PHP-FPM (FastCGI Process Manager). FPM is an alternative PHP FastCGI implementation with some additional features useful for heavy-loaded sites.

You should add Ubuntu’s universe repository before installing php-fpm to ensure you get the correct package.

sudo add-apt-repository universe

If the prompt ask Adding component(s) 'universe' to all repositories., press ENTER.

Now update the package lists and install PHP-FPM on Ubuntu 20.04. We will also install php-mysql to allow PHP to communicate with the MySQL database.

sudo apt update && sudo apt install php-fpm php-mysql

Press Y and ENTER to continue installation.

Once installed, check the PHP version.

php --version

Output:

PHP 7.4.3 (cli) (built: Jul 5 2021 15:13:35) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Above we are using PHP version 7.4.3, though this may be a later version for you depending on which package was in the repository.

Depending on what version of Nginx and PHP you install, you may need to manually configure the location of the PHP socket that Nginx will connect to.

List the contents for the directory /var/run/php/.

ls /var/run/php/

You should see one or a few entries here.

php-fpm.sock  php7.4-fpm.pid  php7.4-fpm.sock

Last updated

Was this helpful?