Install Linux, Apache, MySQL, PHP (LAMP) on Ubuntu

LAMP stands for Linux, Apache, MySQL and PHP. Most of the websites used LAMP stack and it’s very easy to setup. In this lesson, I’m going to install LAMP on Ubuntu server. I’m testing on Ubuntu 20.04 (LTS).

Table of Contents

  1. Install Apache Web Server
  2. Install MySQL
  3. Install PHP
  4. Add Virtual Host

Install Apache Web Server

Install Apache using Ubuntu’s package manager, apt:

# update
apt update && upgrade -y
# installing apache
sudo apt install apache2 -y

 Add rules to ufw Firewall to allow incoming HTTP and HTTPS traffic:

# application list
sudo ufw app list

# apache full info
sudo ufw app info "Apache Full"

# allow 80 & 443 ports
sudo ufw allow in "Apache Full"

Now visit http://your_server_ip and you’ll see the apache default page.

Install MySQL

Run this command to install MySQL:

sudo apt install mysql-server -y

After installation,  check the status of the MySQL server:

sudo systemctl status mysql

Let’s secure our MySQL server. Enter this command:

sudo mysql_secure_installation

You’ll be asked some questions. The questions and answers are:

1. Would you like to setup VALIDATE PASSWORD component?
Ans: y and then set your password.

2. Remove anonymous users?
Ans: y

3. Disallow root login remotely?
Ans: y (if you need, enter N)

4. Remove test database and access to it?
Ans: y

5. Reload privilege tables now?
Ans: y

Now you can login to mysql server using sudo mysql command and create database, user etc.

Install PHP

To install default PHP version, run this command:

sudo apt install php libapache2-mod-php

To install latest version of PHP run this command:

sudo apt install software-properties-common -y
# add Ondrej PHP repository
sudo add-apt-repository ppa:ondrej/apache2

Install PHP 7.4 and some of the most common PHP modules:

sudo apt install php7.4 php7.4-common php7.4-opcache php7.4-cli php7.4-gd php7.4-curl php7.4-mysql -y

Check PHP version:

php -v

Once the packages are installed restart the Apache service:

sudo systemctl restart apache2

Add Virtual Host

Create the directory for example.com as follows:

sudo mkdir /var/www/exammple.com

Set permissions:

sudo chmod -R 755 /var/www/example.com

Create a simple index page:

nano /var/www/example.com/index.html

and paste this code in index.html:

<html>
    <head>
        <title>Welcome to Example.com</title>
    </head>
    <body>
        <h1>Welcome to Example.com</h1>
    </body>
</html>

Then save and close the file. Now we need to create a virtual host file:

sudo nano /etc/apache2/sites-available/example.com.conf

Paste this config:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
</VirtualHost>

Let’s enable the file with the a2ensite tool:

sudo a2ensite example.com.conf

Disable the default site:

sudo a2dissite 000-default.conf

Check configuration:

sudo apache2ctl configtest

If Syntax OK, then restart apache:

sudo systemctl restart apache2

Now you can test this by navigating to http://example.com.

That’s it. Thanks for reading. ?