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.
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.
adduser username then usermod -aG sudo username. Using root directly is not recommended for security.
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)
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
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".
sudo a2enmod rewrite headers expires deflate ssl then restart Apache.
Install MySQL Server:
sudo apt install mysql-server -y
Start and enable MySQL:
sudo systemctl start mysql
sudo systemctl enable mysql
sudo mysql_secure_installation
Recommended answers to the prompts:
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;
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
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
Virtual Hosts allow Apache to serve multiple websites from a single VPS.
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
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>
sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
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.
sudo rm /var/www/example.com/html/info.php
Edit /etc/apache2/conf-available/security.conf:
ServerTokens Prod
ServerSignature Off
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"
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d example.com -d www.example.com
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow 'Apache Full'
sudo ufw enable
| Feature | LAMP (Apache) | LEMP (Nginx) |
|---|---|---|
| Web Server | Apache | Nginx |
| .htaccess support | ✅ Native | ❌ Requires extra config |
| Static file performance | Good | Better |
| Concurrent connections | Good | Better |
| Memory usage | Slightly higher | Lower |
| WordPress compatibility | ✅ Excellent | ✅ Excellent |
| Best for | PHP apps, WordPress | High-traffic, APIs |
sudo apt update && sudo apt upgrade -y — Update systemsudo apt install apache2 -y — Install Apachesudo apt install mysql-server -y — Install MySQLsudo apt install php libapache2-mod-php php-mysql ... — Install PHP