PHP on Ubuntu – If you don’t know how to install PHP Ubuntu or uninstall it on your Ubuntu system then you`re in the right place. In this blog post, we’ll guide you how to install and uninstall PHP Ubuntu system.
Table of Contents
PHP Versions in Ubuntu
The accessible PHP versions in Ubuntu depend on the Ubuntu version and the sources that are enabled. Here’s a brief review of the PHP versions often available in different Ubuntu versions:
- Ubuntu 20.04 (Focal Fossa): The default version is PHP 7.4.
- Ubuntu 22.04 (Jammy Jellyfish): The default version is PHP 8.1.
- Ubuntu 23.10 (Kinetic Kudu): The default version is PHP 8.2.
- Ubuntu 24.04 (LTS, due for release in April 2024): The expected default version is PHP 8.3.
Note: These are the default versions available by the official Ubuntu repository. You can install additional PHP versions using Personal Package Archives (PPAs) if necessary. For example, if you want an older version, such as PHP 5.6, you can add a PPA that includes it for compatibility reasons.
Some additional points to consider
- Security upgrades: Newer PHP versions usually involve higher security features and more frequent safety updates. For additional safety, we recommend using the latest stable version whenever possible for better security.
- Application Compatibility: Ensure that the PHP version you select is compatible with your web applications. If you’re unsure, consult your application’s documentation.
- Multiple Versions: Ubuntu allows you to have multiple PHP versions installed simultaneously. This is handy for development, but make sure your web server is configured to use the correct version for each application.
How to Install PHP on Ubuntu?
Understanding how to install PHP Ubuntu gives you the ability to create your own web development environment. Then, you`re allowed to test and create dynamic apps and websites locally. This provides more control, flexibility, and affordability than relying entirely on external hosting services. Now, let`s see how to install PHP on Ubuntu with different methods.
Prerequisites
For installing PHP on Ubuntu, you need 3 main prerequisites:
- Ubuntu system: You’ll need a computer running a recent version of Ubuntu.
- Sudo privileges: You’ll need access to an Ubuntu user account with sudo privileges to install software packages.
- Terminal access: You’ll need to be comfortable using the terminal window to execute commands during the installation process.
Install PHP with Apache on Ubuntu
Installing PHP with Apache on Ubuntu is a great way to create a local development environment. These tutorials will show you how to install PHP with Apache on your Ubuntu server. You’ll be able to choose between PHP 7.4 and 8.1.
Update and upgrade: This command updates the package lists and upgrades any existing packages on your system.
sudo apt update && sudo apt upgrade
Install PPA prerequisites: This installs the necessary tools to work with Personal Package Archives (PPAs).
sudo apt install software-properties-common
Add PHP PPA: This step provides various PHP versions.
sudo add-apt-repository ppa:ondrej/php
Update package lists (Do this again): Run this command again to ensure the package lists include the new PPA repository.
sudo a
pt update
Install Apache and PHP 7.4, 8.1
Install Apache and PHP: Choose the version you want to install by replacing `7.4` with `8.1` in the following command:
sudo apt install apache2 php<version> libapache2-mod-php<version>
For example, to install Apache and PHP 7.4:
sudo apt install apache2 php7.4 libapache2-mod-php7.4
Restart Apache: This command restarts the Apache service to activate the changes.
sudo systemctl restart apache2
Verify PHP Installation
Verify PHP Installation: There are two ways to verify the installation:
Firs way: Check version, which displays the installed PHP version.
php -v
Second way: Creating a test page. This way you should create a file named `info.php` in your web server’s document root (usually `/var/www/html`). You can use a text editor like `nano`:
sudo nano /var/www/html/info.php
Paste the following code into the file:
<?php
phpinfo();
?>
If the version 7.4 is installed successfully, you’ll see something like the following image:
On the other hand, if you have installed version 8.1, the version will show:
How to uninstall PHP on Ubuntu?
Uninstalling PHP on Ubuntu is a useful skill in maintaining a server. It frees up disk space, increases security by deleting unwanted applications, and helps with debugging issues or replacing web frameworks. Even if you don’t want to uninstall right now, understanding the process allows you to make better decisions about your server’s resource allocation and functioning.
Uninstall PHP 7.4 on Ubuntu
To uninstall PHP 7.4 on your Ubuntu system, run the following command. It will remove the core PHP package:
sudo apt remove --purge php7.4
Now, if you already have installed PHP-FPM, use the command below to uninstall it:
sudo apt remove --purge php7.4-fpm
At last, you need to remove the PHP configuration files. To do that, run the following command:
sudo rm -rf /etc/php/7.4
Uninstall PHP 8.1 on Ubuntu
The process of uninstalling PHP 8.1 is the same as uninstalling PHP 7.4. To remove PHP 8.1 from your Ubuntu system:
- Remove the core package: sudo apt remove –purge php8.1
- Remove PHP-FPM: sudo apt remove –purge php8.1-fpm
- Delete configuration files: sudo rm -rf /etc/php/8.1
(Note: This step is optional. Using this command, the configuration directory specifically for PHP 8.1 will be removed. If you’re sure you won’t need PHP 8.1 again, we recommend using this command.)
PHP on Ubuntu PHP on Ubuntu PHP on Ubuntu