How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu
8 mins read

How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu

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

Installing a LAMP stack on Ubuntu is a fundamental process for setting up a web server environment. This combination of Linux, Apache, MySQL, and PHP provides a robust foundation for hosting dynamic websites and web applications. Here’s a step-by-step guide on how to install each component:

A “LAMP” stack is a group of open-source software that is typically installed together in order to enable a server to host dynamic websites and web apps written in PHP. This term is an acronym which represents the Linux operating system with the Apache web server. The site data is stored in a MySQL database, and dynamic content is processed by PHP.

In this guide, you’ll set up a LAMP stack on an Ubuntu 22.04 server. These steps remain consistent for Ubuntu v18.04 and above.

Step 1 – Installing Apache and Updating the Firewall

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

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

Then, install Apache with:

sudo apt install apache2

You’ll be prompted to confirm Apache’s installation. 

sudo ufw app list
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).

Ubuntu’s default firewall is provided by UFW (Uncomplicated Firewall). If UFW is inactive, you can enable it and allow traffic on Apache’s default port (port 80).

LAMP

Enable UFW:

If UFW is not already enabled, you can do so with the following command:

sudo ufw enable

Allow Apache Through the Firewall:

To allow incoming traffic on port 80 (HTTP), use the following command:

sudo ufw allow 'Apache'

This command allows HTTP traffic to reach your Apache web server.

Verify the Status of UFW:

You can verify the status of UFW to ensure that the rules have been correctly applied:

sudo ufw status

You should see a message indicating that Apache traffic is allowed.

Step 2 – Installing MySQL

Now that you have Ubuntu web server up and running, you need to install the database system to be able to store and manage data for your site. MySQL is a popular database management system used within PHP environments.

Again, use apt to acquire and install this software:

sudo apt install mysql-server

When prompted, confirm installation by typing Y, and then ENTER.

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

Answer Y for yes, or anything else to continue without enabling.

When you’re finished, test whether 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. Below is an example output:

To exit the MySQL console, type:

mysql> exit

Your MySQL server is now installed and secured. Next, you’ll install PHP, the final component in the LAMP stack.

Step 3 – Installing PHP

You have Apache installed to serve your content and MySQL installed to store and manage your data. PHP is the component of our setup that will process code to display dynamic content to the final user. In addition to the php package, you’ll need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. You’ll also need libapache2-mod-php to enable Apache to handle PHP files. Core PHP packages will automatically be installed as dependencies.

To install these packages, run the following command:

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

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

php -v

Step 4 — Creating a 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.

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. Add in the following bare-bones configuration with your own domain name:

<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, do that by pressing CTRL+X, then Y and ENTER.

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 pound sign (#) the beginning of each option’s lines.

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 override 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 the following command:

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

Save and close the file, then go to your browser and access your server’s domain name or IP address:

http://server_domain_or_IP

Your web page should reflect the contents in the file you just edited:

You can leave this file in place as a temporary landing page for your application until you set up an index.php file to replace it. Once you do that, remember to remove or rename the index.html file from your document root, as it would take precedence over an index.php file by default.

Step 5 — Testing PHP Processing on your Web Server

Now that you have a custom location to host your website’s files and folders, 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
/var/www/your_domain/info.php
<?php
phpinfo();

When you are finished, save and close the file.

To test this script, 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

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. Use rm to do so:

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

You can always recreate this page if you need to access the information again later.

Conclusion

In conclusion, you have successfully set up a LAMP (Linux, Apache, MySQL, PHP) stack on your Ubuntu server. This stack provides a robust foundation for hosting dynamic websites and web applications. Here’s a brief summary of what you accomplished:

  1. Installed Apache: Apache is a widely used web server that serves web content to clients over the HTTP protocol.
  2. Configured Firewall: The firewall (UFW) was updated to allow traffic on Apache’s default port (port 80), ensuring that incoming web requests can reach your server.
  3. Installed MySQL: MySQL is a popular relational database management system used for storing and managing data for your web applications.
  4. Secured MySQL Installation: The MySQL secure installation script was run to set a root password, remove insecure default settings, and enhance the security of your MySQL server.
  5. Installed PHP: PHP is a server-side scripting language used for developing dynamic web content. It was installed and integrated with Apache to process PHP files.
  6. Tested PHP Processing: A PHP test file was created to verify that PHP is installed and configured correctly. Accessing this file in a web browser displayed PHP configuration details.

With the LAMP stack set up, you’re now equipped to develop and host dynamic websites and web applications on your Ubuntu server. You can further customize and optimize each component according to your specific requirements. If you have any questions or need further assistance, feel free to ask!