DigitalOceanTutorial2 min read

How Do I Set Up a LAMP Stack on DigitalOcean?

Install Apache, MySQL, and PHP on an Ubuntu Droplet to host dynamic PHP websites like WordPress or Laravel.

Server racks and cloud infrastructure

What Is LAMP?

LAMP stands for Linux, Apache, MySQL, PHP — the classic stack for hosting PHP applications. DigitalOcean Droplets running Ubuntu make an ideal LAMP host. This guide walks through a manual install so you understand each component.

Step 1 — Update the System

sudo apt update && sudo apt upgrade -y

Step 2 — Install Apache

sudo apt install apache2 -y
sudo systemctl enable apache2
curl http://localhost

You should see Apache's default page HTML. Open your Droplet IP in a browser to confirm it works externally.

Step 3 — Install MySQL

sudo apt install mysql-server -y
sudo mysql_secure_installation

The secure installation script prompts you to set a root password, remove anonymous users, and disable remote root login. Answer Y to all security prompts.

Create a database and user for your app:

sudo mysql -u root -p
CREATE DATABASE myapp;
CREATE USER 'myapp'@'localhost' IDENTIFIED BY 'strong_password_here';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 4 — Install PHP

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

Verify PHP works:

echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/info.php

Visit http://YOUR_IP/info.php, confirm PHP details appear, then delete the file for security: sudo rm /var/www/html/info.php

Step 5 — Configure a Virtual Host

sudo nano /etc/apache2/sites-available/myapp.conf
<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /var/www/myapp/public
    <Directory /var/www/myapp/public>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
sudo a2ensite myapp.conf
sudo a2enmod rewrite
sudo systemctl reload apache2

Next Steps

  • Point your domain's A record to the Droplet IP
  • Install SSL with Certbot: sudo certbot --apache -d yourdomain.com
  • Deploy your PHP app to /var/www/myapp

For Nginx instead of Apache, use the LEMP stack (Linux, Nginx, MySQL, PHP-FPM) — same concept, different web server.