Installing Apache 2 and PHP 7.4 on UBuntu 20

On this article I will explain how to install Apache 2 and PHP 7.4 (and his basic extensions) on Linux Ubuntu 20.04 LTS using apt get command.

I like this method (apt get) not just because it is easy, but also because you can go back and restore if you don't like it using apt get remove packageName. Meavy in other post I will explain how to compile from source Apache and PHP…

First, we are going to log in has a Super User and then update and upgrade our system using the following commands:

sudo su 
apt update
apt upgrade

Second, let's install Apache!

sudo apt install apache2

How you can see, I am not specifying Apache version and, because of that, I will get the latest version that Ubuntu 20.04 has in his repositories. For Apache, that is good enough and with that you will be able to install the most recent software.

You can check the Apache version installed with the command:

apache2 -v

By default, Apache service will run from boot and you can manage it (start, stop and restart) using the following commands:

sudo service apache2 start
sudo service apache2 stop
sudo service apache2 restart

Note: If you start Apache and get the following message:

You have to add at the end of the config file apache2.conf the instruction ServerName localhost. You can edit the file with the command:

sudo gedit /etc/apache2/apache2.conf

additionally, you must allow clean url changing the following lines:

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Third, we are going to install PHP. Before, we need to install some base software, add repositories that will enable us to choose php version. With the following code you do all that and finally check the php version installed:

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.4
php -v

How you can see, this time I did specify the php version because that can affect your software and, thus, it is important have it in control.

Finally, we are going to install some basic php extension, which are necessary to deploy the most common software like Drupal 9, Wordpress, etc.:

sudo apt-get install libapache2-mod-php7.4
sudo apt-get install php-mysql
sudo a2enmod rewrite
sudo apt install php-xml
sudo apt-get install php7.4-gd
sudo apt-get install php7.4-mbstring
sudo apt install php7.4-zip

To accomplish that Apache execute first php files instead html files, you must edit the Apache config file “dif.conf” using the following command sudo gedit /etc/apache2/mods-enabled/dir.conf and add index.php as is indicated in the next block:

<IfModule mod_dir.c>
DirectoryIndex index.php index.html
</IfModule>

To validate the installation, create a info.php file in /var/www/html with the following code:

<?phpphpinfo();?>

Then acces to 127.0.0.1/info.php and you should get the next result:

Post a Comment

0 Comments