Skip to main content

[For Ubuntu/Debian] How To Install Apache, MySQL, PHP (LAMP) Stack

Updated over a week ago

Introduction

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

In this quick and easy guide, you will learn how to install and download LAMP on an Ubuntu 22.04 / Debian 12 server.

1. Install Apache web server

The Apache web server is currently the most popular web server in the world, which makes it a great default choice for hosting a website.

We can install Apache easily using Ubuntu's package manager, apt. A package manager allows us to install most software pain-free from a repository maintained by Ubuntu. You can learn more about how to use apt here.

Updating server packages:

sudo apt update
sudo apt upgrade

Installing Apache:

sudo apt install apache2

After that, your web server is installed.

Verify that the Apache service is running:

sudo systemctl status apache2

Output:


You can also do a spot check right away to verify that everything went as planned by visiting your server's public IP address in your web browser:

2. Install MySQL

Now that we have our web server up and running, it is time to install MySQL. MySQL is a database management system. Basically, it will organize and provide access to databases where our site can store information.

Again, we can use apt to acquire and install our software. This time, we'll also install some other "helper" packages that will assist us in getting our components to communicate with each other:

sudo apt install mysql-server

Verify the installation with:

mysqld --version

To secure the installation, run the following command:

sudo mysql_secure_installation

You will be asked to enter the password you set for the MySQL root account. Next, it will ask you if you want to change that password. If you are happy with your current password, type "n" for "no" at the prompt.

For the rest of the questions, you should simply hit the "ENTER" key through each prompt to accept the default values. This will remove some sample users and databases, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.

At this point, your database system is now set up, and we can move on.

3. Install PHP

PHP is the component of our setup that will process code to display dynamic content. It can run scripts, connect to our MySQL databases to get information, and hand the processed content over to our web server to display.

We can once again leverage the apt system to install our components. We’re going to include some helper packages as well:

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

Once it's completed, run the following command to verify the PHP version:

php -v


Did this answer your question?