A LAMP stack is the foundation for hosting dynamic websites and web applications. It includes Linux (the operating system), Apache (the web server), MySQL (the database), and PHP (the programming language).
This guide walks you through installing a LAMP stack on Ubuntu 24.04.
Prerequisites
Before starting, make sure you have:
- A VPS running Ubuntu 24.04 (check our VPS reviews if you don't have one)
- SSH access to your server
- Basic command line knowledge
Step 1: Update Your System
sudo apt update
sudo apt upgrade -y
This updates all existing packages to the latest versions.
Step 2: Install Apache
sudo apt install apache2 -y
To verify Apache is running:
sudo systemctl status apache2
Open your browser and visit your server's IP address. You should see the default Apache welcome page.
To allow HTTP and HTTPS through the firewall:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Step 3: Install MySQL
sudo apt install mysql-server -y
Run the security script to secure your MySQL installation:
sudo mysql_secure_installation
Follow the prompts. Set a root password when asked. Remove anonymous users, disable remote root login, and remove test databases. These are good for security.
Step 4: Install PHP
Install PHP and common extensions needed for web applications:
sudo apt install php php-cli php-curl php-mysql php-gd php-mbstring php-xml php-zip libapache2-mod-php -y
Test PHP processing by creating a test file:
sudo nano /var/www/html/info.php
Add this content:
<?php
phpinfo();
?>
Save and exit. Visit http://your_server_ip/info.php. You should see the PHP information page. Remove this file after testing for security.
Step 5: Configure Apache for Your Site
Apache stores configuration files in /etc/apache2/sites-available/. The default configuration is fine for testing. For production, create a new configuration file for your domain.
sudo nano /etc/apache2/sites-available/yourdomain.conf
Add this configuration (replace yourdomain.com with your actual domain):
<VirtualHost *:80>
ServerAdmin admin@yourdomain.com
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain
<Directory /var/www/yourdomain>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Create the document root directory:
sudo mkdir -p /var/www/yourdomain
Enable the site and disable the default:
sudo a2ensite yourdomain.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
Step 6: Test Your LAMP Stack
Create a test PHP file in your document root:
sudo nano /var/www/yourdomain/index.php
Add this content:
<!DOCTYPE html>
<html>
<head>
<title>LAMP Stack Test</title>
</head>
<body>
<h1>LAMP Stack is Working!</h1>
<?php
echo "<p>PHP is working.</p>";
echo "<p>Server time: " . date("Y-m-d H:i:s") . "</p>";
?>
</body>
</html>
Visit http://yourdomain.com. You should see the test page.
Step 7: Configure MySQL Database for Your Application
Log into MySQL:
sudo mysql -u root -p
Create a database:
CREATE DATABASE myapp_db;
Create a user:
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'strong_password';
Grant privileges:
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Now you have a database ready for your application (WordPress, custom PHP app, etc.).
Troubleshooting Common Issues
Apache fails to start often means a configuration error. Check syntax: sudo apache2ctl configtest
PHP not working may mean you need to install additional modules. Check php -m to see loaded modules.
Permission denied errors can be fixed with:
sudo chown -R www-data:www-data /var/www/yourdomain
sudo chmod -R 755 /var/www/yourdomain
Can't connect to MySQL often means MySQL isn't running. Check status: sudo systemctl status mysql
Next Steps
Now that your LAMP stack is running, you can install applications like WordPress, Laravel, or custom PHP apps. Set up SSL with Certbot for HTTPS, configure regular backups, and monitor your server resources.
Need a VPS to practice on? Check our recommended VPS providers.