VPS  November 29, 2026 · 12 min read

Install LAMP Stack on VPS Ubuntu 22.04 — Complete Guide 2026

LAMP Stack is the backbone of countless web applications worldwide. This comprehensive guide walks you through installing Linux, Apache, MySQL, and PHP on a fresh VPS running Ubuntu 22.04 LTS — from the first command to a fully functional web server with Virtual Host and SSL.

Install LAMP Stack on VPS Ubuntu 22.04

What is LAMP Stack?

LAMP Stack is an acronym for four key software components:

LAMP Stack is the standard environment for running WordPress, Laravel, Drupal, Joomla, and countless PHP-based web applications. Being entirely open-source, it comes at zero software licensing cost.

Prerequisites

💡 Tip If your VPS only has a root account, create a sudo user first: adduser username then usermod -aG sudo username. Using root directly is not recommended for security.

Step 1: Update the System

Always start by updating the package list and upgrading installed packages:

sudo apt update && sudo apt upgrade -y

Verify your Ubuntu version:

lsb_release -a

Expected output:

Ubuntu 22.04.x LTS (Jammy Jellyfish)

Step 2: Install Apache

Apache is the web server component that listens for HTTP requests and serves web content:

sudo apt install apache2 -y

Start and enable Apache to run automatically on boot:

sudo systemctl start apache2
sudo systemctl enable apache2

Check the service status:

sudo systemctl status apache2

Open Firewall Ports

If you're using UFW firewall, allow HTTP and HTTPS traffic:

sudo ufw allow 'Apache Full'
sudo ufw enable
sudo ufw status

Visit http://YOUR_VPS_IP in a browser — you should see the "Apache2 Ubuntu Default Page".

📌 Recommended Modules Enable these Apache modules for WordPress and PHP apps: sudo a2enmod rewrite headers expires deflate ssl then restart Apache.

Step 3: Install MySQL

Install MySQL Server:

sudo apt install mysql-server -y

Start and enable MySQL:

sudo systemctl start mysql
sudo systemctl enable mysql

Secure the MySQL Installation

sudo mysql_secure_installation

Recommended answers to the prompts:

Create a Database and User

sudo mysql -u root -p

CREATE DATABASE mywebsite CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON mywebsite.* TO 'webuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 4: Install PHP

Install PHP and essential extensions:

sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip php-bcmath -y

Verify the installation:

php --version

Configure PHP for Production

Edit the PHP configuration file:

sudo nano /etc/php/8.1/apache2/php.ini

Update these settings:

memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
date.timezone = Asia/Bangkok

Restart Apache to apply changes:

sudo systemctl restart apache2

Step 5: Configure Virtual Host

Virtual Hosts allow Apache to serve multiple websites from a single VPS.

Create the Website Directory

sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com
sudo chmod -R 755 /var/www/example.com

Create the Virtual Host Configuration File

sudo nano /etc/apache2/sites-available/example.com.conf

Add the following content:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/html
    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
    <Directory /var/www/example.com/html>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Enable the Virtual Host

sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2

Step 6: Test PHP

Create a PHP info file to verify PHP is working:

sudo nano /var/www/example.com/html/info.php

Contents:

<?php
phpinfo();
?>

Visit http://example.com/info.php — you should see the PHP Information page.

⚠️ Important Delete the info.php file immediately after testing. It exposes sensitive server configuration details: sudo rm /var/www/example.com/html/info.php

Security Hardening

Hide Apache Version

Edit /etc/apache2/conf-available/security.conf:

ServerTokens Prod
ServerSignature Off

Add Security Headers

Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Content-Type-Options "nosniff"
Header always set X-XSS-Protection "1; mode=block"
Header always set Referrer-Policy "strict-origin-when-cross-origin"

Install SSL with Let's Encrypt

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d example.com -d www.example.com

Configure UFW Firewall

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow 'Apache Full'
sudo ufw enable

LAMP vs LEMP Stack Comparison

FeatureLAMP (Apache)LEMP (Nginx)
Web ServerApacheNginx
.htaccess support✅ Native❌ Requires extra config
Static file performanceGoodBetter
Concurrent connectionsGoodBetter
Memory usageSlightly higherLower
WordPress compatibility✅ Excellent✅ Excellent
Best forPHP apps, WordPressHigh-traffic, APIs

✅ LAMP Stack Installation Summary

  1. sudo apt update && sudo apt upgrade -y — Update system
  2. sudo apt install apache2 -y — Install Apache
  3. sudo apt install mysql-server -y — Install MySQL
  4. sudo apt install php libapache2-mod-php php-mysql ... — Install PHP
  5. Configure Virtual Host for your domain
  6. Test with phpinfo(), then delete the file
  7. Harden security with SSL + UFW + Security Headers

Frequently Asked Questions

What is LAMP Stack?
LAMP Stack stands for Linux, Apache, MySQL, and PHP — four open-source software components that together form a powerful, cost-free web server environment used by WordPress, Laravel, and millions of websites worldwide.
What are the minimum server requirements for LAMP?
Minimum 1 GB RAM, though 2 GB is recommended for real traffic. At least 20 GB SSD storage and a 1 vCPU will handle most small to medium websites. Ubuntu 22.04 LTS is the recommended operating system.
What is the difference between LAMP and LEMP Stack?
LEMP replaces Apache with Nginx (the E comes from Nginx). Nginx is more efficient at serving static files and handles more concurrent connections with less memory. Apache is more flexible with .htaccess and is simpler to configure for WordPress.
MySQL or MariaDB — which should I use?
Both are largely interchangeable. MariaDB is a fully open-source MySQL fork that often performs slightly better. Ubuntu 22.04 installs MySQL 8.0 by default via apt. Both are production-ready options.
Which PHP version should I use in 2026?
PHP 8.2 or 8.3 is recommended. They are significantly faster than PHP 7 and fully supported by WordPress 6.x and Laravel 11. Install via the ondrej/php PPA for the latest versions on Ubuntu.