How to Install LAMP Stack on Ubuntu: A Complete Step-by-Step Guide

Introduction

A LAMP stack (Linux, Apache, MySQL, PHP) is the foundation of countless web applications, from WordPress and Joomla to custom PHP frameworks like Laravel. This guide will walk you through installing and configuring each component on an Ubuntu system (versions 20.04, 22.04, or 24.04).

By the end, you’ll have a fully functional web server ready to host dynamic websites and applications.

Prerequisites

An Ubuntu server or desktop machine (fresh install recommended)

A user account with sudo privileges

Internet connection

Terminal access

Step 1: Update System Packages

Always start by updating the package index and upgrading existing packages:

sudo apt update
sudo apt upgrade -y

Step 2: Install Apache (The Web Server)

Apache is the most widely used web server. Install it with:

sudo apt install apache2 -y

After installation, Apache should start automatically. Verify it’s running:

sudo systemctl status apache2

You can also test by opening a browser and visiting http://your_server_ip. You should see the default Apache2 Ubuntu welcome page.

Useful Apache commands:

  • Stop: sudo systemctl stop apache2
  • Start: sudo systemctl start apache2
  • Restart: sudo systemctl restart apache2
  • Enable on boot: sudo systemctl enable apache2

Step 3: Install MySQL (The Database Server)

MySQL is a popular relational database management system. Install it with:

sudo apt install mysql-server -y

Once installed, run the security script to remove insecure defaults:

sudo mysql_secure_installation

You’ll be prompted to:

  • Set a password validation policy
  • Set a root password (if not already set)
  • Remove anonymous users
  • Disallow root login remotely
  • Remove test databases
  • Reload privilege tables

Answer Y (yes) to most questions for a secure setup.

Log in to MySQL as root to test:

sudo mysql -u root -p

Enter your root password when prompted. Then exit with:

EXIT;

Step 4: Install PHP (The Scripting Language)

sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y

After installation, verify the PHP version:

php -v

Restart Apache to ensure it recognizes PHP:

sudo systemctl restart apache2

Step 5: Test PHP Processing

Create a simple PHP info file to verify Apache can handle PHP:

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

Add the following content:

<?php
phpinfo();
?>

Save and exit (Ctrl+O, Enter, Ctrl+X).

Now visit http://your_server_ip/info.php in your browser. You should see the PHP configuration page.

Security note: Delete this file after testing to prevent exposing sensitive server info:

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

Step 6: Configure Apache to Prefer PHP Files (Optional)

By default, Apache prioritizes index.html over index.php. To change this, edit the dir.conf file:

sudo nano /etc/apache2/mods-enabled/dir.conf

Change this line:

DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

To this (moving index.php to the front):

DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

Save the file and restart Apache:

sudo systemctl restart apache2

Step 7: Test Database Connectivity from PHP

Create a test PHP script to ensure PHP can connect to MySQL:

sudo nano /var/www/html/testdb.php

Add this content (replace 'your_password' with your MySQL root password):

<?php
$servername = "localhost";
$username = "root";
$password = "your_password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully to MySQL!";
?>

Visit http://your_server_ip/testdb.php. You should see a success message.

Delete the test file afterward:

sudo rm /var/www/html/testdb.php

Step 8: Set Up Virtual Hosts (Multiple Websites)

Instead of placing everything in /var/www/html, you can host multiple sites using virtual hosts.

Create a site directory:

sudo mkdir -p /var/www/example.com/public_html

Assign ownership:

sudo chown -R $USER:$USER /var/www/example.com/public_html

Create a sample index file:

nano /var/www/example.com/public_html/index.html

Add some HTML content.

Create a virtual host configuration file:

sudo nano /etc/apache2/sites-available/example.com.conf

Add:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example.com/public_html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the site and disable the default:

sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2

Step 9: Secure MySQL (Additional Tips)

  • Create a dedicated database user for your application (don’t use root in production).
  • Use strong passwords.
  • Regularly back up databases with mysqldump.

Example of creating a new user:

CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;

Step 10: Enable Firewall (If Using UFW)

If UFW is enabled, allow HTTP/HTTPS traffic:

sudo ufw allow 'Apache Full'
sudo ufw reload

Check status:

sudo ufw status

Troubleshooting Common Issues

ProblemLikely Solution
Apache won’t startCheck config: sudo apache2ctl configtest
PHP file downloads instead of executingEnsure libapache2-mod-php is installed and Apache restarted
MySQL connection refusedVerify MySQL is running: sudo systemctl status mysql
Permission denied errorsCheck file ownership: ls -la /var/www/html

Conclusion

Congratulations! You’ve successfully installed a LAMP stack on Ubuntu. Your server is now capable of hosting dynamic PHP applications with a MySQL backend.

What’s next?

  • Install phpMyAdmin for a web-based database manager
  • Set up SSL/TLS using Let’s Encrypt (sudo apt install certbot python3-certbot-apache)
  • Deploy a CMS like WordPress or Drupal

Remember to keep your system updated regularly:

sudo apt update && sudo apt upgrade -y

Happy hosting!

Leave a comment

Your email address will not be published. Required fields are marked *