Setup LAMP Stack (Linux, Apache, MySQL, PHP) Environment on Ubuntu 20.04

What is LAMP Stack?

LAMP is a widely used Full Stack model for web service stacks. Its name “LAMP” is an acronym of four open-source components Linux, Apache, MySQL, and PHP. MySQL as the relational database management system and PHP (Perl or Python) as the object-oriented scripting language.

In this guide, you’ll set up a LAMP stack on an Ubuntu 20.04 server.


In order to complete this tutorial, you will need to have an Ubuntu 20.04 server with a non-root sudo-enabled user account and a basic firewall.

1. Install Apache and Update Firewall

Start by updating the package manager cache. If this is the first time you’re using sudo within this session, you’ll be prompted to provide your user’s password to confirm you have the right privileges to manage system packages with apt.

sudo apt update

Now, install Apache

sudo apt install apache2

You’ll need to complete prompts via pressing Y and then Enter

Now, let’s check and update firewall. Run following command to check if you have Firewall enabled or not:

sudo ufw status

If you see Status: Active means Firewall is Active and we need to allow Apache into Firewall via:

sudo ufw allow in “Apache”

If don’t see Active status means Firewall is disabled and no need to do anything else to update Firewall.

Open http://your_server_ip and you will see Apache2 Ubuntu Default Page like below:

2. Install MySQL

Now that Apache is up and running, next we need to install MySQL:

sudo apt install mysql-server

Again, when prompted, press Y and press Enter to confirm.

When the installation is finished, it’s recommended that you run a security script that comes pre-installed with MySQL. This script will remove some insecure default settings and lock down access to your database system. Start the interactive script by running:

sudo mysql_secure_installation

This will ask if you want to configure the VALIDATE PASSWORD PLUGIN.

Enabling this feature is something of a judgment call. If enabled, passwords which don’t match the specified criteria will be rejected by MySQL with an error. It is safe to leave validation disabled, but you should always use strong, unique passwords for database credentials.

When you’re finished, test if you’re able to log in to the MySQL console by typing:

sudo mysql

This will connect to the MySQL server as the administrative database user root, which is inferred by the use of sudo when running this command. Now, you’re in MySQL console, type exit to close it and get back to previous console.

3. Install PHP

So far we have installed Apache and MySQL. Our server is running, our database is in place, now let’s install PHP. To install PHP, run following command:

sudo apt install php libapache2-mod-php php-mysql

Once the installation has been completed, you can check version via:

php -v

At this point, your LAMP stack is fully operational, but before you can test your setup with a PHP script, it’s best to set up a proper Apache Virtual Host to hold your website’s files and folders. We’ll do that in the next step.

4. Create Apache Virtual Host

In this example, we will be taking as a domain name to create virtual host and create directories.

Create the directory of your domain name like below and assign proper permissions:

cd /var/www


sudo chown -R $USER:$USER /var/www/

Now, create new apache configurations and open it in nano editor like below:

sudo touch /etc/apache2/sites-available/

sudo nano /etc/apache2/sites-available/

Above command will open blank file in nano editor,


<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/
    ErrorLog ${APACHE_LOG_DIR}/
    CustomLog ${APACHE_LOG_DIR}/ combined

Save and close the file when you’re done. You can do that by pressing CTRL+X, then Y and ENTER.

You can now use a2ensite to enable the new virtual host:

sudo a2ensite

sudo a2dissite 000-default

To make sure your configuration file doesn’t contain syntax errors, run:

sudo apache2ctl configtest

Finally, reload Apache so these changes take effect:

sudo systemctl reload apache2

Your new website is now active, but the web root /var/www/ is still empty. Create an index.php file in that location so that we can test that the virtual host works as expected:

nano /var/www/

Add following in file,


    <title>Welcome to</title>
    <?php echo "Howdy!!"; ?>

Now go to your browser and access your server’s domain name or IP address once again: http://server_domain_or_IP

You will see a message “Howdy” printed on page with title “Welcome to


In this guide, we’ve built a flexible foundation for serving PHP websites and applications to your visitors, using Apache as web server and MySQL as database system. You can follow this article to setup LAMP Stack on Ubuntu over Digital Ocean’s droplet OR AWS EC2 server.

In next step, we will go through how to secure connection using Let’s Encrypt to serve our application over HTTPS.

Leave a Reply

Your email address will not be published.

4 × three =