Home > Learn > Server > Installing PHP on Ubuntu 20.04 and 22.04: A Comprehensive Guide
Understanding PHP and Its Importance
PHP, which stands for Hypertext Preprocessor, is a widely used open-source scripting language that is especially suited for web development and can be embedded into HTML. As a server-side language, PHP is executed on the server, generating HTML that is then sent to the client. This makes it a powerful tool for creating dynamic and interactive web pages.
This article is part of Rackset’s Server guides — browse the rest of the category for more in-depth tutorials on this topic.
The Role of PHP in Web Development
PHP is integral to the development of a wide variety of web-based applications. Its versatility and ease of integration with databases make it a preferred choice for building:
- E-commerce Sites: Platforms like Magento and WooCommerce use PHP to manage online stores efficiently.
- Blogs: Content management systems such as WordPress are built on PHP, allowing for dynamic content updates.
- APIs: PHP facilitates the creation of RESTful APIs, enabling seamless communication between different software applications.
- Other Web Applications: From social networking sites to online forums, PHP is used in a variety of applications due to its flexibility.
Why Install the Latest PHP Version?
Keeping PHP updated is crucial for several reasons:
- Security: Newer versions of PHP include patches for known vulnerabilities, making your applications more secure.
- Performance: Each new release of PHP typically includes performance improvements that can make your applications run faster.
- New Features: Up-to-date PHP versions offer new functionalities that can simplify coding and enhance application capabilities.
- Compatibility: Many modern frameworks and libraries require the latest PHP versions to function properly.
PHP Versions and Compatibility
As of the date of writing, PHP 7.4 and 8.1 are among the most popular and supported versions. When setting up a server, particularly on Ubuntu 20.04 or 22.04, it is advisable to install these versions to ensure compatibility with a wide range of applications and frameworks.
Using the apt package manager on Ubuntu, you can easily install PHP. The command:
sudo apt install php
installs PHP 7.4 on Ubuntu 20.04 and PHP 8.1 on Ubuntu 22.04 by default. This ensures that you are equipped with a stable and supported PHP environment for your development needs.
Preparing Your Ubuntu System for PHP Installation
Before installing PHP on your Ubuntu system, it is essential to ensure that your environment is ready. This involves updating your package lists, installing necessary dependencies, and configuring your system to handle PHP installations smoothly. Follow these steps to prepare your Ubuntu system for PHP installation on both Ubuntu 20.04 and 22.04.
Update Package Lists
Begin by updating your package lists to ensure you have the latest information about available packages. This step is crucial for avoiding potential issues with outdated packages.
sudo apt update
Upgrade Existing Packages
Next, upgrade your existing packages to their latest versions. This ensures that your system is running the most up-to-date software, which can help prevent compatibility issues during the PHP installation.
sudo apt upgrade
Install Software Properties Common
The software-properties-common package provides an abstraction of the used apt repositories. It is necessary for adding additional repositories, such as the PHP PPA (Personal Package Archive) when installing different PHP versions.
sudo apt install software-properties-common
Add the PHP PPA
To install the latest PHP versions, you may need to add the PHP PPA maintained by Ondřej Surý. This repository contains multiple PHP versions and is widely trusted in the Ubuntu community.
sudo add-apt-repository ppa:ondrej/php
After adding the PPA, update your package list again to include the packages from this new repository.
sudo apt update
Install Required Dependencies
Ensure that you have all the necessary dependencies installed. These dependencies are required for compiling and running PHP smoothly on your system.
sudo apt install -y libapache2-mod-php php-mysql php-cli php-curl php-zip php-xml php-mbstring
Verify Your System Configuration
Finally, verify that your system is configured correctly and that the necessary components are ready for PHP installation. This includes checking that your web server (e.g., Apache or Nginx) is installed and configured correctly if you plan to use PHP for web applications.
With these preparatory steps completed, your Ubuntu system is now ready for PHP installation. Proceed to install the desired PHP version and start developing your web-based applications.
Installing PHP on Ubuntu with Apache
PHP is a powerful scripting language widely used for developing web applications. When hosting applications on an Apache server on Ubuntu, it is crucial to ensure PHP is properly installed and configured. This guide will walk you through the installation of PHP on Ubuntu 20.04 and 22.04 using Apache.
Step 1: Update Package Index
Before installing PHP, it is advisable to update the package index to ensure apt retrieves the latest package information:
sudo apt update
Step 2: Install Apache
If you haven’t already installed Apache, you can do so with the following command:
sudo apt install apache2
After installation, enable and start the Apache service:
sudo systemctl enable apache2
sudo systemctl start apache2
Step 3: Install PHP
To install PHP, use the apt package manager. By default, Ubuntu 20.04 installs PHP 7.4, while Ubuntu 22.04 installs PHP 8.1:
Read more: Installing Apache on Ubuntu 22.04: A Step-by-Step Guide
If you need this service, Rackset’s team provides professional Linux VPS with full support so you can rely on the quality and get set up quickly.
sudo apt install php
To verify the installation and check the PHP version, execute:
php -v
Installing a Specific PHP Version
If you need a specific PHP version, such as PHP 8.0, you can add the ondrej/php PPA repository:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.0
Step 4: Install PHP Modules
PHP modules add additional functionality to PHP. You can install common modules with:
sudo apt install php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-bcmath
Step 5: Restart Apache
After installing PHP and its modules, restart Apache to apply the changes:
sudo systemctl restart apache2
Step 6: Test PHP Processing
Create a PHP info file to test PHP processing with Apache. Create a file named info.php in the /var/www/html directory:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Visit http://your_server_ip/info.php in your web browser. You should see a page displaying detailed information about your PHP installation, confirming that PHP is working correctly with Apache.
By following these steps, you can ensure that PHP is installed and configured properly on your Ubuntu system with Apache, allowing you to run PHP-based applications efficiently.
Installing PHP on Ubuntu with Nginx
To run PHP applications with Nginx on Ubuntu, you need to install PHP along with some additional packages that enable Nginx to communicate with PHP. This process involves setting up PHP-FPM (FastCGI Process Manager), which is the preferred method for using PHP with Nginx.
Steps to Install PHP with Nginx on Ubuntu 20.04 and 22.04
-
Update Package List
Before installing PHP, it’s a good practice to update the package list to ensure you have the latest information about available packages:
sudo apt update -
Install PHP and PHP-FPM
Install PHP along with PHP-FPM to handle PHP requests:
sudo apt install php-fpmThis command installs the default PHP version for your Ubuntu release. For Ubuntu 20.04, it installs PHP 7.4, and for Ubuntu 22.04, it installs PHP 8.1.
-
Install Additional PHP Modules
Depending on your application’s requirements, you may need additional PHP modules. You can install them using:
sudo apt install php-mysql php-xml php-mbstringReplace the module names with those required by your application.
-
Configure Nginx to Use PHP Processor
Edit the Nginx server block file to process PHP files. Open the server block file (e.g.,
/etc/nginx/sites-available/default) and ensure the following lines are included:server { ... location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # For PHP 8.1, use php8.1-fpm.sock } ... }Adjust the PHP-FPM socket path according to the PHP version installed.
-
Test Nginx Configuration
Check the Nginx configuration for syntax errors:
sudo nginx -t -
Restart Nginx
To apply the changes, restart Nginx:
sudo systemctl restart nginx
After completing these steps, your Nginx server should be ready to serve PHP applications. Ensure that your PHP application files are placed in the web root directory (e.g., /var/www/html) and accessible by Nginx.
Managing Multiple PHP Versions on Ubuntu
Managing multiple PHP versions on Ubuntu is a common requirement for developers who need to support different applications with varying PHP version dependencies. Ubuntu makes it straightforward to install and switch between multiple PHP versions using the command line.
Read more: Installing Let’s Encrypt SSL on Ubuntu 22.04
Installing Multiple PHP Versions
To manage multiple PHP versions, you will first need to ensure that you have the necessary PHP versions installed. You can use the following commands to install PHP 7.4 and PHP 8.1:
For projects that need guaranteed resources and full root control, a VPS is the right step up from shared hosting. With VPS hosting you get dedicated CPU/RAM, NVMe storage, a dedicated IP and high uptime, deployed in minutes.
sudo apt update
sudo apt install php7.4 php8.1
This command installs both PHP 7.4 and PHP 8.1 on your system. You can verify the installation by checking the versions:
php7.4 -v
php8.1 -v
Switching Between PHP Versions
Once you have multiple PHP versions installed, you can switch between them using the update-alternatives command. This allows you to set the default PHP version for the command line.
sudo update-alternatives --set php /usr/bin/php7.4
sudo update-alternatives --set php /usr/bin/php8.1
To switch to PHP 7.4, use the first command. To switch to PHP 8.1, use the second command. You can confirm the active PHP version with:
php -v
Configuring PHP for Apache or Nginx
If you are using Apache or Nginx, you will need to configure the web server to use the desired PHP version. For Apache, use the following commands to enable the appropriate PHP module:
sudo a2dismod php7.4
sudo a2enmod php8.1
sudo systemctl restart apache2
For Nginx, you will need to update the fastcgi_pass directive in your server block configuration to point to the correct PHP version’s socket or TCP/IP address.
Conclusion
Managing multiple PHP versions on Ubuntu is a straightforward process that involves installing the desired versions and configuring your system to switch between them as needed. This flexibility ensures that you can support applications with different PHP requirements on the same server.
Verifying PHP Installation and Configuration
Once you have installed PHP on your Ubuntu system, it is crucial to verify that the installation was successful and that PHP is configured correctly. This ensures that your web applications will run smoothly and without issues.
Step 1: Check PHP Version
To confirm that PHP is installed and to check the version, open your terminal and execute the following command:
php -v
This command will display the current PHP version installed on your system. You should see output similar to the following, depending on your installation:
PHP 8.1.2 (cli) (built: Jan 14 2023 10:00:00) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
Step 2: Verify PHP Configuration
To ensure that PHP is correctly configured, you can create a phpinfo() file. This file provides detailed information about your PHP installation, including configuration settings and loaded modules.
Create a phpinfo File
- Navigate to your web server’s root directory. For example, if you’re using Apache, this is typically
/var/www/html: - Create a new PHP file named
info.php: - Add the following PHP code to the file:
- Save and close the file by pressing
CTRL + X, thenY, andEnter.
cd /var/www/html
sudo nano info.php
<?php
phpinfo();
?>
Access the phpinfo Page
Open a web browser and navigate to http://your_server_ip/info.php. Replace your_server_ip with your server’s IP address. You should see a detailed page displaying PHP configuration information.
If the phpinfo page loads correctly, PHP is properly installed and configured on your Ubuntu system. Remember to delete the info.php file after verification to prevent unauthorized access to your server’s configuration details:
sudo rm /var/www/html/info.php
Troubleshooting Common PHP Installation Issues
While installing PHP on Ubuntu 20.04 and 22.04 is generally straightforward, you may encounter some common issues. This section provides solutions to these problems to ensure a smooth installation process.
Common Issues and Solutions
1. PHP Command Not Found
If you receive a “PHP command not found” error, it typically means that PHP is not installed correctly or the system path is not set. Ensure PHP is installed by running:
sudo apt install php
If PHP is installed, check the version using:
php -v
If the command is still not recognized, you may need to add PHP to your system’s PATH variable.
2. Incorrect PHP Version Installed
Sometimes, the wrong version of PHP might be installed. To check the installed version, use:
php -v
If you need to switch versions, you can do so using:
Read more: How to Install PHP on Windows 10 and 11
sudo update-alternatives --set php /usr/bin/php8.1
Replace php8.1 with your desired version.
Every website needs stable, fast hosting to stay online. Rackset’s web hosting runs on NVMe storage with high uptime and 24/7 support, giving your site a reliable foundation.
3. PHP Modules Not Loading
If certain PHP modules are not loading, you may need to install them explicitly. For example, to install the mbstring module, use:
sudo apt install php-mbstring
After installing, restart the web server:
sudo systemctl restart apache2
or
sudo systemctl restart nginx
4. Configuration File Issues
PHP configuration issues can cause various problems. The configuration file is located at /etc/php/7.4/cli/php.ini or /etc/php/8.1/cli/php.ini depending on the version. Ensure the file is correctly configured, and make necessary edits using:
sudo nano /etc/php/8.1/cli/php.ini
After making changes, restart the PHP service:
sudo systemctl restart php8.1-fpm
Further Assistance
If you continue to experience issues, consider consulting the official PHP documentation or seeking help from the Ubuntu community forums. These resources can provide additional support and guidance.
Next Steps After Installing PHP on Ubuntu
Once you have successfully installed PHP on your Ubuntu system, there are several important steps you should take to ensure your environment is properly configured and secure. These steps will help you make the most of your PHP installation and prepare your system for running PHP applications efficiently.
Verify PHP Installation
First, confirm that PHP is installed correctly by checking the version. Open your terminal and run:
php -v
This command should return the PHP version number, confirming a successful installation.
Install PHP Extensions
Depending on the applications you plan to run, you may need additional PHP extensions. Some common extensions include:
php-mysqlfor MySQL database interactionsphp-gdfor image processingphp-curlfor data transfers
Install these extensions using the following command:
sudo apt install php-mysql php-gd php-curl
Configure PHP Settings
PHP settings can be customized by editing the php.ini file. This file is typically located at /etc/php/{version}/apache2/php.ini or /etc/php/{version}/cli/php.ini. Adjust settings such as:
memory_limit– increases PHP’s memory usage limitupload_max_filesize– sets the maximum upload file sizemax_execution_time– defines the maximum time a script can run
Secure Your PHP Installation
Security is crucial for any server environment. Consider the following steps to secure your PHP installation:
- Disable unnecessary functions by editing the
disable_functionsdirective inphp.ini. - Ensure
display_errorsis set toOffin production environments to prevent sensitive information exposure. - Regularly update PHP to the latest version to patch security vulnerabilities.
Restart Web Server
After making changes to your PHP configuration or installing new extensions, restart your web server to apply these changes. Use the following command for Apache:
sudo systemctl restart apache2
Or for Nginx:
sudo systemctl restart nginx
Test PHP with a Simple Script
Create a PHP info file to test your setup. In your web server’s root directory, create a file named info.php with the following content:
<?php
phpinfo();
?>
Access this file via your web browser by navigating to http://your-server-ip/info.php. This should display the PHP configuration page, confirming that PHP is working correctly.
Supercharge Your PHP Projects
Unleash the full potential of your PHP applications with our high-performance Linux VPS. Rackset provides you with the speed and reliability you need for seamless PHP development on Ubuntu.
Elevate your coding experience with Rackset!
Conclusion
Installing PHP on Ubuntu 20.04 and 22.04 is a straightforward process that involves updating your package lists, installing PHP and its necessary extensions, and verifying the installation. With PHP successfully installed, you can now run PHP scripts and develop dynamic websites and applications on your server. Whether you are setting up a personal project or a production environment, having the latest version of PHP ensures access to the newest features and security updates.
As you continue to build and deploy your applications, remember that Rackset offers reliable web hosting solutions tailored to support your PHP projects. From shared hosting to dedicated servers, we provide the infrastructure you need to ensure optimal performance and scalability. Explore our hosting plans today and take the next step in your development journey with confidence.
Frequently Asked Questions
What are the prerequisites for installing PHP on Ubuntu 20.04 and 22.04?
Before installing PHP, ensure your system is updated. You should have root or sudo privileges, and a stable internet connection is recommended for downloading packages.
How can I install PHP on Ubuntu 20.04 and 22.04 using the terminal?
Use the command ‘sudo apt update && sudo apt install php’ to install PHP. This command updates the package list and installs the latest PHP version available in the Ubuntu repository.
How do I verify the PHP installation on Ubuntu?
After installation, verify PHP by running ‘php -v’ in the terminal. This command displays the installed PHP version and confirms a successful installation.
Can I install multiple PHP versions on Ubuntu 20.04 and 22.04?
Yes, you can install multiple PHP versions using the ‘ondrej/php’ PPA. Use ‘sudo add-apt-repository ppa:ondrej/php’ followed by ‘sudo apt update’ to access different PHP versions.
How do I switch between different PHP versions on Ubuntu?
Use the ‘update-alternatives’ command to switch PHP versions. For example, ‘sudo update-alternatives –set php /usr/bin/php7.x’ where ‘7.x’ is the desired version.

Leave a Reply