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.
LAMP stack is the absolute backbone of web development, powering millions of websites worldwide. It’s a combination of:
While this tutorial focuses on Ubuntu specifically, the concepts apply to most Linux distributions with minor adjustments.
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:
Let’s dive in!
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.
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 apache2Code 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.
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) 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.phpCode 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.phpCode language: JavaScript (javascript) phpMyAdmin provides a user-friendly web interface for managing your MySQL databases:
sudo apt install phpmyadmin -y During installation:
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.
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!”
Now that you have a working LAMP stack, here are some essential security tips:
sudo ufw allow OpenSSH sudo ufw allow 'Apache Full' sudo ufw enablesudo 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 {} \;sudo apt update && sudo apt upgrade -yFor production environments, consider these additional configurations:
sudo nano /etc/apache2/sites-available/example.com.confsudo a2enmod rewrite sudo a2enmod ssl sudo systemctl restart apache2sudo nano /etc/php/8.2/apache2/php.ini Key settings to adjust include memory_limit, upload_max_filesize, and max_execution_time.sudo nano /etc/mysql/my.cnfsudo journalctl -u apache2.serviceCode language: CSS (css) Check for syntax errors in configuration files.
Ensure the PHP module is properly installed and enabled:
sudo a2enmod php8.2
sudo systemctl restart apache2Code language: CSS (css) Verify your credentials and ensure the MySQL service is running:
sudo systemctl status mysql Check file ownership and permissions for your web directory.
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!
Ever wondered what happens when you run Python code? The Python runtime environment—comprising the interpreter, virtual machine, and system resources—executes your code through bytecode compilation…
Tired of repetitive tasks eating up your time? Python can help you automate the boring stuff — from organizing files to scraping websites and sending…
Learn python file handling from scratch! This comprehensive guide walks you through reading, writing, and managing files in Python with real-world examples, troubleshooting tips, and…
This website uses cookies.
View Comments
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.