How to Set Up a LAMP Stack on Ubuntu in 10 Minutes
Looking to set up a LAMP stack on Ubuntu quickly and efficiently? You’re in the right place! Whether you’re a beginner setting up your first web server or a seasoned developer looking for a fast refresher, this step-by-step guide will walk you through installing a LAMP stack (Linux, Apache, MySQL, PHP) on Ubuntu in just 10 minutes. By the end of this LAMP stack tutorial for Ubuntu, you’ll have a fully functional web development environment ready to host your projects.
In this article, you’ll learn:
- How to install and configure Apache on Ubuntu.
- Steps to set up MySQL for database management.
- How to install PHP and integrate it with Apache.
- Quick tests to ensure your Ubuntu LAMP server setup works perfectly.
Let’s dive in and get your LAMP stack on Ubuntu up and running!
What is a LAMP Stack?
Before we start, let’s briefly cover what a LAMP stack is. LAMP stands for Linux, Apache, MySQL, and PHP—a popular combination of open-source software used to build and host dynamic websites and web applications. Ubuntu, a user-friendly Linux distribution, serves as the foundation, while Apache handles web serving, MySQL manages databases, and PHP powers server-side scripting.
This quick LAMP stack setup guide uses Ubuntu 22.04 LTS, but the steps are adaptable to other versions like 20.04 or 24.04. Ready? Let’s begin!
Prerequisites
To follow this LAMP stack tutorial for Ubuntu, you’ll need:
- A system running Ubuntu (preferably a fresh install).
- Root or sudo privileges.
- A stable internet connection.
- About 10 minutes of your time!
Step 1: Update Your Ubuntu System
First things first—ensure your system is up to date. Open your terminal and run the following commands:
sudo apt update sudo apt upgrade -y
The apt update
command refreshes your package list, while apt upgrade -y
installs the latest updates without prompting for confirmation. This ensures compatibility when you install the LAMP stack on Ubuntu.
Step 2: Install Apache Web Server
Apache is the web server component of the LAMP stack. To install it, run:
sudo apt install apache2 -y
Once installed, start and enable Apache to run on boot:
sudo systemctl start apache2 sudo systemctl enable apache2
To verify Apache is running, open your browser and enter your server’s IP address (e.g., http://your_server_ip
). If you’re on a local machine, use http://localhost
. You should see the default Apache welcome page.
Troubleshooting Tip: If it doesn’t load, ensure your firewall allows HTTP traffic:
sudo ufw allow 'Apache'
Step 3: Install MySQL Database Server
Next, let’s set up MySQL, the database management system. Install it with:
sudo apt install mysql-server -y
Start and enable MySQL:
sudo systemctl start mysql sudo systemctl enable mysql
For security, run the MySQL secure installation script:
sudo mysql_secure_installation
Follow the prompts to:
- Set a root password.
- Remove anonymous users.
- Disallow remote root login.
- Remove test databases.
Your MySQL setup is now secure and ready for your Ubuntu LAMP server setup.
Step 4: Install PHP
PHP is the scripting language that powers dynamic content. Install PHP along with common modules and the Apache PHP module:
sudo apt install php libapache2-mod-php php-mysql -y
This installs PHP, integrates it with Apache, and adds MySQL support. To confirm PHP is working, create a test file:
sudo nano /var/www/html/info.php
Add the following code to the file:
Save and exit (Ctrl+O, Enter, Ctrl+X). Then, restart Apache:
sudo systemctl restart apache2
Visit http://your_server_ip/info.php
in your browser. You’ll see a PHP info page if everything’s set up correctly. For security, delete this file afterward:
sudo rm /var/www/html/info.php
Step 5: Test Your LAMP Stack
Let’s ensure your LAMP stack on Ubuntu works together. Create a simple PHP script with MySQL integration. First, log in to MySQL:
sudo mysql -u root -p
Enter your root password, then create a test database and table:
CREATE DATABASE test_db; USE test_db; CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50)); INSERT INTO users (name) VALUES ('Test User'); EXIT;
Now, create a PHP file to connect to this database:
sudo nano /var/www/html/test.php
Add this code:
Save and exit, then visit http://your_server_ip/test.php
. You should see output like:
ID: 1 - Name: Test User
If you see this, congratulations—your Apache MySQL PHP Ubuntu setup is complete!
Additional Tips for Your LAMP Stack
- Optimize Apache: Tweak
/etc/apache2/apache2.conf
for performance. - Secure PHP: Edit
/etc/php/[version]/apache2/php.ini
to disableexpose_php
. - Learn More: Check out the official Ubuntu Server documentation for advanced configurations.
Conclusion
You’ve just learned how to set up a LAMP stack on Ubuntu in under 10 minutes! From installing Apache to configuring MySQL and PHP, you now have a solid foundation for web development or hosting. This quick LAMP stack setup is perfect for testing projects, building websites, or even deploying small applications.
Have questions or run into issues? Drop a comment below—I’d love to help you troubleshoot your Ubuntu LAMP server setup!
Want more Ubuntu tutorials? Check out my Ubuntu category for additional guides and tips.