This tutorial explains all the steps required to install the default distribution of PHP 7 on Ubuntu 20.04 LTS. The steps should be similar to other Linux based systems. It also explains how to configure PHP for NGINX Web Server with FastCGI.
Prerequisites
This tutorial expects that the Nginx is already installed and you have followed the steps to configure the server blocks using separate files for each block as explained at How To Install And Configure Nginx on Ubuntu 20.04 LTS.
Install PHP 7
In this step, we will install PHP 7.4, the default PHP package available for Ubuntu 20.04 LTS release. It can be installed using the command as shown below.
Notes: Nginx needs PHP FPM to process the PHP requests. Use the below-mentioned command to install PHP 7 only if required. It also install the Apache Web Server which is not required by Nginx.
# Refresh indexes sudo apt update
# Install PHP 7.4 on Ubuntu 20.04 LTS sudo apt install php7.4
# Verify PHP php --version
# Output PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( 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
It will ask to confirm the installation. Press Y and hit Enter to confirm the installation. The important paths and files of the PHP installation are /usr/lib/php/7.4, /usr/bin/php7.4, and /etc/php/7.4/cli/php.ini.
Install PHP FPM
Use the below-mentioned commands to install the PHP FPM required by Nginx to process the PHP requests.
# Install PHP FPM - Required by Nginx sudo apt install php7.4-fpm
# Check whether enabled on system boot
sudo systemctl is-enabled php7.4-fpm
# Output enable
Also, make sure that PHP-FPM is active and running.
PHP FPM Status sudo systemctl status php7.4-fpm
# Output ● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2020-06-06 05:36:17 UTC; 24s ago Docs: man:php-fpm7.4(8) Process: 33932 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74 (code=exited,> Main PID: 33919 (php-fpm7.4) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 1119) Memory: 11.3M CGroup: /system.slice/php7.4-fpm.service ├─33919 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) ├─33930 php-fpm: pool www └─33931 php-fpm: pool www
-----
Install MySQL Extension
Use the below-mentioned commands to install the MySQL extension for PHP 7. Also, refer to How To Install MySQL 8 on Ubuntu 20.04 LTS.
# Install MySQL Extension sudo apt install php7.4-mysql
# Install MySQL Extension - Required for Wordpress Installation sudo apt install php-mysql
Additional Extensions
You can also install the additional extensions in order to fully support a web application. Some of these packages might not be required in your scenario, hence it's totally optional to install these extensions.
# Install CGI and CLI if not installed by default sudo apt install php7.4-cgi php7.4-cli
# Install CURL and JSON extensions sudo apt install php7.4-curl php7.4-json
# Install PHP GD and Imagick
sudo apt install php7.4-gd php-imagick
# Multibyte String, Internationalization and Spell Check sudo apt install php7.4-mbstring php7.4-intl php7.4-pspell
# Multibyte String, Internationalization and Spell Check - Required for Wordpress Installation sudo apt install php-mbstring php-intl php-pspell
# Emails sudo apt install php7.4-imap
# SQLite Database sudo apt install php7.4-sqlite3
# Tidy and XML RPC sudo apt install php7.4-tidy php7.4-xmlrpc
# Excel sudo apt install php7.4-xsl
Install OPcache to enable caching at the bytecode level.
# Install OPcache extension sudo apt install php7.4-opcache
Install the extensions to handle compressed files.
# Install Zip sudo apt install php7.4-zip
These are the steps required to install PHP 7 on Ubuntu 20.04 LTS and the additional packages that are mostly required for the website development.
Configure PHP Processor
We can secure PHP by updating the configuration as shown below.
# Update PHP Configuration sudo nano /etc/php/7.4/fpm/php.ini
# Update cgi.fix_pathinfo=0
# Reload PHP FPM sudo systemctl reload php7.4-fpm # OR # Restart PHP FPM sudo systemctl restart php7.4-fpm
# Test Status
sudo systemctl status php7.4-fpm
# Output ● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-06-06 16:47:58 IST; 2min 45s ago Docs: man:php-fpm7.4(8) Process: 38840 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.> Process: 39283 ExecReload=/bin/kill -USR2 $MAINPID (code=exited, status=0/SUCCESS) Main PID: 38814 (php-fpm7.4) Status: "Ready to handle connections" Tasks: 3 (limit: 4624) Memory: 14.7M CGroup: /system.slice/php7.4-fpm.service ├─38814 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) ├─39284 php-fpm: pool www └─39285 php-fpm: pool www
----
----
Important Commands
Below listed are the commands used to start, stop, enable, or disable the PHP FPM process.
# Start PHP FPM sudo systemctl start php7.4-fpm
# Stop PHP FPM
sudo systemctl stop php7.4-fpm
# Restart PHP FPM sudo systemctl restart php7.4-fpm
# Enable PHP FPM sudo systemctl enable php7.4-fpm
# Disable PHP FPM sudo systemctl disable php7.4-fpm
We can also confirm whether PHP is running using the netstat command as shown below.
# Test PHP netstat -pl | grep php
# Output (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) unix 2 [ ACC ] STREAM LISTENING 619439 - /run/php/php7.4-fpm.sock
Nginx User Permissions
We have to configure the Nginx system user in order to access the PHP via FPM. Check the system user allowed to access PHP FPM as shown below.
# Check FPM User sudo nano /etc/php/7.4/fpm/pool.d/www.conf
# Confirm the configuration
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
# Restart FPM sudo systemctl restart php7.4-fpm
We also need to update the Nginx user by updating the configuration as shown below.
# Update Nginx user sudo nano /etc/nginx/nginx.conf
# Update
# user nginx;
user www-data; ... ...
# Test Nginx sudo nginx -t
# Restart Nginx
sudo systemctl restart nginx
If the Nginx user is not configured, it will log the error as shown below.
# Nginx Permission Error 2020/06/06 14:37:20 [crit] 27012#27012: *8 connect() to unix:/run/php/php7.4-fpm.sock failed (13: Permission denied) while connecting to upstream,
Configure Nginx - Domain Server Block
Now we will configure Nginx to access PHP via FPM since Nginx does not support PHP by default. We will update the domain-specific virtual host or server block as part of this step. In this way, we can enable PHP only for the selected sites instead of enabling it by default for the sites. The default configuration of a site without PHP configuration looks like the one as shown below. You can follow Configure Virtual Host Or Server Block On Nginx to configure domain-specific virtual host or server block.
# Server Block without PHP FPM sudo nano /etc/nginx/sites-available/example.com
server { server_name example.com www.example.com;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /var/www/example.com/html;
index index.html index.htm;
}
...
... }
# Create Symbolic Link
sudo ln -s /etc/nginx/sites-available/mydomain.com /etc/nginx/sites-enabled/mydomain.com
# Disable Site (only if required) sudo unlink /etc/nginx/sites-enabled/mydomain.com
Now configure the server block to process all the PHP requests.
# Server Block with PHP FPM sudo nano /etc/nginx/sites-available/example.com
server { server_name example.com www.example.com;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / { root /var/www/example.com/html; index index.html index.htm; }
...
...
# pass the PHP scripts to FastCGI
location ~ \.php$ { root /var/www/example.com/html; fastcgi_intercept_errors on; fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; }
# deny access to .htaccess files
location ~ /\.ht {
deny all;
}
...
... }
# Reload NGINX
sudo systemctl reload nginx
Now we will create the PHP file to print the output of the phpinfo() function as shown below.
# Add PHP File sudo nano /var/www/example.com/html/info.php
# Content <?php echo phpinfo(); # Save the file and exit the editor
Now open the PHP file in the browser by navigating to http://www.example.com/info.php. It should print the PHP details as shown in Fig 1.
Configure Nginx - Localhost - Default Server Block
This section assumes that you have followed the steps to configure the server blocks using separate files for each block and also moved the default server block to /etc/nginx/sites-available/default as explained at How To Install And Configure Nginx on Ubuntu 20.04 LTS.
# Server Block without PHP FPM
sudo nano /etc/nginx/sites-available/default
server { listen 80; server_name localhost;
#charset koi8-r; #access_log /var/log/nginx/host.access.log main;
location / { root /usr/share/nginx/html; index index.html index.htm; }
...
...
}
# Create Symbolic Link
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
# Disable Site (only if required)
sudo unlink /etc/nginx/sites-enabled/default
Now configure the server block to process all the PHP requests.
# Server Block with PHP FPM sudo nano /etc/nginx/sites-available/default
server { listen 80; server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm; }
...
...
# pass the PHP scripts to FastCGI
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_intercept_errors on; fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; }
# deny access to .htaccess files location ~ /\.ht { deny all; }
...
... }
# Reload NGINX sudo systemctl reload nginx
Now we will create the PHP file to print the output of the phpinfo() function as shown below.
# Add PHP File sudo nano /usr/share/nginx/html/info.php
# Content <?php echo phpinfo(); # Save the file and exit the editor
Now open the PHP file in the browser by navigating to http://localhost/info.php. It should print the PHP details as shown in Fig 1.
Summary
This tutorial provided the steps to install PHP 7 on Ubuntu 20.04 LTS and also configure it for the Nginx Web Server. We have also installed the popular extensions for PHP which are required to host web applications.