View all posts

How to install LAMP Stack on Ubuntu 20.04

What is LAMP?

The LAMP acronym stands for Linux, Apache, MariaDB/MySQL, and PHP, all of which are free and open source. It is the most commonly used software stack for dynamic websites and web applications. The operating system is Linux, the webserver is Apache, the database server is MariaDB/MySQL, and PHP is the server-side scripting language that generates dynamic pages.

Install LAMP

As a first step, we will ensure that the list of available packages on the server is up to date before installing anything new.

sudo apt-get -y update && apt-get upgrade -y

Install Apache Web Server

Install Apache Web server by entering the following command. The apache2-utils package includes some useful utilities, such as the Apache HTTP server benchmarking tool (ab).

sudo apt install -y apache2 apache2-utils

Apache should be automatically started after installation. You can check its status with systemctl.

systemctl status apache2

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-02-02 12:47:15 CST; 1s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 91003 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 91001 (apache2)
      Tasks: 48 (limit: 19072)
     Memory: 4.2M
     CGroup: /system.slice/apache2.service
             ├─53011 /usr/sbin/apache2 -k start
             ├─53012 /usr/sbin/apache2 -k start
             └─53013 /usr/sbin/apache2 -k start

This is how you can start & enable apache2.

sudo systemctl start apache2
sudo systemctl enable apache2

This is how you can check the Apache version:

apache2 -v

Put the public IP address of your Ubuntu 20.04 server in the browser address bar. If you see the “It works!“ web page, it indicates that the Apache Web server is functioning properly. When installing LAMP on a local Ubuntu 20.04 instance, type 127.0.0.1 or localhost in the address bar.

Apache on Ubuntu

Troubleshooting

If the connection is refused or cannot be established, it is possible that there is a firewall that prevents incoming requests to TCP port 80. Those who use iptables firewall will need to run the following command in order to open TCP port 80.

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT

Those who use the UFW firewall will need to run the following command in order to open TCP port 80.

sudo ufw allow http

Configure Apache

It is now necessary to designate www-data (Apache user) as the owner of the document root (also known as web root). Usually, the root user is the default owner.

sudo chown www-data:www-data /var/www/html/ -R

Install MariaDB Database Server

The MariaDB database is a drop-in replacement for MySQL. The project is developed by former members of the MySQL team. To install MariaDB on Ubuntu 20.04, run the following commands.

sudo apt install mariadb-server mariadb-client

MariaDB should be automatically started after installation. You can check its status with systemctl.

systemctl status mariadb

● mariadb.service - MariaDB 10.3 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-02-02 14:19:16 UTC; 1s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 8281 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 56 (limit: 9451)
     Memory: 55.4M
     CGroup: /system.slice/mariadb.service
             └─8282 /usr/sbin/mysqld

This is how you can restart & enable MariaDB.

sudo systemctl start mariadb
sudo systemctl enable mariadb

You should now run the post-installation security script.

sudo mysql_secure_installation

On Ubuntu, the MariaDB package uses unix_socket to authenticate user login, which means you can log in to the MariaDB console using your OS’s username and password. You can therefore use the following command to log in without providing a MariaDB root password.

sudo mariadb -u root

Install PHP 8.0

Ondrej PHP PPA is always up-to-date and contains the latest versions of PHP. Install PHP 8.0 on Ubuntu by adding PPA using the following commands.

sudo apt update && sudo apt install software-properties-common

The next step is to enable the PHP PPA and update the apt cache by using the following command.

sudo add-apt-repository ppa:ondrej/php 

The next step in installing PHP 8 on Ubuntu is to run the following command.

sudo apt install libapache2-mod-php8.0 php8.0 php8.0-gd php8.0-xml php8.0-soap php8.0-mbstring php8.0-mysql 

Don’t forget to install the required PHP modules for your application. Use the following command to search all available PHP 8.0 modules.

sudo apt search php8.0-*

Enable the Apache PHP 8.0 module and restart the Apache Web server.

sudo a2enmod php8.0
sudo systemctl restart apache2

You can now check the PHP version of the installation.

php --version

PHP 8.0 (cli) (built: Sep 24 2021 14:51:56)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with the ionCube PHP Loader + ionCube24 v10.4.5, Copyright (c) 2002-2020, by ionCube Ltd.
    with Zend Guard Loader v3.3, Copyright (c) 1998-2014, by Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies

Related Articles...

unison

How to install Unison

What is Unison? Unison is an open-source file-synchronization application for OSX, Linux, Unix, and Windows. It permits two copies of a collection of files and directories to be stored on different hosts, modified independently, and then brought... Read more

This website uses cookies

We use cookies for the analysis of our visitor data, to improve our website, and to give you a great website experience. For more information about the cookies we use, please see our cookie policy.