×


Install LAMP in Ubuntu 20.04 - Step by step guide ?

LAMP basically refers to a stack of programs or software that function in Ubuntu system called Linux, Apache, MariaDB/MySQL, and PHP, all of these are free to use and open source, combined on a server ultimately allows them to host dynamic webpages. 

The operating system is Linux which represent the 'L', the web server is Apache which represent 'A', the database server is MariaDB/MySQL which represents 'M', and PHP is the server-side language of programming, which represents 'P', and it performs the task of generating interactive web pages.

Here at Ibmi Media, we shall look into how you can install LAMP in Ubuntu 20.04.


Steps to install LAMP in Ubuntu 20.04 Focal Fossa

1. Initiate Command Line Interface

To begin, installing the LAMP in Ubuntu, simply open the Command line interface, which can simply be done by pressing 'Ctrl + Alt + T' buttons on keyboard together. As soon as the CLI is open, run the following command:

$ sudo su

There might be 'password required' pop up on your screen after this, the password is required to authenticate the action, it is a small opportunity. Enter the password and command will then put into action.


2. Perform System Update

It is advisable and a great practice for the software packages to be updated before the installation of the LAMP into your system because this step makes sure that you are looking at the latest available versions of programs/apps/software in the system with the below command:

$ sudo apt update


3. Install Apache 2 on the system

Now you should start by installing Apache2, and to install Apache Web server, use the command below in the Command Line interface. By hitting 'y', then 'Enter', you may authorize the download:

$ sudo apt install apache2

After the installation is complete, Apache2 will be functioning already by default, but to make sure that the Apache2 is yet functioning properly or not, you can check the status of Apache2 by giving the system command. If it is not functioning properly, you can enable it by giving the 'enable Apache2' command.

The commands below can be used to stop, start Apache2 service to always start up with the server boots:

$ sudo systemctl start apache2
$ sudo systemctl status apache2


4. Install the MySQL database server

MySQL is a well-known open source program that is indeed utilized widely in order to maintain massive quantities of content i.e. data in practically any program or application installed in the system. Now it is time for you to install MySQL server into your system, to do so you should enter the following command in the system:

$ sudo apt install mysql-server

Type 'Y', hit "enter".

The installation will begin shortly.

After the completion of installation of the MySQL server on the Ubuntu, the server will be ready to run but might require the password. Because by default, the MySQL package on Ubuntu utilizes auth_socket to authenticate user login, this essentially implies that you can login into MySQL interface using merely the OS's id and passcode and are not required to put the exact MySQL root passcode into the system. To do that enter the following command in the system:

$ mysql -uroot

By now, MySQL server should be up and running by default but you can check its status too just to make sure, to check the status, enter the command of 'systemctl status mysql' into the system and wait for the output, the output will show you the status of MySQL server. If you want to create a new User id, you should enter the following commands one by one.

Provide a new user name and the password:

CREATE USER 'user1' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *. TO 'user1' WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit


5. Install Hypertext Preprocessor (PHP) 7.4

PHP 7.4 is a good version with the best efficient performance and is very reliable and dependable to be installed in Ubuntu 20.04 version as compared to the previous ones.

PHP 7.4 and all other PHP collections are essential to download as they work the best with Apache 2 that you have already downloaded.

To download the PHP 7.4 on your Ubuntu, you must enter the following few mentioned commands in your system.

After entering the command, press 'y', then 'Enter', you may validate the download:

$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update
$ sudo apt install php7.4 libapache2-mod-php7.4

You can install the required PHP modules on your system. Just change the PHP version with the packages names as per your requirements:

$ sudo apt install php7.4-mysql php7.4-curl php7.4-json php7.4-cgi php7.4-xsl

Next, Configure and reboot the webserver after enabling the apache modifications to interact with php7. And to do so, enter the following commands:

$ sudo a2enmod php7.4
$ sudo a2enmod rewrite
$ sudo service apache2 restart


6. Install PHP 8 and its Extensions (Optional)

You can easily install PHP 8 on Ubuntu by running the below command:

$ sudo apt install php8.0 libapache2-mod-php8.0

To install PHP 8 extensions on Ubuntu 20.04 using the command syntax:

$ sudo apt install php8.0-[extension]

Some available extensions:

$ sudo apt install php8.0-
php8.0-amqp       php8.0-common     php8.0-gd         php8.0-ldap       php8.0-odbc       php8.0-readline   php8.0-sqlite3    php8.0-xsl
php8.0-apcu       php8.0-curl       php8.0-gmp        php8.0-mailparse  php8.0-opcache    php8.0-redis      php8.0-sybase     php8.0-yac
php8.0-ast        php8.0-dba        php8.0-igbinary   php8.0-mbstring   php8.0-pgsql      php8.0-rrd        php8.0-tidy       php8.0-yaml
php8.0-bcmath     php8.0-dev        php8.0-imagick    php8.0-memcached  php8.0-phpdbg     php8.0-smbclient  php8.0-uuid       php8.0-zip
php8.0-bz2        php8.0-ds         php8.0-imap       php8.0-msgpack    php8.0-pspell     php8.0-snmp       php8.0-xdebug     php8.0-zmq
php8.0-cgi        php8.0-enchant    php8.0-interbase  php8.0-mysql      php8.0-psr        php8.0-soap       php8.0-xhprof
php8.0-cli        php8.0-fpm        php8.0-intl       php8.0-oauth      php8.0-raphf      php8.0-solr       php8.0-xml

An example to install a few more useful extensions:

$ sudo apt install php8.0-cli php8.0-common php8.0-fpm php8.0-redis php8.0-snmp php8.0-xml


7. Switch Between PHP Versions

You can use update-alternatives command to set the default PHP version. Use this tutorial to read more details about switching PHP version for CLI and Apache:

$ sudo update-alternatives --config php

Select PHP version number as per your requirement. This will change the PHP CLI version only.

There are 4 choices for the alternative php (providing /usr/bin/php):

Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php7.4   74        auto mode
  1            /usr/bin/php8.0   80        manual mode
Press to keep the current choice[*], or type selection number: 1


[Need help in fixing Apache Configuration issues ? We can help you. ]


Conclusion

This article covers how to install LAMP on Ubuntu 20.04 and run the Apache module of the PHP. In fact, PHP code can be executed via the Apache web server; PHP module of Apache and PHP-FPM.