In this article we are going to cover How to install PHP on Ubuntu 20.04 LTS and run PHP code on Apache web server.

What is Apache Web Server ?

Apache is the most widely used webserver software and runs on 67% of all websites in the world. Developed and maintained by Apache Software Foundation, Apache is open source software and available for free. It’s fast, reliable, and secure. And Apache can be highly customized to meet the needs of many different environments by using extensions and modules.

What is PHP?

PHP, which stands for “Hypertext Preprocessor,” is a popular server-side scripting language used in web development. It is embedded within HTML to create dynamic web pages, processing data and interacting with databases on the server.

Steps to Install php on ubuntu 20.04/22.04 LTS

Let’s see the steps of installation of php on ubuntu server.

Step#1: Installing Apache

To install Apache, install the latest meta-package apache2 by running:

ubuntu@RushiInfotech:~$ sudo apt-get update
ubuntu@RushiInfotech:~$ sudo apt install apache2

After letting the command run, all required packages are installed and we can test it out by typing in our IP address for the web server.

If you see the page above, it means that Apache has been successfully installed on your server! Let’s move on.

Step#2: Installation of PHP

PHP is available on Ubuntu Linux, but unlike Python (which comes pre-installed), must be manually installed.

To install PHP – and the Apache PHP module – you can enter the following command into a terminal prompt:

ubuntu@RushiInfotech:~$ sudo apt install php libapache2-mod-php

Step#3: Configuration of PHP

If you have installed the ‘libapache2-mod-php’ or ‘php-cgi’ packages, you can run PHP scripts from your web browser.

By default, when libapache2-mod-php is installed, the Apache2 web server is configured to run PHP scripts using this module. First, verify if the files ‘/etc/apache2/mods-enabled/php8.*.conf’ and ‘/etc/apache2/mods-enabled/php8.*.load’ exist. If they do not exist, you can enable the module using the a2enmod command.

ubuntu@RushiInfotech:~$ sudo systemctl restart apache2.service 

Step#4: Test PHP code on Apache Web Server

Create a PHP test file to verify that PHP is working properly. In this example, we’ll create a file named info.php

ubuntu@RushiInfotech:~$ cd /var/www/html
ubuntu@RushiInfotech:/var/www/html$ sudo touch info.php
ubuntu@RushiInfotech:/var/www/html$ sudo nano info.php

Add the following content to the ‘info.php’ file:

<?php
phpinfo();
?>

Step#5: Access the PHP Info Page on Browser

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 PHP info page displaying information about your PHP configuration. This page can help you verify that PHP is installed and configured correctly.

Output

Conclusion:

In this article, we’ve provided a comprehensive guide to installing PHP on Ubuntu 20.04 LTS and configuring it to run PHP code seamlessly on an Apache web server. These steps enable users to establish a robust web development environment with ease. By following these instructions, you can quickly set up a functional PHP environment on your Ubuntu server.

Reference:

For reference on installation of php on ubuntu please visit official website.

Any queries pls contact us @Rushi Infotech

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *