Setting up a LAMP stack (Linux, Apache, MySQL, PHP) on Ubuntu is one of the most rewarding first steps for anyone diving into web development. The combination gives you a powerful, open‑source platform that can host anything from a simple personal blog to a full‑blown e‑commerce site. In this guide we’ll walk through each installation and configuration step, explain why each command matters, and highlight the most common snags beginners encounter. By the end you’ll have a fully functional web server ready to serve dynamic PHP pages and store data in MySQL—all on a clean Ubuntu installation.
What You’ll Need
- A fresh Ubuntu 22.04 LTS server (or desktop) with sudo privileges.
- An internet connection for downloading packages.
- A non‑root user account (recommended for security).
- Basic familiarity with the terminal.
- Optional: A domain name or sub‑domain pointing to your server’s IP.
Step 1: Update System Packages
Before installing any software, ensure your package index is up to date and that existing packages are upgraded. Open a terminal and run:
sudo apt update && sudo apt upgrade -y The apt update command refreshes the list of available packages, while apt upgrade applies any pending security patches. Skipping this step can lead to version conflicts later when you try to install Apache or PHP.
Step 2: Install Apache Web Server
Apache is the most widely used web server on Linux. Install it with a single command:
sudo apt install apache2 -y Once installed, start and enable the service so it boots automatically:
sudo systemctl start apache2
sudo systemctl enable apache2 Test the installation by opening a web browser and navigating to http://your_server_ip. You should see the default Ubuntu Apache welcome page. If you see a “Connection refused” error, double‑check that the firewall allows HTTP traffic (see the Tips section).
Step 3: Install MySQL Database Server
MySQL (or its drop‑in replacement MariaDB) stores your application data. Install it with:
sudo apt install mysql-server -y During installation, Ubuntu’s package will set up a default root account that authenticates using the auth_socket plugin, meaning you can log in as root without a password when you’re on the server.
Start and enable MySQL:
sudo systemctl start mysql
sudo systemctl enable mysql Step 4: Secure MySQL Installation
Running mysql_secure_installation hardens the database by setting a root password, removing anonymous users, and disabling remote root logins.
sudo mysql_secure_installation You’ll be prompted with a series of yes/no questions. For a beginner setup, answer as follows:
- Validate password plugin? – y (choose a medium strength level).
- Set root password? – y (enter a strong password).
- Remove anonymous users? – y.
- Disallow root login remotely? – y.
- Remove test database and access to it? – y.
- Reload privilege tables now? – y.
These steps close the most obvious security holes while keeping the configuration simple.
Step 5: Install PHP and Required Modules
PHP interprets the server‑side scripts that generate dynamic content. Install the core package and a few common extensions:
sudo apt install php libapache2-mod-php php-mysql -y The libapache2-mod-php module tells Apache to hand .php files to the PHP interpreter. After installation, restart Apache so it loads the new module:
sudo systemctl restart apache2 Verify PHP is working by creating a info.php file in the web root:
echo "" | sudo tee /var/www/html/info.php Now visit http://your_server_ip/info.php. A page full of PHP configuration details should appear. Delete the file afterward because it reveals sensitive information:
sudo rm /var/www/html/info.php Step 6: Test the Complete LAMP Stack
To confirm that Apache, MySQL, and PHP are cooperating, create a tiny database‑driven script.
sudo bash -c 'cat > /var/www/html/test.php <<"EOF"
connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
echo "Success! MySQL and PHP are talking.
";
?>
EOF'
Replace YOUR_ROOT_PASSWORD with the password you set earlier. Then, in MySQL, create the test database:
sudo mysql -u root -p -e "CREATE DATABASE testdb;"
Open http://your_server_ip/test.php. If you see the success message, your LAMP stack is fully operational.
Common Mistakes to Avoid
Even seasoned developers trip over a few recurring pitfalls when building a LAMP environment on Ubuntu. Below are the most frequent errors and how to sidestep them:
- Skipping
apt updatebefore install: Old package lists can cause dependency conflicts, especially with PHP extensions. - Leaving the default firewall closed: Ubuntu ships with
ufw. If you enable it, remember to allow HTTP (port 80) and HTTPS (port 443):sudo ufw allow in "Apache Full". - Running MySQL as root without a password: The
mysql_secure_installationscript prevents remote root logins and sets a password; never skip it. - Editing
/etc/apache2/apache2.confinstead of/etc/apache2/sites-available/000-default.conffor document root changes: Misplaced edits can break virtual host handling. - Forgetting to restart services after configuration changes: Apache and MySQL won’t pick up new settings until you run
systemctl restartfor each.
Tips and Tricks
Here are a few pro‑level shortcuts that make managing a LAMP stack smoother:
- Enable Apache’s
mod_rewrite: Many PHP frameworks rely on clean URLs. Activate it withsudo a2enmod rewrite && sudo systemctl restart apache2. - Use
systemctl statusto diagnose service issues: Example:sudo systemctl status apache2shows why a service may have failed to start. - Leverage
php -vandapache2 -vfor quick version checks. - Back up your MySQL databases regularly:
mysqldump -u root -p --all-databases > ~/backup.sqlcreates a full dump you can restore later. - Consider installing
phpmyadminfor a graphical MySQL UI:sudo apt install phpmyadminand follow the prompts.
Frequently Asked Questions
Do I need to install MariaDB instead of MySQL?
Ubuntu’s mysql-server package now points to MariaDB by default, which is a drop‑in replacement. Both work with PHP, so you can stick with the default unless you have a specific requirement for Oracle MySQL.
Can I run LAMP on a non‑root user account?
Absolutely. The sudo prefix gives temporary elevated rights for each command, keeping the system more secure. Avoid logging in as root for everyday tasks.
How do I enable HTTPS on my new server?
Use Let’s Encrypt’s Certbot tool: sudo apt install certbot python3-certbot-apache -y then run sudo certbot --apache. Follow the prompts to obtain a free SSL certificate and automatically configure Apache.
Conclusion
Congratulations! You’ve just assembled a fully functional LAMP stack on Ubuntu, complete with a working Apache web server, a secured MySQL database, and a PHP interpreter ready to execute dynamic code. By following the step‑by‑step commands, avoiding the common mistakes listed, and applying the tips and tricks, you now have a solid foundation for any web development project. Keep your system updated, back up your databases, and experiment with frameworks like Laravel or WordPress to get the most out of your new environment. Happy coding!
Photo by Robert Stemler on Unsplash




