LAMP stack stands for Linux, Apache, MySQL, and PHP, a widely used combination for hosting and developing dynamic websites on Ubuntu servers.

This guide walks through installing each LAMP component on Ubuntu 24.04, creating a test PHP page, configuring phpMyAdmin, and testing MySQL.

Quick Answer

Install Apache (sudo apt install apache2), PHP (sudo apt install libapache2-mod-php -y), and MySQL (sudo apt install mysql-server) in sequence.

Then install phpMyAdmin (sudo apt install phpmyadmin), restart Apache, and open localhost/phpmyadmin in Firefox to confirm the stack is running.

How to Install LAMP Stack on Ubuntu 24.04

Step 1: Install Apache

Run the command below in Terminal to install the Apache web server from Ubuntu’s apt repository. Press Y if prompted to confirm the installation.

sudo apt install apache2
Terminal installing Apache 2 web server on Ubuntu 24.04 using apt install apache2

Step 2: Test the Apache Server

Open Firefox and enter localhost in the address bar. The Apache default welcome page confirms the server is installed and listening on port 80.

Firefox browser showing the Apache 2 default welcome page at localhost on Ubuntu 24.04

Step 3: Install PHP

After confirming Apache is running, install the PHP module for Apache. The -y flag accepts all prompts automatically so no interaction is needed.

sudo apt install libapache2-mod-php -y
Terminal installing libapache2-mod-php to add PHP support to Apache on Ubuntu 24.04

Step 4: Restart Apache

Restart Apache after installing PHP so the server loads the new module. The command exits silently if successful, which is the expected behavior.

sudo systemctl restart apache2
Terminal restarting Apache service with systemctl restart apache2 on Ubuntu 24.04

Step 5: Configure PHP

Create a PHP test file in the Apache web root to confirm PHP is working correctly. The gnome-text-editor command opens it in a GUI text editor.

sudo gnome-text-editor /var/www/html/phpinfo.php
Terminal command to create phpinfo.php file in the Apache web root using gnome-text-editor

Add the line below to the phpinfo.php file and save it. Opening localhost/phpinfo.php in Firefox will then display a full PHP configuration page.

<?php phpinfo() ?>
gnome-text-editor showing phpinfo.php file with PHP phpinfo function code

Enter the URL below in Firefox to verify PHP is configured correctly. A PHP information page confirms the integration between Apache and PHP.

localhost/phpinfo.php
Firefox displaying the PHP information page at localhost/phpinfo.php confirming Apache and PHP integration

Step 6: Verify PHP Version

Run the command below in Terminal to display the PHP version installed on Ubuntu 24.04. The output includes the version number and build date.

php -v
Terminal showing PHP version output from php -v command on Ubuntu 24.04

Step 7: Install MySQL

Install MySQL server from the Ubuntu repository. Press Y to confirm when prompted. MySQL provides the database layer for the LAMP stack.

sudo apt install mysql-server
Terminal installing MySQL server on Ubuntu 24.04 using apt install mysql-server

Step 8: Install MySQL Client and PHP Extensions

Install the MySQL client and the PHP-MySQL extension for your PHP version. The extension allows PHP scripts to connect to the MySQL database.

sudo apt install mysql-client
Terminal installing mysql-client package on Ubuntu 24.04 using apt

Install php8.3-mysql to link PHP with MySQL. Adjust the version number to match the PHP version installed on your system if it differs.

sudo apt install php8.3-mysql

Also install php8.3-curl, which enables PHP to make HTTP requests. Replace 8.3 with your installed PHP version if the version number differs.

sudo apt install php8.3-curl
Terminal installing php8.3-curl extension on Ubuntu 24.04 using apt

Step 9: Verify MySQL

Check the MySQL service status with systemctl to confirm it is active and running. A green active (running) status confirms the installation succeeded.

sudo systemctl status mysql
Terminal showing MySQL service status as active and running after installation on Ubuntu 24.04

Step 10: Install phpMyAdmin

Install phpMyAdmin to manage MySQL databases through a web interface. During setup, select apache2 when prompted for the web server to configure.

sudo apt install phpmyadmin
Terminal installing phpMyAdmin on Ubuntu 24.04 using apt install phpmyadmin

On the configuration screen, select the apache2 option and press Enter to confirm. Accept the automatic database configuration on the next screen.

