PHP is a powerful server-side programming language widely used to create dynamic websites, web applications, and content management systems (CMS). It can seamlessly integrate with a Ubuntu server to build cutting-edge web solutions. PHP is a go-to choice for web developers as it allows for easy integration with various databases and frameworks, enabling the creation of robust and dynamic web applications. Some of the key characteristics of PHP include the following:
- Support for various web servers: PHP can be integrated with web servers such as Apache, Nginx, and IIS.
- Flexibility: PHP supports both object-oriented and functional programming paradigms.
- Scalability: PHP can handle large amounts of data and can be easily scaled to meet the demands of growing web applications.
- Ease of deployment: PHP applications can be easily deployed and maintained on a Ubuntu server.
- Support for popular frameworks: PHP has a wide range of libraries and frameworks, such as Laravel and CodeIgniter, making web development more efficient.
- Easy integration with front-end technologies: PHP can be easily integrated with front-end technologies like HTML, CSS, and JavaScript to build dynamic websites and web applications.
- Easy integration with other systems: PHP can be integrated with other systems like payment gateways, email servers, and more.
- Widely supported by hosting providers: PHP is widely supported by hosting providers, making it easy to set up on various platforms.
- Highly customizable: PHP is highly customizable and configurable to suit specific project requirements.
In this guide, you will learn how to use the command line terminal to add the Ondřej Surý Repository and install the latest versions of PHP (8.2, 8.1, 8.0, or 7.4) on Ubuntu 22.04 “Jammy Jellyfish” or Ubuntu 20.04 “Focal Fossa” on your system.
Table of Contents
Step 1: Update Ubuntu System Packages
Keep your Ubuntu system up to date by running the following command to update all existing packages.
sudo apt update
Before proceeding, you can check the updates that will be made to your system by using the command to list the upgrades available.
apt list --upgradable
Use the provided command to start the upgrade process if any updates are available.
sudo apt upgrade
Step 2: Install Required Dependencies
To complete this guide, certain packages need to be installed. Please execute the following command to install them.
sudo apt install ca-certificates apt-transport-https software-properties-common lsb-release -y
Step 3: Import PPA LaunchPAD Repository
To access the latest versions of PHP, you can import the repository maintained by Ondrej, a well-known PHP maintainer for Debian and Ubuntu distributions. He has maintained this repository for over a decade, known as one of the most reliable Launchpad PPA’s. This repository contains updated versions of PHP that are much more up-to-date than the default APT repository offered by Ubuntu.
If you are new to importing GPG keys from Ubuntu’s KeyServer, a common issue you may encounter is that your command line terminal may not create the necessary directories. However, you can quickly resolve this by using a simple command to make these directories and ensure smooth operation when launching PPAs on Launchpad.
sudo gpg --list-keys
Example output:
gpg: directory '/root/.gnupg' created
gpg: keybox '/root/.gnupg/pubring.kbx' created
gpg: /root/.gnupg/trustdb.gpg: trustdb created
This step is optional, but if you encounter any issues, you can run the command and try again.
The following step is to import the necessary GPG key.
sudo gpg --no-default-keyring --keyring /usr/share/keyrings/ondrej.gpg --keyserver keyserver.ubuntu.com --recv-keys 14AA40EC0831756756D7F66C4F4EA0AAE5267A6C
Example output:
gpg: keybox '/usr/share/keyrings/ondrej.gpg' created
gpg: key 4F4EA0AAE5267A6C: public key "Launchpad PPA for Ondřej Surý" imported
gpg: Total number processed: 1
gpg: imported: 1
The next step is to import the repository for your Ubuntu system by executing the following command.
echo "deb [signed-by=/usr/share/keyrings/ondrej.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list.d/ondrej-php.list
You can run a quick update to cache the new source with the imported repository.
sudo apt update
After importing the PPA and running an update, you should see a few packages that need updating; run an upgrade now.
sudo apt upgrade
Step 2: Install PHP
Option 1: Install PHP with Apache Option
You can run PHP as an Apache module or use PHP-FPM when using an Apache HTTP server. Installing only the PHP version you need is recommended, as having multiple versions can make it hard to manage and keep clean. You can use the following command to install PHP as an Apache module.
To install PHP 8.2 module, run the following command:
sudo apt install php8.2 libapache2-mod-php8.2
To install PHP 8.1 module, run the following command:
sudo apt install php8.1 libapache2-mod-php8.1
To install PHP 8.0 module, run the following command:
sudo apt install php8.0 libapache2-mod-php8.0
To install PHP 7.4 module, run the following command:
sudo apt install php7.4 libapache2-mod-php7.4
After the installation, you will need to restart your Apache server for the new PHP module to be loaded.
sudo systemctl restart apache2
Option 2: Install Apache with PHP-FPM
PHP-FPM (FastCGI Process Manager) is a popular alternative to the standard PHP (Hypertext Processor) FastCGI implementation. To install the desired version of PHP, PHP-FPM, with Apache, use the corresponding command.
To install PHP, and PHP-FPM 8.2 with Apache, use the following command.
sudo apt install php8.2-fpm libapache2-mod-fcgid
To install PHP, and PHP-FPM 8.1 with Apache, use the following command.
sudo apt install php8.1-fpm libapache2-mod-fcgid
To install PHP, and PHP-FPM 8.0 with Apache, use the following command.
sudo apt install php8.0-fpm libapache2-mod-fcgid
To install PHP, and PHP-FPM 7.4 with Apache, use the following command.
sudo apt install php7.4-fpm libapache2-mod-fcgid
Please note that PHP-FPM is not enabled for Apache by default. You must enable it using the following command.
sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php{version}-fpm
It’s important to note that you must replace the {version} section in the above command with your desired PHP version. For example, if you want to use PHP 8.2, the command should be as follows.
sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php8.2-fpm
Finally, restart the Apache web server.
sudo systemctl restart apache2
Option 3: Install PHP support for Nginx
Install PHP & PHP-FPM
Unlike other web servers like Apache, Nginx does not have built-in support for processing PHP files. To handle PHP files with Nginx, you must install PHP-FPM (FastCGI Process Manager).
Use the following terminal command to install PHP and PHP-FPM for Nginx.
To install PHP 8.2 for Nginx support, use the following command.
sudo apt install php8.2 php8.2-fpm php8.2-cli -y
To install PHP 8.1 for Nginx support, use the following command.
sudo apt install php8.1 php8.1-fpm php8.1-cli -y
To install PHP 8.0 for Nginx support, use the following command.
sudo apt install php8.0 php8.0-fpm php8.0-cli -y
To install PHP 7.4 for Nginx support, use the following command.
sudo apt install php7.4 php7.4-fpm php7.4-cli -y
Once the installation is complete, the PHP-FPM service should start running automatically. In case it does not, you can use the following command to start it.
sudo systemctl start php{version}-fpm
Configure Nginx Server Block for PHP-FPM
For Nginx to process PHP files, you need to edit your Nginx server block and include the following example. This should be added to all server blocks that handle PHP files, specifically the location block that matches the ~ .php$ pattern.
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php{version}-fpm.sock;
}
Make sure to replace {version} with the PHP version you want.
You can test the modifications made to your Nginx configuration for errors by executing the following command.
sudo nginx -t
Next, use the command below that will verify the syntax of the configuration files and notify you of any errors that are found.
Example output if successful:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
To finalize the installation, it is necessary to restart the Nginx service.
sudo systemctl restart nginx
As a reminder, you can use the following command to check which version of PHP is currently installed on your system.
php --version
Example output with PHP 8.2 installed:
Step 3: Create a PHP Test File
Here are the steps to creating a PHP test file on an Ubuntu Linux terminal.
Use the following command to create a new file named “test.php” in the desired directory.
touch test.php
Now use the following command to open the file in a text editor, such as nano:
nano test.php
In the text editor, you can add the following PHP code.
<?php
echo "PHP is working!";
?>
To save the file, press CTRL + O; to exit the editor, press CTRL + X.
You can run the test file by using the following command.
php test.php
If the PHP works correctly on your system, you should see the message “PHP is working!” printed on the terminal. Please note that you can use other text editors, such as vim, gedit, etc., instead of nano.
Additional Commands & Tips
Switch PHP Alternative Versions
In Ubuntu, you can switch between different versions of PHP using the “update-alternatives” command in the terminal. Here are the steps to switch to PHP alternatives:
First, you need to check the current PHP version active on your system by running the command.
php -v
Use the command to see a list of all available PHP versions on your system.
sudo update-alternatives --list php
Example output:
To switch to a different version of PHP, use the command.
sudo update-alternatives --set php /usr/bin/php{version}
Example: To switch to PHP 7.4, you would use the command.
sudo update-alternatives --set php /usr/bin/php7.4
You will likely see an output similar to the following in your terminal.
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual mode
After executing the command, you can check the newly activated version of PHP by using the following command.
php -v
This should now be PHP version 7.4, as the example had all versions of PHP from this guide installed at the time.
Please remember that when using PHP-FPM, you may need to make adjustments to your Nginx server blocks.
Conclusion
To sum up, installing PHP on a Ubuntu system is a relatively easy task. By adhering to the steps outlined in this guide, you should quickly set up and configure PHP on your Debian system. Furthermore, you should also be able to confirm that PHP is operating correctly by executing a basic test script. With PHP installed, you can run a wide range of dynamic web applications and scripts on your Ubuntu system.