Skip to main content

Set Up a LAMP Development Server on Ubuntu

A LAMP stack combines Linux, Apache, MySQL, and PHP for local web development. This guide sets up the stack on an Ubuntu workstation or virtual machine; it is not a FileWave Server prerequisite or a production-server hardening guide.

Keep the first build local. Before exposing a web server to the internet, plan operating-system updates, firewall rules, TLS, database accounts, backups, and application-specific permissions.

Install Apache, MySQL, and PHP

Refresh the Ubuntu package index, then install the web server, database server, PHP, the Apache PHP module, and the MySQL PHP extension:

sudo apt update
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql

Confirm the services are running and validate the Apache configuration:

sudo systemctl status apache2 --no-pager
sudo systemctl status mysql --no-pager
sudo apache2ctl configtest

Ubuntu normally configures the local MySQL administrative account with socket authentication. Open the MySQL shell with sudo mysql; do not switch the root account to password authentication merely to support a web interface.

Optional: phpMyAdmin

phpMyAdmin is not required for a LAMP stack. If you choose to install it, run sudo apt install phpmyadmin, follow the Ubuntu package prompts, and sign in with a dedicated MySQL account rather than the MySQL root account.

Create a local development directory

Apache’s default document root is /var/www/html. Instead of changing ownership of the entire document root, create a project directory owned by your current user:

sudo install -d -o "$USER" -g www-data /var/www/html/lamp-test

Test Apache and PHP

Create a minimal PHP response, restart Apache, and request the page locally:

printf '%s\n' '<?php echo "PHP is working"; ?>' > /var/www/html/lamp-test/index.php
sudo systemctl restart apache2
curl http://localhost/lamp-test/

The response should be PHP is working. If it is not, rerun sudo apache2ctl configtest and inspect sudo journalctl -u apache2 before changing permissions or Apache configuration.

Example Script

This script installs the core stack plus commonly used PHP and image modules. It deliberately leaves MySQL authentication, Apache directory overrides, application files, and database users for the application-specific setup.

#!/usr/bin/env bash
set -euo pipefail

sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y \
  apache2 mysql-server \
  php libapache2-mod-php php-mysql php-cli php-gd php-curl php-zip \
  imagemagick php-imagick

sudo a2enmod rewrite
sudo apache2ctl configtest
sudo systemctl enable --now apache2 mysql
sudo systemctl restart apache2

printf '%s\n' 'LAMP packages installed.'
printf '%s\n' 'Create a project directory and a dedicated MySQL user for your application.'

Enabling mod_rewrite does not enable .htaccess overrides. If an application requires them, add a narrowly scoped AllowOverride rule for that application’s directory instead of changing every Apache directory.

Add TLS with Certbot

The Certbot example below is for a web server with a public DNS name that resolves to this host and can receive validation traffic. It does not issue a certificate for localhost.

#!/bin/bash

# Install Certbot and the Certbot Apache plugin
sudo apt-get install -y certbot python3-certbot-apache

# Prompt for the domain name
echo "Please enter the domain name for the SSL certificate:"
read domain_name

# Run Certbot for the domain
sudo certbot --apache -d "$domain_name"

# Test automatic renewal
sudo certbot renew --dry-run

# Check if ufw is installed
if command -v ufw &> /dev/null
then
    # Allow HTTPS through the firewall
    sudo ufw allow 'Apache Full'
else
    echo "ufw not found. If you have another firewall, please manually open port 443 (HTTPS)."
fi

# Output completion message
echo "TLS setup completed. Visit https://${domain_name} to verify the certificate."