Skip links
installing-apache-mysql-and-php-lamp-stack-on-ubuntu-banner

Installing Apache, MySQL, and PHP (LAMP stack) on Ubuntu

Let’s set up a LAMP stack (that’s Apache, MySQL, and PHP) on Ubuntu. It’s like creating the foundation for a website.

In this guide, we’ll install Apache2 (that’s your web server), MySQL (a database), and PHP 7.4 (which makes your website dynamic).

We’ll also add some extra tools to make PHP work better and tweak its settings.

After that, we’ll make sure your website is secure by adding Let’s Encrypt SSL, which is like a protective shield for your website. We’ll also make sure your website uses HTTPS (that’s the secure version of HTTP).

This whole process has been tested on Google Cloud, so you can use it on other services like AWS or Azure, or on your own private servers running Ubuntu 20.04.

Requirements

  • Full control over your server or a user with sudo privileges.

Step 1 : Environment Setup

First, let’s make sure everything is fresh and up-to-date. To do that, just type in these two commands, one after the other

sudo apt update
sudo apt upgrade

Once your system is all freshened up, you’re ready to move on to the next steps.

Step 2 : Install Apache Server

Next, we’re going to bring in Apache, which is like the traffic cop for your website. It makes sure everything gets to the right place. To do that, type this command

sudo apt install apache2

This command will not only install Apache but also get all its dependencies as well.

Step 3 : Setting Up the Firewall

Okay, now let’s make sure that your website can be seen by everyone on the internet. We’re going to use something called ‘Uncomplicated Firewall’ or UFW. It’s like setting up security rules for your website.

We’ll set it up so that people can access your website through regular web addresses (HTTP) and secure web addresses (HTTPS). It’s like opening the door for your website to the outside world.

sudo ufw app list

You’ll find a list of all the applications.

// Output
Available applications:
   Apache
   Apache Full
   Apache Secure
   OpenSSH
  • Apache: This option allows port 80 for regular, unencrypted web traffic.
  • Apache Full: This setting permits both port 80 for unencrypted web traffic and port 443 for TLS/SSL encrypted traffic.
  • Apache Secure: Use this choice to enable only port 443 for TLS/SSL encrypted traffic.
  • OpenSSH: This setting opens port 22 for secure SSH access.

If you don’t plan on using SSL (that secure padlock for websites), just go ahead and enable the Apache profile.

Now, let’s switch on the Apache Full profile.

sudo ufw allow 'Apache Full'

Now, let’s check the status of the firewall by running this command

sudo ufw status

The below result will appear

// Output
Status: active
 To                         Action      From
 --                         ------      ----
 Apache Full                ALLOW       Anywhere                  
 OpenSSH                    ALLOW       Anywhere                  
 Apache Full (v6)           ALLOW       Anywhere (v6)             
 OpenSSH (v6)               ALLOW       Anywhere (v6)

Step 4 : Verify Apache Installation

After you’ve installed Apache, it starts up all by itself and is already working.

To keep tabs on what Apache is up to, we use the systemctl command. You can see how Apache is doing with this command

sudo systemctl status apache2

That should generate the below output

Output
● apache2.service - The Apache HTTP Server
    Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Drop-In: /lib/systemd/system/apache2.service.d
            └─apache2-systemd.conf
    Active: active (running) since Tue 2020-01-06 03:59:34 UTC; 5min ago
  Main PID: 10617 (apache2)
     Tasks: 55 (limit: 667)
    CGroup: /system.slice/apache2.service
            ├─10617 /usr/sbin/apache2 -k start
            ├─10619 /usr/sbin/apache2 -k start
            └─10620 /usr/sbin/apache2 -k start
 Jan 06 03:59:34 apache systemd[1]: Starting The Apache HTTP Server…
 Jan 06 03:59:34 apache systemd[1]: Started The Apache HTTP Server.

Now that we’ve got Apache set up and the firewall configured, let’s go ahead with the database

Step 5 : Installing MySQL

In Ubuntu 20.04, MySQL 8 comes included in the Focal Fossa repositories by default, making it a easy to install using the apt install command.

sudo apt install mysql-server

After the installation is done, the MySQL service start running on its own. To make sure the MySQL server is up and running, simply type

sudo service mysql status

It should display the below output

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-05-05 07:13:18 UTC; 1min 4s ago
   Main PID: 3333 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 2010)
     Memory: 322.9M
     CGroup: /system.slice/mysql.service
             └─3333 /usr/sbin/mysqld 

Step 6 : Secure MySQL Installation

The MySQL installation includes a handy script called mysql_secure_installation, which makes it simple to enhance the security of your MySQL server.

sudo mysql_secure_installation

During the setup, you’ll come across the VALIDATE PASSWORD PLUGIN. It’s like a handy tool that checks how strong your MySQL user passwords are to keep things secure.

