How to install LAMP stack on Ubuntu on DigitalOcean
Use DigitalOcean's LAMP 1-Click App on Ubuntu 20.04 for fastest setup, or manually install on a standard Ubuntu Droplet. Create Droplet, SSH in, verify with php -i, secure with sudo user and UFW. Test by visiting http://your_droplet_ip for Apache default page.
Prerequisites
- DigitalOcean account with billing enabled
- SSH client (terminal on Linux/Mac or PuTTY on Windows)
- Basic command line knowledge
- Domain name (optional for virtual hosts)
- SSH key pair (recommended for authentication)
Step-by-Step Instructions
Log in to DigitalOcean Control Panel
cloud.digitalocean.com and log in to your account. If you don't have an account, sign up first. This is the starting point for creating any Droplet.[1]Create a New Droplet
Select LAMP 1-Click App (Recommended)
Configure Droplet Settings
Create and Note Droplet IP
SSH into Droplet as Root
ssh root@your_droplet_ip Accept fingerprint, enter password if no SSH key. You're now logged in as root user.[1][2]Verify LAMP Installation
php -i to confirm PHP 8.1 (cli/php.ini at /etc/php/8.1/cli), Apache, MySQL running. Defaults: HTTP port 80, HTTPS 443, SSH 22 open via UFW. For manual install, run sudo apt update && sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql.[1][2][3]Secure Server with Sudo User
adduser sammy (set password), usermod -aG sudo sammy. Log out and SSH as sammy. Follow DigitalOcean's Initial Server Setup: configure sudo, UFW, SSH keys.[1][2]Test Web Server
http://your_droplet_ip in browser. Apache default page loads, confirming HTTP on port 80 works.[1][2]Optional: Enable SSL with Certbot
certbot (pre-installed in 1-Click) for free Let's Encrypt HTTPS certificate on port 443.[1]Common Issues & Troubleshooting
SSH connection refused or timeout
Verify Droplet IP, ensure VPC firewall allows SSH (port 22), check UFW status with <code>sudo ufw status</code>, allow if needed: <code>sudo ufw allow OpenSSH</code>
Apache default page not loading (connection refused)
Check Apache status: <code>sudo systemctl status apache2</code>, restart if inactive: <code>sudo systemctl restart apache2</code>, verify UFW allows HTTP: <code>sudo ufw allow 'Apache'</code>
PHP info shows wrong version or modules missing
For manual install, install PHP: <code>sudo apt install php libapache2-mod-php php-mysql</code>, restart Apache: <code>sudo systemctl restart apache2</code>, verify: <code>php -m</code>
MySQL not starting or access denied
Secure MySQL: <code>sudo mysql_secure_installation</code>, set root password, remove anonymous users
Permission denied editing web files
For virtual host: <code>sudo mkdir -p /var/www/your_domain && sudo chown -R $USER:$USER /var/www/your_domain</code>