
How to Install PHP 7.3 on Ubuntu 18.04: A Step-by-Step Guide
While newer versions of PHP offer the latest features, many production applications still require a specific version for compatibility and stability. PHP 7.3, known for its performance improvements and features like flexible Heredoc and Nowdoc syntax, remains a popular choice for many projects. If you’re running a server on Ubuntu 18.04 (Bionic Beaver), you’ll find that the default repositories don’t include this specific version.
This comprehensive guide will walk you through the entire process of installing PHP 7.3 on your Ubuntu 18.04 server, configuring it with popular web servers like Apache or Nginx, and securing your installation for production use.
Prerequisites
Before you begin, ensure you have the following:
- An Ubuntu 18.04 server.
- A user account with
sudoor root privileges. - Your system’s package index is up to date.
To update your package list, run the following commands in your terminal:
sudo apt update
sudo apt upgrade
Step 1: Add the Ondřej Surý PPA Repository
The most reliable way to install specific PHP versions on Ubuntu is by using the PPA (Personal Package Archive) maintained by Ondřej Surý. This repository is widely trusted in the developer community and provides up-to-date PHP packages.
First, install the software-properties-common package, which allows you to manage PPA repositories easily.
sudo apt install software-properties-common
Next, add the Ondřej Surý PPA to your system’s sources.
sudo add-apt-repository ppa:ondrej/php
You will be prompted to confirm. Press Enter to proceed. After adding the PPA, you must update your package list again to include the packages from the new repository.
sudo apt update
Step 2: Install PHP 7.3 and Core Modules
With the PPA enabled, you can now install PHP 7.3. This command will install the core PHP engine, the command-line interface (CLI), and PHP-FPM (FastCGI Process Manager), which is essential for modern web server integration.
sudo apt install php7.3 php7.3-cli php7.3-fpm
Step 3: Install Common PHP 7.3 Extensions
A base PHP installation is rarely sufficient for running modern applications like WordPress, Laravel, or Magento. You will likely need several extensions to handle database connections, image processing, XML parsing, and more.
You can install multiple extensions with a single command. Here is a list of commonly required extensions:
php7.3-mysql: For connecting to MySQL or MariaDB databases.php7.3-curl: For making HTTP requests to external APIs.php7.3-gd: A graphics library for image manipulation.php7.3-mbstring: For handling multi-byte strings (essential for UTF-8).php7.3-xml: For working with XML data.php7.3-zip: For handling .zip archives.php7.3-intl: For internationalization functions.
Install them using the following command:
sudo apt install php7.3-mysql php7.3-curl php7.3-gd php7.3-mbstring php7.3-xml php7.3-zip php7.3-intl
Search for other available extensions by running sudo apt-cache search php7.3-.
Step 4: Verify the Installation
Once the installation is complete, you can verify that PHP 7.3 is the active version on your command line.
php -v
The output should look similar to this, confirming the version number:
PHP 7.3.33-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Aug 22 2022 10:09:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.33-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
Configuring Your Web Server for PHP 7.3
After installing PHP-FPM, you must configure your web server to use it to process PHP files.
For Apache
If you are using Apache, you’ll need to install a module to communicate with PHP-FPM and enable the necessary configurations.
- Install the FastCGI module for Apache.
bash
sudo apt install libapache2-mod-fcgid
- Enable the required Apache modules and the PHP-FPM configuration.
bash
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.3-fpm
- Restart Apache to apply the changes.
bash
sudo systemctl restart apache2
Apache will now automatically pass .php files to the PHP 7.3-FPM service for processing.
For Nginx
Nginx is well-known for its excellent performance with PHP-FPM. To configure it, you need to edit your site’s server block configuration file (usually located in /etc/nginx/sites-available/).
Open your configuration file and add or modify the location block for .php files to look like this:
server {
# ... other server directives like listen, server_name, root
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# Pass PHP scripts to PHP-FPM
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# Use the PHP 7.3 FPM socket
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
# Deny access to .htaccess files
location ~ /\.ht {
deny all;
}
}
Key change: Ensure the fastcgi_pass directive points to the correct PHP 7.3 socket path: unix:/run/php/php7.3-fpm.sock.
After saving your changes, test the Nginx configuration and restart the service.
sudo nginx -t
sudo systemctl restart nginx
Important Configuration and Security Tips
Your PHP configuration is controlled by the php.ini file. There are different files for the CLI and FPM services:
- FPM Config:
/etc/php/7.3/fpm/php.ini - CLI Config:
/etc/php/7.3/cli/php.ini
When making changes for your web application, you should edit the FPM configuration file. After making any changes, remember to restart the PHP-FPM service:
sudo systemctl restart php7.3-fpm
Here are a few recommended security and performance adjustments:
- Hide PHP Version: Set
expose_php = Offto prevent attackers from easily identifying your PHP version. - Adjust Resource Limits: Modify
memory_limit,upload_max_filesize, andpost_max_sizeto suit your application’s needs while preventing abuse. - Disable Dangerous Functions: For added security, you can disable functions that are often exploited in attacks by adding them to the
disable_functionsdirective.
ini
disable_functions = exec,passthru,shell_exec,system,proc_open,popen
Conclusion
You have successfully installed and configured PHP 7.3 on your Ubuntu 18.04 server. By using a trusted PPA, you ensure access to a stable and secure version of PHP that meets your application’s requirements. Remember to properly configure your web server and apply essential security settings from the php.ini file to maintain a robust and performant environment.
Source: https://kifarunix.com/how-to-install-php-7-3-3-on-ubuntu-18-04/


