How to Install PHP for Apache on Ubuntu 20.04
January 4, 2022PHP is one of the most used server-side programming languages. Many popular content management system and frameworks such as WordPress, Magento, and Laravel are written in PHP. At the time of writing, the default Ubuntu 20.04 repositories include PHP 7.4 version. Before installing getting started, you should already have install Ubuntu on your instance and have install Apache2.4 running as your web server.
Install and Configure PHP 7.4
Update all available repositories on your system and install the Apache webserver using the apt command below.
sudo apt update && sudo apt upgrade -y
In this step we will install and configure PHP 7.4. By default, the official Ubuntu 20.04 repository provides PHP 7.4 packages.
Install PHP 7.4 packages using the apt command below.
sudo apt install libapache2-mod-php php php-mysql php-common php-xml php-gd php-opcache php-intl php-pear php-mbstring php-memcached php-tokenizer php-json php-bcmath php-zip php-curl unzip
After entering this command, apt
will tell you which packages it plans to install and how much disk space they’ll take up. Press Y
and hit ENTER
to confirm, and the installation will proceed.
Default Index.php on Apache
In case you want to change this behavior, you’ll need to edit the /etc/apache2/mods-enabled/dir.conf
file and modify the order in which the index.php
file is listed within the DirectoryIndex
directive. The nano is a text editor in Ubuntu that will allow you to create/edit your configuration file:
sudo nano /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c> DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Save and close using the CTRL + X
combination, then press Y
and confirm by pressing ENTER
.
After saving and closing the file, you’ll need to reload Apache so the changes take effect.
sudo systemctl reload apache2