PHP (Hypertext Preprocessor) is one of the most popular server-side scripting languages used to develop dynamic and interactive websites. If you’re starting with web development on Ubuntu, installing PHP is one of the first steps you’ll need to take. Whether you’re setting up a LAMP stack for web development or configuring a server to run PHP-based applications, this guide will walk you through the process how to install PHP on Ubuntu.
In this guide, we will cover everything from installing the latest version of PHP to configuring it with a web server such as Apache or Nginx, ensuring you’re fully equipped to start developing with PHP on Ubuntu. By the end, you will have a fully functioning PHP environment on your Ubuntu system, and you’ll be ready to create dynamic websites and web applications.
Why Install PHP on Ubuntu?
PHP is widely used for web development and has a large ecosystem of frameworks and tools, making it a go-to choice for many developers. It powers millions of websites and content management systems like WordPress, Joomla, and Drupal.
Some key reasons to install PHP on Ubuntu include:
- Building Dynamic Websites: PHP allows you to build interactive and data-driven websites by processing user inputs, interacting with databases, and serving content dynamically.
- Integration with Databases: PHP works seamlessly with MySQL, PostgreSQL, and other databases, making it perfect for developing content-heavy websites or applications.
- Broad Community and Documentation: PHP’s popularity means it has a vast community, comprehensive documentation, and numerous tutorials that make troubleshooting and development much easier.
- Compatibility with Popular Frameworks: Frameworks like Laravel, Symfony, and CodeIgniter are all built around PHP, giving you access to ready-made solutions for various types of projects.
Prerequisites for Installing PHP on Ubuntu
Before installing PHP, make sure you have the following prerequisites:
- Ubuntu system: This guide applies to Ubuntu 18.04, 20.04, and later versions.
- Root or Sudo privileges: You’ll need administrative access to install packages on your system.
- Internet connection: You’ll need an internet connection to download the PHP packages.
Let’s begin by ensuring your system is up-to-date.

Step 1: Update Your Ubuntu System
Before installing any new packages, it’s important to update your system to the latest version. This ensures that you have the latest security patches and software updates.
- Open a terminal on your Ubuntu system.
- Run the following commands to update your package list and upgrade your existing packages:
sudo apt update
sudo apt upgrade
- After the update finishes, it’s a good practice to clean up unnecessary packages:
sudo apt autoremove
Once your system is updated, you’re ready to install PHP.
Step 2: Install PHP on Ubuntu
There are several ways to install PHP on Ubuntu, depending on your specific needs. You can install the default version of PHP available in the official Ubuntu repositories or you can install a newer version directly from the Ondřej Surý PPA repository, which provides more up-to-date PHP versions.
Installing PHP from Ubuntu’s Default Repository
Ubuntu repositories typically contain stable versions of PHP. To install PHP from the official Ubuntu repository:
- Install PHP with the following command:
sudo apt install php
This will install the default PHP version available for your Ubuntu version. To check which version of PHP is installed, run:
php -v
You should see something like this:
PHP 7.4.3 (cli) (built: Feb 18 2020 09:55:40) ( NTS )
Installing a Specific Version of PHP
If you need a newer version of PHP (e.g., PHP 8.0 or PHP 8.1), you can add the Ondřej Surý PPA repository, which provides the latest versions of PHP for Ubuntu.
- First, add the repository to your system:
sudo add-apt-repository ppa:ondrej/php
- Update your package list again:
sudo apt update
- Now, you can install a specific version of PHP. For example, to install PHP 8.0:
sudo apt install php8.0
To check the installed version:
php -v
This will display the version of PHP that you just installed.
Step 3: Install PHP Extensions
PHP extensions are libraries that extend the core functionality of PHP. Depending on the application you’re working with, you may need to install certain extensions.
For example, if you are working with a MySQL database, you’ll need the php-mysql
extension. To install commonly used PHP extensions, run the following command:
sudo apt install php-mysql php-cli php-curl php-xml php-mbstring php-zip
This will install the MySQL, CLI, cURL, XML, mbstring, and Zip extensions. You can install other PHP extensions as needed by replacing the package names.
To check which extensions are installed, run:
php -m
This will list all the installed PHP extensions.
Step 4: Install and Configure Apache Web Server with PHP
Now that PHP is installed on your system, you might want to set it up to run with a web server. The most common web server for PHP is Apache.
Install Apache
If Apache is not already installed, you can install it using the following command:
sudo apt install apache2
Once Apache is installed, you can verify the installation by accessing your server’s IP address or localhost
in a browser. You should see the Apache2 Ubuntu default page.
Install PHP with Apache
To ensure that Apache can process PHP files, you need to install the Apache PHP module:
sudo apt install libapache2-mod-php
After the module is installed, restart Apache to load the PHP module:
sudo systemctl restart apache2
Test PHP with Apache
To test if PHP is working with Apache, create a new PHP file in the /var/www/html
directory:
sudo nano /var/www/html/info.php
Add the following content to the file:
<?php
phpinfo();
?>
Save and close the file. Then, navigate to http://localhost/info.php
in your browser. You should see the PHP info page, which displays information about your PHP installation.
Once you’re done testing, don’t forget to delete the info.php
file for security reasons:
sudo rm /var/www/html/info.php
Step 5: Install and Configure Nginx with PHP
If you prefer using Nginx over Apache, you can also configure Nginx to run PHP. Here’s how to set it up.
Install Nginx
To install Nginx, run:
sudo apt install nginx
Once Nginx is installed, start it:
sudo systemctl start nginx
You can check if Nginx is working by accessing your server’s IP or localhost
in a web browser. You should see the default Nginx page.
Install PHP-FPM (FastCGI Process Manager)
Nginx does not have native support for PHP, so you need to install PHP-FPM (FastCGI Process Manager) to handle PHP requests:
sudo apt install php-fpm
After installation, check that PHP-FPM is running:
sudo systemctl status php8.0-fpm
Configure Nginx to Use PHP-FPM
Next, configure Nginx to pass PHP requests to the PHP-FPM service. Open the default Nginx server block configuration file:
sudo nano /etc/nginx/sites-available/default
Find the section that looks like this:
server {
...
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
...
}
Make sure that the fastcgi_pass
directive points to the correct PHP-FPM socket.
Once done, save the file and restart Nginx:
sudo systemctl restart nginx
Test PHP with Nginx
As with Apache, create a info.php
file in the /var/www/html
directory to test PHP:
sudo nano /var/www/html/info.php
Add the following content:
<?php
phpinfo();
?>
Visit http://localhost/info.php
in your browser. You should see the PHP info page, confirming that PHP is working with Nginx.
Conclusion
You’ve successfully install PHP on Ubuntu and configured it to work with both Apache and Nginx web servers. Whether you’re developing a simple website or building a complex web application, PHP is a versatile and powerful language for web development. With the knowledge from this guide, you now have the tools to start creating dynamic web applications and exploring the rich ecosystem of PHP frameworks and libraries.
Did you find this guide helpful? If you have any questions or run into issues while installing PHP on Ubuntu, leave a comment below, and we’ll be happy to assist you. Don’t forget to read our other article on Install Tips and Tricks. Happy Installation!
Read Also : How to Install Maven on Ubuntu?