THU.JUN.18
2026
23:39:38
← back to modules MODULE · 01 · PHP
0 / 10 chapters complete · 0%

Install the LAMP Stack

By the end of this chapter, your laptop will be serving real web pages. Same stack that runs Wikipedia.
LAMP = Linux + Apache + MariaDB + PHP. You've got Linux. The other three are three apt commands.

The restaurant analogy

  • Linux = the building
  • Apache = the waiter (takes orders, brings food)
  • MariaDB = the walk-in fridge (stores ingredients)
  • PHP = the chef (reads order, cooks meal)

1. Install Apache (the web server)

sudo apt install apache2 -y

Apache starts itself automatically. It's already listening on port 80.

From your Windows browser, hit http://192.168.0.19/ (your server's IP).
You should see the Apache default page ("Apache2 Ubuntu Default Page"). If yes — you are officially hosting a website right now. Wild moment.

Four commands to memorize for life:

sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl reload apache2     # graceful — no dropped connections

reload = update the menu while customers eat. restart = kick everyone out, reopen.

2. Install MariaDB (the database)

sudo apt install mariadb-server -y

Now lock it down — fresh installs have unsafe defaults. Don't skip this.

sudo mariadb-secure-installation

It'll ask you a bunch of questions:

  • Current root password? → just hit Enter (blank)
  • Switch to unix_socket auth? → n (keep default)
  • Set a root password? → y, pick a strong one, save it
  • Remove anonymous users? → y
  • Disallow remote root login? → y
  • Drop test database? → y
  • Reload privileges? → y
Log into MariaDB as root: sudo mariadb
You should land at MariaDB [(none)]>. Type EXIT; to leave.

3. Install PHP + the Apache bridge

sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml -y

What each piece does:

  • php — the PHP language itself
  • libapache2-mod-phpcritical: teaches Apache how to run PHP. Without this, Apache serves your source code as plaintext (bad!)
  • php-mysql — lets PHP talk to MariaDB
  • php-curl — make outbound HTTP calls
  • php-gd — image manipulation
  • php-mbstring — handle emoji and Unicode properly
  • php-xml — XML + Composer needs it
sudo systemctl restart apache2
php -v
php -v should print "PHP 8.x.x (cli)". If yes, PHP is installed.

Stack Health Check

Goal: prove Apache + PHP are actually wired together with a one-line test file.

  1. SSH into your server from Windows
  2. Drop a test file: echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
  3. From your Windows browser, hit http://192.168.0.19/info.php
  4. You should see a massive page of PHP info (version, modules, server config — the whole kitchen sink)
  5. IMPORTANT: delete it immediately: sudo rm /var/www/html/info.php

Why delete? phpinfo() is a shopping list for attackers — leaks every detail about your setup. Fine for 60 seconds during debug. Never leave it sitting there.

Last time: sudo rm /var/www/html/info.php. Confirm it's gone before moving on. Treat it like leaving your house keys in the front door.

Bonus: install Composer

Composer is PHP's package manager. You'll want it as soon as you reach for any library.

sudo apt install composer -y
composer --version
LAMP stack confirmed alive. Apache serves, PHP runs, MariaDB stores. You're ready to build.