2 min read

Setting Up a LAMP Stack on Ubuntu

The LAMP stack (Linux, Apache, MySQL, PHP) is the foundation of the web. Even in 2026, it remains the most popular way to host dynamic websites like WordPress, Joomla, and custom PHP applications.

Since you are running Ubuntu (Linux), you already have the "L". This guide covers installing the rest.

1. Install Apache Web Server

Apache is the software that actually "serves" your web pages to the browser.

  1. Install Apache:
    sudo apt update
    sudo apt install apache2 -y
  2. Verify it works: Open your web browser on Windows and type: http://localhost
    • You should see the "Apache2 Ubuntu Default Page".

WSL Note: If localhost doesn't work, ensure the service is running: sudo service apache2 start. In WSL, services don't always start automatically on boot.

2. Install MySQL Database

MySQL is where your application stores its data (like blog posts, user accounts, and comments).

  1. Install MySQL:

    sudo apt install mysql-server -y
  2. Secure the Installation: Run the included security script to set a root password and remove insecure defaults.

    sudo mysql_secure_installation
    • VALIDATE PASSWORD PLUGIN? Press N (unless you want strict password enforcement for a dev machine).
    • Remove anonymous users? Y
    • Disallow root login remotely? Y
    • Remove test database? Y
    • Reload privilege tables now? Y
  3. Test Access:

    sudo mysql

    Type exit to leave the MySQL prompt.

3. Install PHP

PHP is the scripting language that connects Apache to MySQL and generates dynamic HTML.

  1. Install PHP and dependencies:
    sudo apt install php libapache2-mod-php php-mysql -y
  2. Check Version:
    php -v

4. Connecting the Parts (The "info.php" Test)

To confirm that Apache can pass .php files to the PHP processor, we create a test file.

  1. Create the file: By default, Apache looks for files in /var/www/html.
    sudo nano /var/www/html/info.php
  2. Add the code: Paste the following into the editor:
    <?php
    phpinfo();
    ?>
  3. Save and Exit: Ctrl+O, Enter, Ctrl+X.
  4. Restart Apache:
    sudo service apache2 restart
  5. View the result: Go to http://localhost/info.php in your browser. You should see a purple PHP information page detailing your configuration.

Security Warning: Once you confirm it works, delete this file! It exposes sensitive server information. sudo rm /var/www/html/info.php

5. Managing Services in WSL

Unlike a standard server, WSL doesn't use systemd to keep services running in the background perfectly all the time.

  • Start LAMP: sudo service apache2 start && sudo service mysql start
  • Stop LAMP: sudo service apache2 stop && sudo service mysql stop
  • Check Status: sudo service apache2 status