🚀 Ubuntu LAMP Stack (Apache, MySQL, PHP) Installation Guide (2025)
By following the steps below, you can quickly install the LAMP stack (Linux, Apache, MySQL, PHP) on your Ubuntu-based server and start running your web applications. LAMP Stack consists of a Linux OS, Apache web server, MySQL database, and PHP scripting language—making it an ideal setup for dynamic websites and applications. This guide is based on Ubuntu 22.04, but also works on Ubuntu 18.04 and newer.🛠️ Step 1: Update the Server
Before installing the LAMP Stack, ensure you have a clean server with Ubuntu OS installed. As this installation is irreversible, it is highly recommended to use an empty or fresh server. Make sure your server is fully updated using the commands below, then proceed with the LAMP Stack installation.
Bash:
apt update -y && apt upgrade -y && apt autoremove -y && apt -y install wget && apt -y install nano && apt -y install curl
⚠️ After running the commands, make sure to answer Yes (yes) to all prompts on screen.
🛠️ Step 2: Install Apache Web Server & Configure the Firewall
Apache is the first component of LAMP and serves your website content to users. It's open-source and easily available from Ubuntu’s repositories.Use the command below to install Apache:
Bash:
sudo apt install apache2
⚠️ After running the commands, make sure to answer Yes (yes) to all prompts on screen.
🔐 Firewall Configuration
By default, Ubuntu uses UFW (Uncomplicated Firewall). To allow HTTP (port 80) and HTTPS (port 443) traffic for Apache, use the following:
Bash:
sudo ufw allow 'Apache Full'
sudo ufw enable
🌍 Test Apache Installation
Once installation and firewall setup are complete, open your server IP address in a browser to verify Apache is working.Try accessing:
http://your_server_ip🛠️ Step 3: Install MySQL Database Server
Most web applications require a database. In LAMP, this is handled by MySQL. Installing MySQL on Ubuntu is straightforward.Use the command below to install MySQL:
Bash:
sudo apt install mysql-server
⚠️ After running the commands, make sure to answer Yes (yes) to all prompts on screen.
Once installation is complete, MySQL will start automatically. Check its status with:
Bash:
sudo systemctl status mysql
🔐 Secure MySQL Configuration
After installing MySQL, it's recommended to run the built-in secure installation script:
Bash:
sudo mysql_secure_installation
⚠️ This script will help you set a root password, remove anonymous users, delete test databases, and disable remote root login. Follow the prompts carefully.
✅ To verify a successful installation, log in to MySQL shell:
Bash:
sudo mysql
If you can access the MySQL shell, everything is working correctly. To exit:
Bash:
exit
🛠️ Step 4: Install PHP & Integrate with Apache
PHP allows your website to become dynamic and interact with databases. In this step, you’ll install PHP and the required modules to work with Apache and MySQL.Use the following command:
Bash:
sudo apt install php libapache2-mod-php php-mysql
⚠️ After running the commands, make sure to answer Yes (yes) to all prompts on screen.
This command installs:
php: Core PHP interpreterlibapache2-mod-php: Lets Apache interpret PHP filesphp-mysql: Enables PHP to communicate with MySQL
⚙️ Optional: Set PHP as Default File Type
Apache determines the default index file from thedir.conf file.To prioritize PHP over HTML:
Bash:
sudo nano /etc/apache2/mods-enabled/dir.conf
Find this line:
Bash:
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
Change it to:
Bash:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
CTRL+X, then Y, then ENTER).Restart Apache to apply:
Bash:
sudo systemctl restart apache2
✅ To test PHP:
Bash:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Visit
http://your_server_ip/info.php — if you see the PHP Info page, PHP is working correctly.⚠️ Note: For security, delete the test file after confirming:
Bash:
sudo rm /var/www/html/info.php
🛠️ Step 5: Create a Virtual Host (Apache Virtual Server)
To host multiple websites on one server, set up Apache Virtual Hosts. In this example, we’ll configure a host forlamp.jetto.net.🧱 1. Create the Directory Structure
Create a separate root directory for each site:
Bash:
sudo mkdir -p /var/www/lamp.jetto.net/public_html
Set correct ownership and permissions:
Bash:
sudo chown -R $USER:$USER /var/www/lamp.jetto.net/public_html
🧾 2. Create the Virtual Host Config File
Navigate to Apache config directory and create the file:
Bash:
sudo nano /etc/apache2/sites-available/lamp.jetto.net.conf
Paste the following:
Bash:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName lamp.jetto.net
ServerAlias www.lamp.jetto.net
DocumentRoot /var/www/lamp.jetto.net/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
✅ 3. Enable the Configuration
Enable the site with:
Code:
sudo a2ensite lamp.jetto.net.conf
sudo systemctl reload apache2
✅ 4. Disable the Default Apache Site
To prevent default Apache page showing athttp://your_server_ip, disable the default site:
Bash:
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
✅ 5. Add index.html to Test the Website
To test the virtual host, create an index.html page.Navigate to the site's directory:
Bash:
cd /var/www/lamp.jetto.net/public_html
Create an index file:
Bash:
nano index.html
Add the content: "website is working", then save and exit.
⚠️ Your basic LAMP setup is now complete. We will explore more advanced topics in future guides.