How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 20.04 [Quickstart]
How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 20.04 [Quickstart]

Tutorial

How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 20.04 [Quickstart]

ApachePHPLAMP StackUbuntu 20.04

Introduction

In this quickstart guide, we’ll install a LAMP stack on an Ubuntu 20.04 server.

For a more detailed version of this tutorial, with more explanations of each step, please refer to How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu 20.04

Prerequisites

To follow this guide, you’ll need access to an Ubuntu 20.04 server as a sudo user.

Step 1 — Install Apache

Update your package manager cache and then install Apache with:

  • sudo apt update
  • sudo apt install apache2

Once the installation is finished, you’ll need to adjust your firewall settings to allow HTTP traffic on your server. Run the following command to allow external access on port 80 (HTTP):

  • sudo ufw allow in "Apache"

With the new firewall rule added, you can test if the server is up and running by accessing your server’s public IP address or domain name from your web browser. You’ll see a page like this:

Ubuntu 20.04 Apache default

Step 2 — Install MySQL

We’ll now install MySQL, a popular database management system used within PHP environments.

Again, use apt to acquire and install this software:

  • sudo apt install mysql-server

When the installation is finished, it’s recommended that you run a security script that comes pre-installed with MySQL. Start the interactive script by running:

  • sudo mysql_secure_installation

This will ask if you want to configure the VALIDATE PASSWORD PLUGIN. Answer Y for yes, or anything else to continue without enabling. If you answer “yes”, you’ll be asked to select a level of password validation.

Your server will next ask you to select and confirm a password for the MySQL root user. Even though the default authentication method for the MySQL root user dispenses the use of a password, even when one is set, you should define a strong password here as an additional safety measure.

For the rest of the questions, press Y and hit the ENTER key at each prompt.

Note: At the time of this writing, the native MySQL PHP library mysqlnd doesn’t support caching_sha2_authentication, the default authentication method for MySQL 8. For that reason, when creating database users for PHP applications on MySQL 8, you’ll need to make sure they’re configured to use mysql_native_password instead. Please refer to step 6 of our detailed LAMP on Ubuntu 20.04 guide to learn how to do that.

Step 3 — Install PHP

To install PHP and its dependencies, run:

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

Once the installation is finished, you can run the following command to confirm your PHP version:

  • php -v
Output
PHP 7.4.3 (cli) (built: Mar 26 2020 20:24:23) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Step 4 — Create a Virtual Host for your Website

In this guide, we’ll set up a domain called your_domain, but you should replace this with your own domain name.

Apache on Ubuntu 20.04 has one server block enabled by default that is configured to serve documents from the /var/www/html directory. Instead of modifying /var/www/html, we’ll create a directory structure within /var/www for the your_domain site, leaving /var/www/html in place as the default directory to be served if a client request doesn’t match any other sites.

Create the directory for your_domain as follows:

  • sudo mkdir /var/www/your_domain

Next, assign ownership of the directory with the $USER environment variable, which will reference your current system user:

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

Then, open a new configuration file in Apache’s sites-available directory using your preferred command-line editor:

  • sudo nano /etc/apache2/sites-available/your_domain.conf

This will create a new blank file. Paste in the following bare-bones configuration:

/etc/apache2/sites-available/your_domain.conf
<VirtualHost *:80>
    ServerName your_domain
    ServerAlias www.your_domain 
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/your_domain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file when you’re done. If you’re using nano, 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 your_domain

To disable Apache’s default website, type:

  • 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/your_domain is still empty. Create an index.html file in that location so that we can test that the virtual host works as expected:

  • nano /var/www/your_domain/index.html

Include the following content in this file:

/var/www/your_domain/index.html
<html>
  <head>
    <title>your_domain website</title>
  </head>
  <body>
    <h1>Hello World!</h1>

    <p>This is the landing page of <strong>your_domain</strong>.</p>
  </body>
</html>

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

http://server_domain_or_IP

You’ll see a page like this:

Apache virtual host test

Step 5 — Test PHP with Apache

We’ll now create a PHP test script to confirm that Apache is able to handle and process requests for PHP files.

Create a new file named info.php inside your custom web root folder:

  • nano /var/www/your_domain/info.php

This will open a blank file. Add the following content inside the file:

/var/www/your_domain/info.php
<?php
phpinfo();

When you are finished, save and close the file.

Go to your web browser and access your server’s domain name or IP address, followed by the script name, which in this case is info.php:

http://server_domain_or_IP/info.php

You’ll see a page similar to this:

Ubuntu 20.04 PHP info

After checking the relevant information about your PHP server through that page, it’s best to remove the file you created as it contains sensitive information about your PHP environment -and your Ubuntu server. You can use rm to do so:

  • sudo rm /var/www/your_domain/info.php

Here are links to more detailed guides related to this tutorial:

Creative Commons License