Install Apache, PHP, MySQL (LAMP Stack) in Ubuntu

The LAMP (Linux, Apache, MySQL and PHP) stack is one of the most common approaches to building dynamic websites. A LAMP stack uses mature and reliable opensource software as building blocks and has been the cornerstone for a variety of projects including WordPress, Facebook, and Wikipedia. Understanding how to install and configure a LAMP stack is an important skill for Linux administrators and web developers alike.

A LAMP stack is a set of four popular opensource components for web development.
Each of the letters in the LAMP acronym stands for one of the components:

  • Linux is the operating system in the stack.
  • Apache is the web server in the stack.
  • MySQL is the database in the stack.
  • PHP is the programming language in the stack.

With these components, web developers have all the components they need to build web applications that can serve dynamic content. Linux acts as the base layer where the other components are installed, Apache serves HTTP(S) pages, MySQL allows data persistence, and PHP allows developers to tie the web server and database layers together and create dynamic sites.

For example, PHP can take data from a webform and perform CRUD (create, read, update, delete) operations on a MySQL database.

Prerequisites

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. This can be configured using initial server setup guide for Ubuntu 20.04.

Install Apache

The Apache web server is among the most popular web servers in the world. It’s well documented, has an active community of users, and has been in wide use for much of the history of the web, which makes it a great choice for hosting a website.

sudo apt update
sudo apt install apache2

You’ll also be prompted to confirm Apache’s installation by pressing Y, then ENTER.

Update the Firewall

Once the installation is finished, you’ll need to adjust your firewall settings to allow HTTP traffic. UFW has different application profiles that you can leverage for accomplishing that. To list all currently available UFW application profiles, you can run:

sudo ufw app list

You’ll see output like this:

Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH

Here’s what each of these profiles mean:

Apache: This profile opens only port 80 (normal, unencrypted web traffic).
Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic).
Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic).

To only allow traffic on port 80, use the Apache profile:

sudo ufw allow in "Apache"

You can verify the change with:

sudo ufw status

Output
Status: active

To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)

Traffic on port 80 is now allowed through the firewall.

Once the Apache2 package installation completed, check the service status:

sudo systemctl status apache2

The Apache service is active and running. Now, open a web browser and enter IP address of your server. You will see the default Apache web page

http://your_server_ip

 

Install PHP

To install PHP on your system, update apt index and then install it on your system.

sudo apt install -y php libapache2-mod-php

Also install additional PHP modules required for your application.

sudo apt install php-curl php-gd php-json php-mbstring php-xml

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

php -v

 

Install MySQL Server

The default Ubuntu 20.04 apt repositories contains MySQL server 8.0. Finally, install mysql-server packages for the MySQL database. Also, install the php-mysql package to use MySQL support using PHP. Use the following command to install it.

sudo apt install mysql-server php-mysql

The installer will prompt for the root password, This password will work for your MySQL root user. After installing MySQL execute the following command for initial settings of MySQL server. You will see that script will prompt for more settings than earlier MySQL versions like password validation policy etc.

sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.

- Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

If you see error while installing and configuring mysql secure server then set mysql root password separately from the article update mysql root password.

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.

Virtual Host for your Website

When using the Apache web server, you can create virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. 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. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. 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. Here, we’ll use nano:

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

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

<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.

To configure your Apache server with SSL check How to Install SSL Certificate on Apache 20.04 Web Server

With this VirtualHost configuration, we’re telling Apache to serve your_domain using /var/www/your_domain as the web root directory. If you’d like to test Apache without a domain name, you can remove or comment out the options ServerName and ServerAlias by adding a # character in the beginning of each option’s lines.

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

sudo a2ensite your_domain

You might want to disable the default website that comes installed with Apache. This is required if you’re not using a custom domain name, because in this case Apache’s default configuration would overwrite your virtual host. 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, restart Apache so these changes take effect:

sudo systemctl restart apache2

Upload the website files in the folder /var/www/your_domain.

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

http://server_domain_or_IP

And you can see your website on the browser.