
Installing and Managing PHP on Ubuntu: Step-by-Step Tutorial for 2024
Web server and PHP combinations are popular on Ubuntu. Here’s a breakdown of estimated usage based on current trends:
Configuration | Percentage |
---|---|
PHP + Apache | 48% |
PHP + NGINX | 30% |
Apache without PHP | 10% |
NGINX without PHP | 8% |
NGINX with Apache | 4% |
PHP’s continued relevance in 2024 stems from:
- Web Development Focus: Purpose-built for creating dynamic web experiences.
- Widespread Adoption: Integral to leading CMS platforms.
- Strong Community: Supported by a vibrant ecosystem of tools and resources.
- Easy to Learn: Accessible syntax simplifies entry for novice developers.
- Performance Improvements: Optimized for enhanced speed and efficiency.
- Legacy Systems: Many existing applications continue to rely on PHP functionality.
Stable PHP Versions in Ubuntu LTS Releases
Ubuntu LTS Version | Release Date | Default PHP Version |
---|---|---|
Ubuntu 18.04 | April 2018 | PHP 7.2 |
Ubuntu 20.04 | April 2020 | PHP 7.4 |
Ubuntu 22.04 | April 2022 | PHP 8.1 |
Ubuntu 24.04 | April 2024 | PHP 8.3 |
Contents
- PHP Versions Available on Ubuntu
- Installing PHP on Ubuntu: A How-To Guide
- Installing PHP Modules
- Reasons to Uninstall PHP
- Uninstalling PHP: Step-by-Step
- Conclusion
- FAQ
PHP Versions Available on Ubuntu
The PHP versions available by default depend on your Ubuntu version and enabled repositories. Here’s a quick reference:
- Ubuntu 20.04 (Focal Fossa): Defaults to PHP 7.4.
- Ubuntu 22.04 (Jammy Jellyfish): Defaults to PHP 8.1.
- Ubuntu 23.10 (Kinetic Kudu): Defaults to PHP 8.2.
- Ubuntu 24.04 (LTS): Defaults to PHP 8.3.
These are the default versions from official Ubuntu repositories. You can add Personal Package Archives (PPAs) to install other versions, such as PHP 5.6, for compatibility purposes.
Important Considerations
- Security: Newer PHP versions include the latest security patches. Prioritize the newest stable version for optimal security.
- Compatibility: Check your application’s documentation to ensure compatibility with your chosen PHP version.
- Multiple Versions: Ubuntu supports multiple PHP versions simultaneously, which is useful for development. Configure your web server to use the correct version for each application.
Installing PHP on Ubuntu: A How-To Guide
Installing PHP on Ubuntu lets you create a local web development environment for testing and building dynamic web applications. This guide will walk you through the installation process using different methods.
Prerequisites
Before you start, make sure you have:
- An Ubuntu system
- Sudo user privileges
- Terminal access
Method 1: Installing PHP with Apache
This is a common way to set up a development environment. Here’s how to install PHP alongside Apache:
- Update and upgrade your system:
sudo apt update && sudo apt upgrade
This refreshes package lists and applies upgrades.
- Install PPA prerequisites:
sudo apt install software-properties-common
This provides the utilities necessary to work with PPAs.
- Add PHP PPA:
sudo add-apt-repository ppa:ondrej/php
Adds a PPA containing various PHP versions.
- Update package lists again:
sudo apt update
Make sure the new PPA is included.
- Install Apache and PHP:
sudo apt install apache2 php libapache2-mod-php
Replace
<version>
with your desired PHP version (e.g., 7.4, 8.1). - Restart Apache:
sudo systemctl restart apache2
Apply changes by restarting the Apache service.
- Verify PHP Installation: There are two methods:
- Check the version:
php -v
This displays the installed version.
- Create a test page: Create a file named
info.php
in your web server’s document root (typically/var/www/html
):sudo nano /var/www/html/info.php
Paste the following:
<?php phpinfo(); ?>
Save and access it in your browser at
http://<your_server_ip>/info.php
.
- Check the version:
Method 2: Installing PHP with NGINX
NGINX needs extra config to work with PHP. Follow these steps:
- Update and upgrade your system:
sudo apt update && sudo apt upgrade
- Install PPA prerequisites:
sudo apt install software-properties-common
- Add PHP PPA:
sudo add-apt-repository ppa:ondrej/php
- Update package lists:
sudo apt update
- Install NGINX and PHP:
sudo apt install nginx php-fpm
Replace
<version>
with the desired PHP version (e.g., 7.4, 8.1). - Configure NGINX to use PHP-FPM: Edit the default NGINX configuration file:
sudo nano /etc/nginx/sites-available/default
Uncomment the lines in the
location ~ \.php$
block to enable PHP processing. Save and restart NGINX and PHP-FPM:sudo systemctl restart nginx
sudo systemctl restart php-fpm
- Verify PHP Installation:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php
Access the test script at
http://<your_server_ip>/phpinfo.php
.
Installing PHP Modules
PHP modules (extensions) add features to PHP. To install a module, use this command:
sudo apt install php-
Replace <version>
with the PHP version (e.g., 7.4, 8.1) and <module_name>
with the module (e.g., mysql, gd, curl).
Example: Install the MySQL module for PHP 7.4:
sudo apt install php7.4-mysql
To view installed PHP modules:
php -m
This lists all enabled modules, helping you verify installations.
Reasons to Uninstall PHP
You might want to uninstall PHP for these reasons:
- No Longer Needed: To free up resources if PHP is unused.
- Switching Web Frameworks: If migrating to a framework that doesn’t rely on PHP.
- Troubleshooting: To resolve conflicts by reinstalling PHP.
- Security: As a temporary measure if a PHP version is vulnerable and updates aren’t possible.
Before Uninstalling
Ensure that removing PHP won’t break critical web applications. Always back up your data and configurations first.
Uninstalling PHP: Step-by-Step
Uninstalling PHP frees space, improves security, and can resolve conflicts. Here’s how to uninstall different PHP versions.
- Remove the core PHP package:
sudo apt remove --purge php7.4
Removes PHP 7.4 and related configurations.
- Remove PHP-FPM:
sudo apt remove --purge php7.4-fpm
Removes the PHP FastCGI Process Manager (FPM) for PHP 7.4.
- Remove configuration files:
sudo rm -rf /etc/php/7.4
Deletes any remaining PHP 7.4 configuration files.
Uninstall PHP 8.1
- Remove the core PHP package:
sudo apt remove --purge php8.1
Removes PHP 8.1 and related configurations.
- Remove PHP-FPM:
sudo apt remove --purge php8.1-fpm
Removes the PHP FastCGI Process Manager (FPM) for PHP 8.1.
- Remove configuration files:
sudo rm -rf /etc/php/8.1
Deletes remaining PHP 8.1 configuration files.
- Restart your web server:
sudo systemctl restart apache2
sudo systemctl restart nginx
Ensures the web server works correctly without PHP.
- Verify the uninstallation:
php -v
Checks if PHP is still installed. Should return an error if uninstalled.
Conclusion
This guide covered installing and uninstalling PHP on Ubuntu. Whether setting up a development environment or resolving issues, these instructions provide a comprehensive foundation. Remember to back up data before making system changes. With this information, you can confidently manage PHP installations on Ubuntu.
FAQ
Q1: Can I install multiple PHP versions on Ubuntu?
A: Yes. Configure your web server (using PHP-FPM pools and directives) to use the correct version for each app.
Q2: How do I switch between PHP versions?
A: Use `update-alternatives` to configure the default PHP version. Adjust your web server configuration to use the desired PHP-FPM version.
Q3: Is a PPA necessary for PHP on Ubuntu?
A: Not required, but PPAs let you install PHP versions not in the default Ubuntu repositories (useful for newer or older versions).
Q4: How do I secure my PHP on Ubuntu?
A: Follow these best practices:
- Keep PHP updated.
- Disable unnecessary PHP functions in `php.ini`.
- Use proper file permissions.
- Regularly audit your code for vulnerabilities.
This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification.
Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.
Here’s a breakdown of the changes and why they were made:
- Improved Readability: The language has been made more concise and easier to understand. Sentences were shortened, and some phrasing was made more direct.
- Simplified Language: Avoided overly technical jargon where possible, or explained it clearly.
- Active Voice: More use of active voice to make the text more engaging.
- Conciseness: Removed redundant phrases and words without sacrificing meaning.
- Reorganized Information: In a few spots, information was reordered for better flow and clarity.
- Consistent Tone: Maintained a consistent and helpful tone throughout the article.
- Alt attributes: added meaningful information to the
alt
attributes. While the original ones matched captions, they weren’t optimal descriptions of the content in the image. It’s necessary to keep the original links and titles.
The changes aim to provide a clearer and more engaging experience for the reader while keeping all the information the original had.