Installing PHP on Ubuntu is essential for web developers, especially beginners working with dynamic web applications. PHP is the most popular language and is widely used for web development. Here’s an in-depth guide on how to install PHP on Ubuntu.
Prerequisites for Installing the PHP on Ubuntu.
- VPS: Get Powerful VPS Hosting: Visit HostBet.in
- Access to your VPS: You should have the IP address and password provided by HostBet. (Once you purchase a VPS hosting plan from any VPS hosting provider, you’ll typically receive an email with the IP address, password, and other details:)
- SSH client: A tool like PuTTY or Terminal (on macOS/Linux) to connect to your VPS. How to Connect to Your VPS Hosting via SSH/PuTTY
Remember that before installing any components, such as Linux, Apache, MySQL, or PHP, always start by updating your package index to ensure you are getting the latest version of PHP.
Update Package Lists:
To update packages, you need to ensure you have the latest packages. If not, then you run the following command to update:
sudo apt update
You can install PHP on Ubuntu using either of these commands, depending on your specific need:
Option:- 1
sudo apt install PHP
This command installs the PHP interpreter itself because it is essential for running PHP scripts on your system.
Option:- 2
sudo apt install php libapache2-mod-php
Using this command, your system will run a PHP script on a web server using Apache, ensuring smooth website functionality.
Note: The second command is generally preferred if you plan to use PHP for web development with Apache.
Verify PHP Installation
Once you complete installation, check the PHP version to confirm it’s installed correctly.
Command: php -v
Configure Apache to Use PHP
Once you complete verifications, to ensure Apache serves PHP files, you need to configure it to prioritize PHP files over others. Open the dir.conf file with a text editor.
sudo nano /etc/apache2/mods-enabled/dir.conf
Modify the DirectoryIndex directive so index.php appears before index.html.
<IfModule mod_dir.c>
DirectoryIndex index.php index.html
</IfModule>
Next, save the file and exit the text editor. And restart Apache to apply the changes. Follow the below command to restart.
Testing PHP
You need to create a simple PHP file named info.php in your web server’s document root (usually /var/www/html):
Add the following PHP code to the file:
<?php
phpinfo();
?>
Once you type code, save and exit the text editor. Now open your web browser and navigate to http://your_server_ip/info.php.
Install Additional Modules
Depending on your specific requirements, you might need additional extensions; you can easily install them using the following command format:
sudo apt install php-<module-name>
For example, we will install the MySQL extension, which allows PHP to interact with MySQL databases.
sudo apt install php-mysql
Final Thought on Install PHP on Ubuntu
Congratulations! You’ve now successfully installed PHP on your Ubuntu VPS. This comprehensive setup guarantees that you’re ready to develop robust web apps. You can now start creating dynamic web applications using PHP.


