PHP (Hypertext Preprocessor) is a popular server-side scripting language used in web development. It is widely used for building dynamic web pages and web applications. In this blog post, we’ll guide you through the steps of installing and uninstalling PHP on an Ubuntu system.
Prerequisites
Before we start, make sure you have the following:
- A running instance of Ubuntu (version 20.04, 22.04, or later).
- Root or sudo privileges to install and manage packages.
Step 1: Update Your Package Manager
Before installing any software, it’s a good practice to update your package index to ensure you’re getting the latest available packages.
Open your terminal and run the following command:
sudo apt update
This command updates the local package database with the latest information from the repositories.
Step 2: Install PHP
To install PHP on Ubuntu, follow these steps:
Option 1: Install PHP from the Default Ubuntu Repositories
The simplest way to install PHP is from Ubuntu’s default repositories. This method will install the stable version of PHP available in Ubuntu’s repository.
sudo apt install php
This will install the latest PHP version available in your Ubuntu repository, typically PHP 7.x or 8.x depending on your version of Ubuntu.
Option 2: Install a Specific PHP Version
If you want to install a specific version of PHP, for instance, PHP 8.0, you can use apt
with the specific version number:
sudo apt install php8.0
To confirm the installation and check the installed PHP version, use:
php -v
You should see an output similar to:
PHP 8.0.20 (cli) (built: Sep 10 2023 15:00:00) ( NTS )
Step 3: Install PHP Extensions
To enhance the functionality of PHP, you may need to install additional extensions. You can search for available PHP extensions using the following command:
apt search php | grep php-
To install a specific extension, use:
sudo apt install php-<extension-name>
For example, to install the php-mysql
extension to enable MySQL database support:
sudo apt install php-mysql
You can install multiple extensions at once by separating them with spaces:
sudo apt install php-curl php-json php-mbstring
Common PHP Extensions:
php-mysql
: MySQL supportphp-curl
: Curl supportphp-json
: JSON supportphp-mbstring
: Multi-byte string functionsphp-xml
: XML parsing
Step 4: Verify Installation
Once you’ve installed PHP and any necessary extensions, you should verify the installation by creating a PHP file to test the server’s ability to execute PHP scripts.
Step 4.1: Create a Test PHP File
Navigate to your web server’s root directory:
cd /var/www/html/
Create a new PHP file named info.php
:
sudo nano info.php
Add the following code to the file:
phpinfo();
Save and close the file (Ctrl + O, then Enter to save, and Ctrl + X to exit the editor).
Step 4.2: Access the File in a Web Browser
Open a web browser and go to:
http://your_server_ip/info.php
You should see a page with detailed information about your PHP installation, including the installed modules and configuration settings.
Step 5: Uninstall PHP
If you ever need to uninstall PHP from your system, follow these steps.
Uninstall PHP and Related Packages
To uninstall PHP and all its extensions, use the following command:
sudo apt purge php*
The purge
command will remove all packages that start with php
, including any related configuration files.
After the purge is complete, you can remove any residual packages that are no longer needed by running:
sudo apt autoremove
This command cleans up any leftover dependencies that were installed with PHP but are no longer required.
Remove PHP Configuration Files
In some cases, you may want to ensure that all configuration files related to PHP are removed. You can do this manually by deleting the configuration directory:
sudo rm -rf /etc/php/
This will delete all PHP-related configuration files.
Conclusion
In this guide, we covered the steps to install and uninstall PHP on Ubuntu. Whether you’re setting up a LAMP stack, working with a CMS like WordPress, or building custom web applications, PHP is a versatile and essential tool. You can easily install it using apt
, customize it with extensions, and remove it when it’s no longer needed.
Remember to keep your PHP version up-to-date and secure by regularly updating your package manager and applying security patches.
Happy coding!
Further Reading
Let us know if you encounter any issues or have any questions in the comments below!
WARP+ by Cloudflare: Boosting Internet Speed and Security on the Go (F.A.Q)
How do I check my PHP version after installation?
You can check your installed PHP version by running the following command in the terminal: php -v
Can I install multiple PHP versions on the same Ubuntu server?
Yes, you can install multiple PHP versions and switch between them using tools like update-alternatives
or by configuring your web server (Apache/Nginx) to use a specific PHP version.
How do I restart my web server after installing PHP?
For Apache:
sudo systemctl restart apache2