If you’d like to use this plugin, press y, or any other key to move forward.

There are three levels for password strength: low, medium, and strong. Go for 2 to have strong password checks.

Next up, you’ll be prompted to create a password for the MySQL root user. If you’re using the password plugin, the script will evaluate the strength of your new password. Hit y to confirm.

Following that, you’ll be asked to remove the anonymous user, restrict root user access to the local machine, get rid of the test database, and refresh the privilege settings. It’s a good idea to reply y to all these questions.

Step 7 : Install PHP

Ubuntu 20.04 is all set with the latest PHP 7.4 repository added by default. That means you can install PHP easily with this command

In case you are opting for AWS or other services which does not have the repository, add the repository using following commands

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

then run the following command to install php and the required extensions, here we are adding 7.4 version but you can also install the latest version

sudo apt install php7.4 libapache2-mod-php php7.4-mysql php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl -y 

After PHP is all set up, you can take a peek at the version by running this simple command

php -v

Step 8 : Modify Default PHP Settings

Next, let’s fine-tune PHP for your web applications by tweaking some settings in the php.ini file.

For PHP 7.4 with Apache, you’ll find the php.ini file on the path below, type this to open it

sudo nano /etc/php/7.4/apache2/php.ini

Once you’re in the editor, press F6 to search and update these values to make things run smoother

upload_max_filesize = 32M
post_max_size = 48M
memory_limit = 256M
max_execution_time = 600
max_input_vars = 3000
max_input_time = 1000

When you’ve made these changes, remember to restart Apache for them to kick in and make a difference.

Step 9 : Configure Apache

First, we’ll disable the default Apache configuration, which is like turning off a default setting. To do that, type

sudo a2dissite 000-default

Now, let’s get things ready for your website. We need to create some directories and set up the right permissions.

Use this command to create directories

sudo mkdir -p /var/www/html/domainname/public
sudo mkdir -p /var/www/html/domainname/public

Then, set the right permissions with

sudo chmod -R 755 /var/www/html/domainnamesudo chown -R www-data:www-data /var/www/html/domainname

Now, it’s time to make a new configuration for your website. Open the file where this is done, type

sudo nano /etc/apache2/sites-available/domainname.conf

In that file, paste the following configuration settings

<VirtualHost *:80>
     ServerAdmin admin@domainname.com
     ServerName domainname.com
     ServerAlias www.domainname.com

     DocumentRoot /var/www/html/domainname/public

     <Directory /var/www/html/domainname/public>
         Options Indexes FollowSymLinks
         AllowOverride All
         Require all granted
     </Directory>

     ErrorLog ${APACHE_LOG_DIR}/error.log 
     CustomLog ${APACHE_LOG_DIR}/access.log combined 
 </VirtualHost>

Now, let’s activate the new configuration

sudo a2ensite domainname.conf

Step 10 : Installing SSL

Okay, let’s talk about HTTPS. It’s a way for your website to have secure conversations with your visitors. It’s like making sure your chats are locked and secure.

Thanks to Let’s Encrypt, you can get SSL certificates for free, and that’s fantastic because it means even more websites are using HTTPS, and it builds trust with your audience.

Now, to get this all set up on your Ubuntu 20.04, you need to run a command. Here it is. First, install Certbot by Let’s Encrypt

sudo apt install python3-certbot-apache

After that, run this command to get your certificates

sudo certbot --apache --agree-tos --redirect -m youremail@email.com -d domainname.com -d www.domainname.com

You’ll be prompted to choose some options. Just select the one that suits your needs and hit Enter.

This command does a lot of things – it installs a free SSL certificate, sets up automatic redirection to HTTPS, and even restarts the Apache server.

Step 11 : SSL Renewal

Keep in mind, Let’s Encrypt certificates are like tickets to a concert that only last for 90 days. So, you’ve got to renew them regularly.

To check if everything is set up for automatic renewal, you can run a quick test. Just type in:

sudo certbot renew --dry-run

This command checks if the certificates are close to expiring and makes sure the auto-renewal system is good to go.

Step 12 : Test the setup

After you’ve completed the previous steps, you can now create a test PHP file in your web directory.

Open the file for editing with

sudo nano /var/www/html/domainname/public/info.php

Inside that file, paste the following code

<?php phpinfo();

Make sure to save the file.

Now, go ahead and check your domain name with info.php in the URL (like domainname.com/info.php).

You’ll notice that your domain automatically switches to HTTPS, and you’ll see all the details about your PHP setup.

Conclusion

You’ve just picked up the know-how to install a LAMP stack on Ubuntu 20.04.

Thanks for spending your time with us. If you run into any issues or have any feedback, drop us a message below. We’re here to help!

This website uses cookies to ensure you get the best experience on our website. By using this site, you agree to our use of cookies. Learn more