PHP, short for PHP hypertext preprocessor, is one of the server-side programming languages. It is widely used in web development for supporting and running PHP applications on a web server. It first appeared in 1995 but is now currently maintained by Zend technologies. Many content management systems (CMS) such as WordPress and Lavaravel are written in PHP.
In this tutorial, we will demonstrate how to install PHP on Ubuntu 22.04 and combine it with Apache and Nginx.
Prerequisites
To get started, here is what you need:
An instance of Ubuntu 22.04 with SSH access.
A sudo user configured on the instance for carrying out root user tasks.
Enabling PHP Repository on Ubuntu
At the time of writing this guide, the default PHP version that is currently hosted or provided by Ubuntu repositories is PHP 8.1.
To install PHP using the APT package manager from the default Ubuntu repository, type:
sudo apt install php
To take advantage of these latest PHP versions, you need to enable the Ondrej PPA repository which provides the latest versions of PHP. This is a PPA that is maintained by Ondrej Sury, a Debian developer who has been packaging PHP since PHP 5.
To accomplish this, first, install the prerequisites
sudo apt install software-properties-common -y
Then enable the Ondrej PPA using the following command.
sudo add-apt-repository ppa:ondrej/php
Then update the local package index to start using the newly added PHP repository.
sudo apt update
Install PHP 8.0 with Apache
With the PPA installed, you can now install your preferred PHP version. In case you have Apache as your web server, you have the option of running PHP as an Apache PHP module or PHP-FPM.
To install PHP 8.0 on Ubuntu as an Apache module, run the following commands:
Once installed, reload Apache for the changes to take effect:
sudo systemctl restart apache
To confirm PHP 8.0 is installed, run the following command:
php -v
Install PHP 8.1 with Apache
PHP 8.1 is the latest version of PHP at this time of writing. As it comes default in the repository you simply install PHP and Apache PHP module as follows:
Don't forget to restart the Apache webserver of PHP-FPM after the installation of PHP extensions.
Switch Between PHP Versions
Suppose you have installed different versions of PHP. How do you switch from one version to another?
First, you might want to have a peek at the multiple PHP versions installed on your system. To do this, run the following command:
sudo update-alternatives --config php
This is the output that you will get.
There are 3 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/php8.1 81 auto mode
* 1 /usr/bin/php7.4 74 manual mode
2 /usr/bin/php8.0 80 manual mode
3 /usr/bin/php8.1 81 manual mode
Press <enter> to keep the current choice[*], or type selection number:
To switch from, PHP 7.4 to PHP 8.1, run the following commands:
sudo a2dismod php7.4
sudo a2enmod php8.1
The above commands simply disable PHP 7.4 and enable PHP 8.0 on ubuntu. You can switch from one version to another simply by interchanging the versions. To apply the changes made, restart your Apache webserver.
sudo systemctl apache2 restart
Check PHP Version
To verify the default installed PHP version, run the command
php -v
You should get output similar to what we have here:
PHP 8.1.2 (cli) (built: Apr 7 2022 17:46:26) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies
Another way to test if PHP works is by creating a file called info.php file in the Document root. The info.php file is a file that provides details of the default PHP version installed and other details of php modules.
So, create the file in the /var/www/html directory as follows.
sudo vim /var/www/html/info.php
Next, add the following code
<?php
phpinfo();
?>
Save the file. Now open your browser and visit the URL
http://server-ip/info.php
Both methods confirm that, indeed, PHP is installed.
Conclusion
In this guide, we learned various approaches to installing PHP and the required PHP modules on Ubuntu 22.04. PHP 8.1 is the default version that is provided by the official Ubuntu repositories. To install the latest versions of PHP, you need to enable the Ondrej PPA on your Linux system.
If this resource helped you, let us know your care by a Thanks Tweet.
Did you find this article helpful?
We are glad you liked the article. Share with your friends.
Comments