Install PHP on Ubuntu 20.04 Desktop
There are a few steps we need to perform
- Install Apache
- Install PHP
- Configure “DirectoryIndex” on Apache
Installing Apache
open terminal (alt+ ctrl + t) and type the following command
sudo apt update
sudo apt install apache2
This will install an Apache web server on your machine. You can check the apache status by using the following command.
sudo service apache2 status
you should get output like following
Press ‘Esc’ ‘:’ ‘ q’ to exit from the above output
You can access the apache server by opening http://localhost on your browse. It will open the below default page.
Optionally you might have to install database server as well. You can install MySQL by the following command.
sudo apt install mysql-server
Your distribution might already have MySQL server already installed.
Visit this post If you want to configure MySQL server
2. Install PHP
User the following command to install PHP
sudo apt install php
It will install the latest version of PHP. php7.4 in my case at the time of this post.
You can check the installed version by the following command
php -v
In addition to the PHP package, you’ll need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. You’ll also need libapache2-mod-php to enable Apache to handle PHP files. You can install these packages using the following command
sudo apt install libapache2-mod-php php-mysql
3. Configure DirectoryIndex
We have completed installing PHP. We will change the execution preference to PHP over HTML page.
sudo nano /etc/apache2/mods-enabled/dir.conf
Nano is an editor you can use the editor of your chose. I am using nano for this post. Nano commands: ‘ctrl + o: write to file’, ‘ctrl +x: exit nano’
You will find the following contents in that
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Change the sequence of the DirectoryIndex as follows
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Restart apache
sudo service apache2 restart
Now test whether our apache is executing PHP files
Create a new page on the apache document root. The default document root for apache is ‘/var/www/html’. You can check the document root for apache as follows.
grep -i 'DocumentRoot' /etc/apache2/sites-available/000-default.conf
Now create a new file in the document root. Before that, we need to change the permission of the document root folder.
As you can see the HTML folder is owned by root, so you can’t create new files or folders in that folder. Change ower to your login user using the following command
sudo chown -R PLACE-YOUR-USERNAME_HERE /var/www/html/
(ps. This is not recommended on production server please refer this article to set up production server)
Now we can create a new file in the document root.
nano /var/www/html/first.php
Write some PHP script in the file
<?phpecho "My First PHP site";?>
Now you can check access your file on the browser as http://localhost/first.php