phpMyAdmin setup dialog prompting to select apache2 as the web server to configure
phpMyAdmin configuration dialog for automatic database setup during installation

Enter a password for the phpMyAdmin database user and confirm it. Keep this password as you will need it to log in to the web interface.

phpMyAdmin setup dialog prompting to enter and confirm the application password

Restart Apache after the phpMyAdmin installation, then open localhost/phpmyadmin in Firefox to confirm the web interface is accessible.

sudo systemctl restart apache2
Terminal restarting Apache service after phpMyAdmin installation on Ubuntu 24.04
localhost/phpmyadmin
Firefox showing the phpMyAdmin login interface accessible at localhost/phpmyadmin

To find the phpMyAdmin credentials, open the config file below. The file shows the username and password assigned during the setup process.

sudo gnome-text-editor /etc/phpmyadmin/config-db.php
Terminal command to open phpMyAdmin config-db.php file in gnome-text-editor
gnome-text-editor showing phpMyAdmin config-db.php file with database username and password

Enter these credentials on the phpMyAdmin login page in Firefox and click Log in to access the database management interface.

phpMyAdmin login page in Firefox showing username and password fields
phpMyAdmin database management dashboard after successful login on Ubuntu 24.04

How to Create a Custom Web Page with LAMP

Use the LAMP stack to host dynamic multi-page websites by creating HTML files in the Apache web root directory at /var/www/html.

Step 1: Create an HTML Page

Create an HTML file in the web root using gnome-text-editor. Add basic HTML markup, save the file, then visit localhost/filename.html in Firefox.

sudo gnome-text-editor /var/www/html/mywebpage.html
Terminal command to create a custom HTML file in the Apache web root on Ubuntu 24.04
<!Doctype html>
<html>
<head>
<title>Installing the Lamp Stack on Ubuntu 24.04</title>
</head>
<body>
<h1>This is the first page</h1>
</body>
</html>
gnome-text-editor showing custom HTML webpage code for the Apache web root
localhost/mywebpage.html
Firefox displaying a custom HTML web page served by Apache at localhost/mywebpage.html

How to Fix phpMyAdmin 404 Error

If localhost/phpmyadmin returns a 404 error, Apache is not loading the phpMyAdmin configuration. Fix it by adding an Include directive manually.

Step 1: Edit apache2.conf

Open the Apache main configuration file in gnome-text-editor. Append the Include line for phpMyAdmin at the bottom of the file and save.

sudo gnome-text-editor /etc/apache2/apache2.conf
Include /etc/phpmyadmin/apache.conf

Step 2: Restart Apache and MySQL

Restart Apache and MySQL after editing the configuration. Return to localhost/phpmyadmin in Firefox to confirm the 404 error is resolved.

sudo systemctl restart apache2
sudo /etc/init.d/mysql restart
Terminal showing MySQL service restart output using the init.d script on Ubuntu 24.04

How to Test MySQL Integration with PHP

Test that PHP can connect to MySQL by creating a database, a user, and granting permissions, then logging in from a new MySQL session.

Step 1: Open MySQL Shell

Run sudo mysql to open the MySQL interactive shell with root privileges. The shell accepts SQL commands for database administration.

sudo mysql
Terminal showing the MySQL interactive shell prompt after running sudo mysql

Step 2: Create a Database and User

Create a new database, then create a user with a password and grant that user full access to the database with the commands below.

CREATE DATABASE DATABASE_NAME;
CREATE USER 'USER1'@'%' IDENTIFIED BY 'password';
GRANT ALL ON DB1.* TO 'USER1'@'%';
MySQL shell showing CREATE USER command being executed for a new database user

Step 3: Verify Database Access

Exit the root shell, then log in as the new user with sudo mysql -u user1 -p. Run SHOW DATABASES to verify the user can see the database.

sudo mysql -u user1 -p
Terminal logging in to MySQL as a custom user using sudo mysql -u user1 -p
SHOW DATABASES;

When LAMP Stack Is the Right Tool

LAMP stack is the right tool when you need to run PHP-based applications like WordPress, Drupal, or Laravel on an Ubuntu server or local machine.

For projects using Node.js, Python, or other stacks, Apache and MySQL can still serve as the underlying infrastructure alongside those runtimes.

Related Guides