https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_b964594d3d957944241961017b9eb19bf02834de44cce93d8e67dd306852dbe346167181e455e33d5268ea01d973d77bb056848546f31794f31a4c31a9da5aa3.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_23f1ae74c634d7e5e0a067c22b7a8c2d79c3ffd9a3b9395fc82c1b3b99635552b994f1f72f532f28ceaff1ea054ea026cd488cd62fa03a4ad91d212b5f3c5a72.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_e6b7e0bf68aa4a61d5c6a0065ec42e38a0cc53e39a4fbee057b72d4b2297b37c01e716e1e61bac7f240b5a0edbb178d37b62f7ed4ea4ea3d10e46dbe7429f326.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_bfff9e63e857e9ee612e292d4a6edf3ced64d6a756925c953a9d8f77845ff601eca64d73dfa48756b1a9f4a4d6de6127a273bcde16ddeb71a22383460f4e94b0.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_f4dd7e1d73ae5eda35ed5ad6aa965b612dbf483ece3ca50c1e8e30ad8dff1c66a160ed75e958e2db399661d229874783e0834ad813a479437035666b8e9e3386.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_4fce0769137d4cd096989b0349bc3c2bbfca79ac311fdf714c41ab24d87551c7b49b756c8a8de090b0714a0ad0560e49fa532ba5a88875ea4afd78efac464df6.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_85cec8b07d60426b11040e471babca0d2f9c8dc87a9b56e06cad39828f7f67179e29609100f282a574872c9a93fb635b25416300eb4c97bc5a653d00cf6f8dbf.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_6768e5a27d4d357347338621c0d20bd269b126d30eec796193390f2f530fbaea60af84130c46f9786114be65149e661e87d55c339219c90aa76396d7e5b734ef.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_2acd6bdff3b680341e8c727da5169a647123eb8fd0a90253161b4c3af272c15d293bf9bb217008bb13f84d1910b0e166798001f8603b6c026d5c20a76c41d47c.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_031f200f8e99767f9935a89593e093a4d59081f7a7e9654342649007fde8ab4a1422dba552a6cff4984ab16f2d462764622ba122dbaae86691a47e508777ed17.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_268c9bba6ba649318f0da28c37b09a9bbfa371210f9b6b52faa7fd8ae94abf6b3c3bfeb5df5705c93495ce1152ca58aeabc435d6c6c1bd959025165c3f50e086.js
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • Advanced Python Topics
    • AWS Learning Roadmap
    • JWT Complete Guide
    • Git CheatSheet
  • Explore
    • Programming
    • Development
      • microservices
      • Front End
    • Database
    • DevOps
    • Productivity
    • Tutorial Series
      • C# LinQ Tutorials
      • PHP Tutorials
  • Dev Tools
    • JSON Formatter
    • Diff Checker
    • JWT Decoder
    • JWT Generator
    • Base64 Converter
    • Data Format Converter
    • QR Code Generator
    • Javascript Minifier
    • CSS Minifier
    • Text Analyzer
  • About
  • Contact
CodeSamplez.com

CodeSamplez.com

Programming And Development Resources

You are here: Home / DevOps / Installing LAMP Stack on Ubuntu: The Ultimate Guide

Installing LAMP Stack on Ubuntu: The Ultimate Guide

Updated May 6, 2025 by Rana Ahsan 3 Comments ⏰ 7 minutes

Installing LAMP Stack On Ubuntu

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:

  1. 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.
  2. 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?
  3. Better troubleshooting skills – Understanding each component’s installation process helps tremendously when debugging application errors later.
  4. 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 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.

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.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)

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:

  1. Use UFW firewall to restrict access: bashsudo ufw allow OpenSSH sudo ufw allow 'Apache Full' sudo ufw enable
  2. Set proper file permissions: bashsudo 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 {} \;
  3. Configure Apache security headers in your virtual host files.
  4. Regularly update all components: bashsudo apt update && sudo apt upgrade -y

Advanced Configuration Tips

For production environments, consider these additional configurations:

  1. Set up virtual hosts for multiple websites: bashsudo nano /etc/apache2/sites-available/example.com.conf
  2. Enable Apache modules as needed: bashsudo a2enmod rewrite sudo a2enmod ssl sudo systemctl restart apache2
  3. Optimize PHP by editing php.ini: bashsudo nano /etc/php/8.2/apache2/php.ini Key settings to adjust include memory_limit, upload_max_filesize, and max_execution_time.
  4. Configure MySQL performance by editing my.cnf: bashsudo nano /etc/mysql/my.cnf

Troubleshooting Common Issues

Apache Won’t Start

sudo journalctl -u apache2.serviceCode 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 apache2Code 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!

