
Ever faced the challenge of setting up a complete web development environment from scratch? I’ve been there countless times. In this comprehensive guide, I’ll walk you through each step of installing LAMP Stack on Ubuntu Server, whether you’re using AWS EC2, another cloud provider, or your own hardware.
What is LAMP Stack and Why Should You Care?
LAMP stack is the absolute backbone of web development, powering millions of websites worldwide. It’s a combination of:
- Linux (operating system)
- Apache (web server)
- MySQL/MariaDB (database management)
- PHP (The programming language)
While this tutorial focuses on Ubuntu specifically, the concepts apply to most Linux distributions with minor adjustments.
Why Install Components Separately Instead of Using the Bundle?
You might be wondering – “Can’t I just run sudo apt install lamp-server^
and be done with it?” Yes, you absolutely can. But there are compelling reasons why manual installation gives you more control:
- Greater flexibility with updates – PHP, MySQL, and Apache receive frequent vendor updates. Installing separately means you can update individual components when needed rather than waiting for the entire bundle to update.
- Customized installations – If you’re using a separate database server or AWS RDS, you might not need MySQL on your web server at all. Why waste resources?
- Better troubleshooting skills – Understanding each component’s installation process helps tremendously when debugging application errors later.
- Fine-tuned performance – Individual installations allow you to optimize each component for your specific needs.
Let’s dive in!
Step 1: Update Your System
Before installing anything, it’s crucial to update your package lists:
sudo apt update && sudo apt upgrade -y
This ensures you have the latest security patches and package information before proceeding.
Step 2: Installing Apache Web Server
Apache is the foundation of our stack and handles HTTP requests. Let’s install it first:
sudo apt install apache2 apache2-utils -y
The installation process typically takes only a minute or so. Once complete, you can control Apache with these commands:
# Start Apache
sudo systemctl start apache2
# Stop Apache
sudo systemctl stop apache2
# Restart Apache
sudo systemctl restart apache2
# Check status
sudo systemctl status apache2
Code language: PHP (php)
Pro tip 💡: Modern Ubuntu versions use systemctl instead of the older /etc/init.d/ scripts for service management.
After installation, verify Apache is working by navigating to your server’s IP address or domain name in a web browser. You should see the Apache2 Ubuntu Default Page.
Step 3: Installing MySQL Database Server
Next, we’ll install MySQL to handle our data storage needs:
sudo apt install mysql-server -y
Important Security Step: After installation, run the MySQL secure installation script:
sudo mysql_secure_installation
This interactive script guides you through setting a root password, removing anonymous users, disabling remote root login, and removing test databases. Always answer “Y” to these security questions unless you have a specific reason not to.
To verify MySQL installation and access the MySQL console:
sudo mysql
Within the MySQL prompt, you can change the root password, create databases, and more:
-- Change root password
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password';
-- Create a new database
CREATE DATABASE my_website;
-- Create a new user with privileges
CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'user_password';
GRANT ALL PRIVILEGES ON my_website.* TO 'webuser'@'localhost';
FLUSH PRIVILEGES;
-- Exit MySQL
EXIT;
Code language: PHP (php)
Step 4: Installing PHP
Now let’s install PHP and necessary extensions:
sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-json php-zip php-mbstring -y
This command installs PHP with Apache integration and common extensions needed for most web applications.
After installation, restart Apache to apply the changes:
sudo systemctl restart apache2
To verify PHP is working correctly, create a test file:
sudo nano /var/www/html/info.php
Code language: JavaScript (javascript)
Add this code:
<?php
phpinfo();
?>
Code language: HTML, XML (xml)
Save and exit (Ctrl+X, then Y, then Enter). Now visit http://your_server_ip/info.php in your browser. You should see detailed information about your PHP installation.
Security Tip: Remove this file after verification as it exposes detailed server information:
sudo rm /var/www/html/info.php
Code language: JavaScript (javascript)
Step 5: Installing phpMyAdmin (Optional but Recommended)
phpMyAdmin provides a user-friendly web interface for managing your MySQL databases:
sudo apt install phpmyadmin -y
During installation:
- Select “Apache2” when prompted for web server configuration
- Choose “Yes” when asked to configure a database for phpMyAdmin
- Enter a strong password for the phpMyAdmin application password
Modern Ubuntu installations typically integrate phpMyAdmin with Apache automatically. If not, you’ll need to manually enable it:
sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
sudo a2enconf phpmyadmin.conf
sudo systemctl reload apache2
Access phpMyAdmin by visiting http://your_server_ip/phpmyadmin and logging in with your MySQL credentials.
Testing Your LAMP Installation
Let’s verify everything works together. Create a simple test application:
bashsudo nano /var/www/html/db-test.php
Add this code:
<?php
$host = 'localhost';
$user = 'webuser'; // Use the user you created earlier
$pass = 'user_password';
$db = 'my_website';
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Database connection successful!";
$conn->close();
?>
Code language: HTML, XML (xml)
Save the file and access http://your_server_ip/db-test.php in your browser. If everything is set up correctly, you’ll see “Database connection successful!”
Securing Your LAMP Stack On Ubuntu:
Now that you have a working LAMP stack, here are some essential security tips:
- Use UFW firewall to restrict access: bash
sudo ufw allow OpenSSH sudo ufw allow 'Apache Full' sudo ufw enable
- Set proper file permissions: bash
sudo chown -R www-data:www-data /var/www/html/ sudo find /var/www/html/ -type d -exec chmod 755 {} \; sudo find /var/www/html/ -type f -exec chmod 644 {} \;
- Configure Apache security headers in your virtual host files.
- Regularly update all components: bash
sudo apt update && sudo apt upgrade -y
Advanced Configuration Tips
For production environments, consider these additional configurations:
- Set up virtual hosts for multiple websites: bash
sudo nano /etc/apache2/sites-available/example.com.conf
- Enable Apache modules as needed: bash
sudo a2enmod rewrite sudo a2enmod ssl sudo systemctl restart apache2
- Optimize PHP by editing php.ini: bash
sudo nano /etc/php/8.2/apache2/php.ini
Key settings to adjust include memory_limit, upload_max_filesize, and max_execution_time. - Configure MySQL performance by editing my.cnf: bash
sudo nano /etc/mysql/my.cnf
Troubleshooting Common Issues
Apache Won’t Start
sudo journalctl -u apache2.service
Code language: CSS (css)
Check for syntax errors in configuration files.
PHP Scripts Download Instead of Execute
Ensure the PHP module is properly installed and enabled:
sudo a2enmod php8.2
sudo systemctl restart apache2
Code language: CSS (css)
MySQL Connection Errors
Verify your credentials and ensure the MySQL service is running:
sudo systemctl status mysql
Permission Denied Errors
Check file ownership and permissions for your web directory.
Conclusion
Congratulations! You’ve successfully set up a complete LAMP stack on your Ubuntu server. This powerful combination of Linux, Apache, MySQL, and PHP provides the foundation for countless web applications and sites.
Remember that while the individual technologies may evolve over time, the fundamental concepts of the LAMP stack remain incredibly relevant and useful in modern web development.
What’s next? Consider exploring frameworks like Laravel, WordPress, or Drupal that leverage your LAMP environment. Or dive deeper into security hardening practices to keep your stack protected.
Have you encountered any unique challenges setting up a LAMP stack? Comment below and share your experience—I’d love to hear how your journey goes!
Discover more from CodeSamplez.com
Subscribe to get the latest posts sent to your email.
Hi, I’m going to install Ubuntu for LAMP purposes on a Dell Poweredge R300. Will it make much difference its performance if I install the server based O/S instead of Desktop? I like the option of a GUI as well as the command line
actually, performance won’t matter much, I guess, as actually Ubuntu don’t have very much differences in server vs desktop version. Real differences are in the software. If you install the server version, you won’t get the GUI by default, though you will be able to get it installed yourself later. You can setup the development environment on the desktop version without any issue and what which run on this environment will run on server version OS environment as well. Hope this make sense. Thanks.
Thanks for the tutorial – I used it to successfully install LAMP on a EC2 instance on AWS. I have just one suggestion: just before the line “Now you should be able to load phpmyadmin on your server…”, you should mention that Apache needs to be restarted for the change in the config file to be activated.