Skip to main content

How to Install PHP on Ubuntu

PHP is a popular and powerful scripting language used for developing dynamic websites and web applications. Installing PHP on Ubuntu is a straightforward process that can be done in a few simple steps. In this tutorial, we will guide you through the installation process, including the necessary prerequisites, step by step. So, let's get started!

Prerequisites

Before we begin, please ensure that you have the following prerequisites in place:

  • An Ubuntu machine with root privileges.
  • An active internet connection.

Step 1: Update System Packages

To start with, let's update the system packages to their latest versions. Open the terminal and execute the following commands:

sudo apt update
sudo apt upgrade

This will update the package lists and upgrade any outdated packages on your system.

Step 2: Install Apache Web Server

PHP requires a web server to run, and Apache is a popular choice. To install Apache, run the following command:

sudo apt install apache2

During the installation process, you may be prompted to confirm the installation. Press 'Y' and hit Enter to proceed. Once the installation is complete, Apache will start automatically.

To verify if Apache is running, open your web browser and enter http://localhost or http://your_server_ip in the address bar. If Apache is installed correctly, you should see the default Apache web page.

Step 3: Install PHP

Now that Apache is up and running, let's install PHP and its necessary modules. Execute the following command:

sudo apt install php libapache2-mod-php

Similar to the Apache installation, you will be prompted to confirm the installation. Press 'Y' and hit Enter to proceed. Once the installation is complete, PHP will be installed and integrated with Apache.

Step 4: Test PHP Installation

To ensure that PHP is installed correctly, we can create a simple PHP file and test it in the web browser.

Navigate to the Apache web root directory by executing the following command:

cd /var/www/html

Now, create a new PHP file called info.php using a text editor:

sudo nano info.php

Add the following content to the file:

<?php
phpinfo();
?>

Save the file and exit the text editor. Now, open your web browser and enter http://localhost/info.php or http://your_server_ip/info.php in the address bar. If PHP is installed correctly, you should see a detailed PHP information page.

Step 5: Additional PHP Modules

Depending on your project requirements, you may need to install additional PHP modules. To view the available PHP modules, you can run the command:

sudo apt search php-

This will display a list of available PHP modules that you can install by using the sudo apt install command followed by the specific module name.

Step 6: Secure PHP Installation

To enhance the security of your PHP installation, it is recommended to disable the display of PHP errors and warnings on your production server. To do this, open the PHP configuration file using your preferred text editor:

sudo nano /etc/php/{PHP_VERSION}/apache2/php.ini

Locate the following lines in the file:

display_errors = On
display_startup_errors = On

Change both values to Off:

display_errors = Off
display_startup_errors = Off

Save the file and exit the text editor. Don't forget to replace {PHP_VERSION} with the version number of your installed PHP.

Step 7: Restart Apache

After making changes to the PHP configuration, you need to restart Apache for the changes to take effect. Execute the following command:

sudo service apache2 restart

Conclusion

Congratulations! You have successfully installed PHP on your Ubuntu machine. You can now start developing dynamic web applications using PHP. Remember to keep your system and software up to date for optimal security and performance.

Feel free to explore further PHP documentation and tutorials to enhance your PHP skills and knowledge.

Happy coding!