Share if liked!

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Pocket (Opens in new window) Pocket

You may also like


Discover more from CodeSamplez.com

Subscribe to get the latest posts sent to your email.

First Published On: March 13, 2013 Filed Under: DevOps Tagged With: apache, mysql, php, ubuntu

About Rana Ahsan

Rana Ahsan is a seasoned software engineer and technology leader specialized in distributed systems and software architecture. With a Master’s in Software Engineering from Concordia University, his experience spans leading scalable architecture at Coursera and TopHat, contributing to open-source projects. This blog, CodeSamplez.com, showcases his passion for sharing practical insights on programming and distributed systems concepts and help educate others.
Github | X | LinkedIn

Reader Interactions

Comments

  1. Craig Wellman says

    April 28, 2013 at 10:08 AM

    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

    Reply
  2. Md Ali Ahsan Rana says

    April 28, 2013 at 9:43 PM

    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.

    Reply
  3. Jim says

    July 26, 2014 at 6:50 AM

    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.

    Reply

Leave a ReplyCancel reply

Primary Sidebar

  • Facebook
  • X
  • Pinterest
  • Tumblr

Subscribe via Email

Top Picks

python local environment setup

Python Local Development Environment: Complete Setup Guide

In-Depth JWT Tutorial Guide For Beginners

JSON Web Tokens (JWT): A Complete In-Depth Beginners Tutorial

The Ultimate Git Commands CheatSheet

Git Commands Cheatsheet: The Ultimate Git Reference

web development architecture case studies

Web Development Architecture Case Studies: Lessons From Titans

static website deployment s3 cloudfront

Host Static Website With AWS S3 And CloudFront – Step By Step

Featured Dev Tools

  • JWT Decoder
  • Diff Checker

Recently Published

service worker best practices

Service Worker Best Practices: Security & Debugging Guide

advanced service worker features

Advanced Service Worker Features: Push Beyond the Basics

service worker framework integration

Service Workers in React: Framework Integration Guide

service worker caching strategies

Service Worker Caching Strategies: Performance & Offline Apps

service worker lifecycle

Service Worker Lifecycle: Complete Guide for FE Developers

Footer

Subscribe via Email

Follow Us

  • Facebook
  • X
  • Pinterest
  • Tumblr

Demos

  • Demo.CodeSamplez.com

Explore By Topics

Python | AWS | PHP | C# | Javascript

Copyright © 2025

