How To Install Linux, Apache, MySQL and PHP (LAMP) stack on Ubuntu 22.04

Published on  

7 min read

Looking for other version?

Choose a different version or distribution.

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

The LAMP stack (Linux, Apache, MySQL, and PHP) is a prominent open-source software stack used to build dynamic webpages and web applications.

A "LAMP" stack is a collection of open source software that is often deployed together on a server to allow it to run dynamic websites and PHP-based web apps. This is an abbreviation for both the Linux operating system and the Apache web server. The site's data is kept in a MySQL database, and dynamic content is handled by PHP.

  • Option 1: Installing a LAMP Stack Quickly With Tasksel
  • Option 2: Installing Apache Server, MySQL and PHP separately

Prerequisites

  • A server with Ubuntu 22.04 as OS
  • User privileges: root or non-root user with sudo privileges

Option 1: How To Install A LAMP Stack Quickly With Tasksel

Tasksel is a command-line utility for installing a group of packages used for specific tasks on Debian-based systems. Tasksel offers a variety of group packages for quickly configuring your server, such as "LAMP Server," "DNS Server," "Mail Server," and "Tomcat Java Server," among others. This article will walk you through installing tasksel and then quickly installing LAMP using tasksel on Debian, Ubuntu, and LinuxMint.

Step 1: Install Tasksel

The tasksel package is available under default repositories on most of Ubuntu, Debian, and LinuxMint operating systems. Use the following command to install it.

sudo apt-get install tasksel

Step 2: Install LAMP using Tasksel

Let's install LAMP setup on your Ubuntu, Debian, or LinuxMint system after you've installed the tasksel package. To install lamp-server, run the tasksel command in the terminal with sudo privileges.

sudo tasksel install lamp-server

The installation will take only a few minutes. The MySQL server is configured without a password for the root user. MySQL can be accessed without a password.

Then execute sudo mysql_secure_installation to set a new password and secure MySQL installation through a command line wizard.

Step 3: Testing the LAMP Stack

Congratulation’s your LAMP setup has completed. To verify open your web browser and access http://localhost or http://your_server_ip. You will see Apache 2 Default Page screen. Thus, it is showing that Apache is working fine.

Option 2: Installing Apache, MySQL and PHP Separately On Ubuntu 22.04

LTS (long term support) releases of Ubuntu are among the most popular Linux operating systems used in LAMP stacks. In this step, we'll install and configure a working LAMP stack on Ubuntu 22.04 (Jammy Jellyfish) LTS.

Step 1: Update your System

Before you install any software, it's important to ensure that your Ubuntu 22.04 system is up to date. Open a terminal window and run the following command:

sudo apt update

This command will update your package list and retrieve information on the latest versions of software packages available in Ubuntu's repositories.

Step 2: Install Apache

Apache is the most widely used web server software in the world. To install Apache on Ubuntu 22.04, run the following command:

sudo apt install apache2

This command will install the Apache web server and its dependencies.

Step 3: Install MySQL

MySQL is a popular open-source relational database management system that's used to store and retrieve data for web applications. To install MySQL on Ubuntu 22.04, run the following command:

sudo apt install mysql-server

During the installation process, you'll be prompted to set a password for the MySQL root user.

Step 4: Install PHP

PHP is a server-side scripting language that's used to create dynamic web content. To install PHP on Ubuntu 22.04, run the following command:

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

This command will install PHP along with the Apache module for PHP and the MySQL extension for PHP.

Configuring Firewall (Optional)

By default, Ubuntu's uncomplicated firewall a.k.a. ufw is disabled. You can enable ufw to implement firewall policies and help secure your system. If you choose to leave ufw disabled, you can skip this step.

If you're not sure if ufw is enabled on your system, you can check its status with this command:

sudo ufw status

If ufw is enabled, there are three available applications (listed in the output of sudo ufw app list) related to Apache you can allow through the firewall:

  • Apache will allow traffic on TCP port 80 (default HTTP) only
  • Apache Secure will allow traffic on TCP port 443 (default HTTPS) only
  • Apache Full will allow traffic on both TCP ports 80 and 443

For our example, will allow inbound traffic to Apache on TCP ports 80 and 443 with this command:

sudo ufw allow in "Apache Full"

You should see output similar to:

// Output

Rule added
Rule added (v6)

How To Find your Server’s Public IP Address

There are several methods for determining your server's public IP address if you do not know it. This is usually the address you use to connect to your server via SSH.

There are several ways to accomplish this from the command line. To begin, you could use the iproute2 tools to obtain your IP address by entering the following:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

This will return two or three lines. They are all valid addresses, but your computer may only be able to use one of them, so try them all.

Another option is to use the curl utility to contact an outside party and ask it how it sees your server. This is accomplished by requesting your IP address from a specific server:

curl http://icanhazip.com

Testing the Installation

To verify that the LAMP stack has been installed correctly, you can create a PHP file and place it in the Apache document root directory. To do this, open a text editor and create a file called info.php with the following content:

<?php
phpinfo();
?>

Save this file as info.php and copy it to the Apache document root directory:

sudo cp info.php /var/www/html/

Then, open a web browser and navigate to http://localhost/info.php or http://your_server_ip/infp.php. If the installation was successful, you should see a page with detailed information about the PHP configuration and modules.

Conclusion

We've taught you how to install the LAMP stack on Ubuntu 22.04 (Jammy Jellyfish) in this post. Following these procedures will result in a robust web development environment ready for the creation of dynamic websites and online apps. The LAMP stack is a must-have tool for constructing modern online applications, whether you're a newbie or an experienced developer.

About the Author

Rexposed Staff

Was this helpful?