https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_98a75bbad16ef2337949ffaec061e79878ce01a03e25c8bd968e7d82d707a296bc0ba226e6e85729d42e2fecff1440d4357cb04e0214e199c56fd55b562df2fe.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_c402e38f1879c18090377fb6b73b15ac158be453ecda3a54456494fe8aba42b990c293bae5424e5643d52515ffc2067e0819995be8d07d5bba9107a96780775c.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_ffc3511227531cc335353c54c3cbbaa11d0b80e5cb117478e144436c13cd05495b67af2e8950480ed54dbdabcdcef497c90fdb9814e88fe5978e1d56ce09f2cf.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_d57da9abfef16337e5bc44c4fc6488de258896ce8a4d42e1b53467f701a60ad499eb48d8ae790779e6b4b29bd016713138cd7ba352bce5724e2d3fe05d638b27.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_edc0e9ef106cc9ef7edd8033c5c6fcff6dc09ee901fd07f4b90a16d9345b35a06534f639e018a64baaf9384eee1df305570c1ecad747f41b787b89f53839962b.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_5a3aa28cd4eb24db3e3e2acd58230ff5cdc78120b94c2b118227d4c7806ecac03978e1a10404240314f66a3b82af8823768abb8b9eccc5003d198eb077ea12b8.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_dccc492dbbfdac33d1411f9df909e849c7268fcf99b43007f278cde3a0adc0ae00e8cae5ec81cf255b9a6eae74e239ba1fa935572af77173219cb081f7d2327d.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_00bacf9e36181aac2b666d110cd9d82257f846766e7041b2d7b3c909b458982931ccc9b203e37098fbdfcf43ca359cf04e3824a724a6789fc204196d3a72ad29.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_0912e3b2f9f766dd35322f58ab36173717f6452a305c72752abd37ecb789c8ba2a535cf8bb24d713f6fb20bd209e4cb4c35909935dba268d3381a77bea313059.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_aa5a5d229b421633f4247380e1e8c0a4854f82efb35d13a5b07b7b8fbe22e98842a580f063e5965345a51c477a7f5c2585edf8dd7d896b2438dc61f91d8d970c.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_bb8058a9e234a7ffaa98891b1df7f6b8e67410e6984568b151daa05113b8c7f89d7b5918ae73f020998a16f7f5a087a13d6a9a5e5d7c301e2ca12fd9d1f8d177.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_647fb67570c6108fb10ae6785a1abdbecac99ffcf80351d0bef17c3cf783dce497b1895fcdaae997dacc72c359fbfb128cc1540dd7df56deb4961e1cd4b22636.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_f7a298a0f1f754623fe3b30f6910ce2c1373f715450750bd7a391571812b00df1917e2be90df6c4efc54dbdfda8616278a574dea02ba2c7a31992768df8db334.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_df30604d5842ef29888c3c1881220dc6d3f8854666d94f0680c5f38aa643c5fb79b10eb9f10998d8856eb24ca265783195937434fd6c2bb8e4846df0277a7fb7.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_f17fe6fb0993f1703181d7ae9e9ea570f3d33a43afd6f2a4567daa1a6745698c7b8193dc72d50991d2dd87cd3dcf663959206607d193a9b57926d061a1f50aef.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_945dcbab2c2a131f3c90f4fb91776b76066d589f84fb55bff25cd5d79a56218000616bfca1f0af9a74f32348693707af49e8fe624de8aa34f1e1c5b6a25709cf.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_65820d252e1b93596de6697fd5f02483f3e2524a0696c7d698b64745edb32bf5831a90e556842f5f88c8209766cc78ca3a41cf783d20236a9f90d4a7ea7b3e72.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_7286884797a1210857e2a36f8ab46604b0034b6abf512380447a5763c873db6a72b8547f660053de0ea69faef1eb64878f39ff4b0ea86c963efab95764a3bf5b.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_cbcf6c279ac6c6a25ae138bf964e64a5fd90d22dcdf8a53b6fe7b72cefa51063bfb0181a6e50dd2acdcae2795619887d1d83b10461e44e5103be756f2588d837.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_47965bc586b95810c925b9df3314e0c9a5cd121e70ca0831f87df0bc034695de4f83ecf2def86f737e14614ee138794473cf32cd3082a5d38db9dec0c1f266fa.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_12aa201cea075846d266536aa222d64d4088b851d87f55dac5e611b77add6826c8ebc6e82650fcd1a9e88a05a0072dedd195719c5f64cd4580a0acd8aee05d92.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_7859317dea28a85c983d7b2a933704b193600b52929d2d894deae21a5d78f1f9715214d4c2ed1b925e9183146806725621d586779705dea3b651260eb53a2f8a.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_2aa84d6c5c62787d2f29867b4969d0fa94c42ce39434cffe9aab388d976828b1e7945f7a906afc229ce0655223221863a8a0486a48268a3205bf70e03efc8e2d.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_d87ea86dd0e7ecdd5fe7a5bb67becf943e57c3add866b456034d51663d099031bd563e12f61fdccc044969adf938a8584ed22ccd401ab8b669e20e4f92fb54e8.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_35311c3d71a3605fad4e1d6b50f3911311cdcc46418bdf56d6d0308a75a69585269ee7582a335e29989adf308fa1a81a10a2c2d4e257e9d680447a4996f6269e.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_f4fc182ef03c12e9dcadd6febc3dbaa4a29134469057ca9e8ec0be2f2de29a494514ff4b59798e74debf26f78b2df2b3e2665c69b77035761fb463b783202915.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_85c0f2769456e60153b0fd8364b82a035da53384f62de342d9bdca806f3f1ea56486919a00497a18d457949c82bf8bfacc4423fc332074ddf71a49a8fe628fff.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_67f99bef3678c549a14b5f2ff790cce6aba338dca29020755444231b45fa0f980f795e3658496ba70739a099b47b22bc2eab564343ac6132309de3adbbae3455.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_09eecfdd96206ed13830b4b93cfb2cc75cd38083671a34194437b5734b5bb38712209dc335b07e3266ceb3c3a44a155b9bbe5f3e0e1105b19dd45d3def76f020.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_4c089fbdb88e3b624a6f884d3ba1bf606f003bfcd3742376d0d353cd62181dc663aa3811a56361c3100de488fc4d6595a50de2b26f058921ba74f5f2c1b5be00.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_897ff6ac314c5f5e0f496c6af624bd9abf296a02cb5aeb850b9220b6dc3ce2fc4004cb02ed8b59d59d4b9c9d90f050d6eebc1d08ecaebab2f671f7d9367e6410.js
https://codesamplez.com/wp-content/cache/breeze-minification/js/breeze_67d1e619e71d36ae00ddcf85ee18628bb4eb64fcb3d6119b463e75cb987013420a21136d19cd03e6634ccc01cfa9af4a357930e4cf6900953b7812efb4f249fb.js