LAMP stack web server setup

Install LAMP Stack on VPS Ubuntu Correctly

June 2026 · 7 min read

LAMP Stack is a popular open-source software bundle used to host web applications, consisting of Linux, Apache, MySQL, and PHP that work together seamlessly. This article will guide you through installing all components on Ubuntu 22.04 LTS VPS from scratch until your website is live, complete with Virtual Hosts and Let's Encrypt SSL certificates.

Prerequisites

If using a non-root user, prepend sudo to any commands requiring system-level privileges

Step 1 — Update System to Latest

Before installing new software, always update the package list and upgrade existing packages

apt update && apt upgrade -y

Wait for the process to complete. If the system prompts for a reboot, run the command below and reconnect

reboot

Step 2 — Install Apache

Apache is the web server that receives HTTP requests and serves files to visitors

apt install apache2 -y

Enable Apache and configure it to start automatically on boot

systemctl enable apache2
systemctl start apache2

Check the status

systemctl status apache2

Then open the firewall to allow HTTP and HTTPS traffic

ufw allow "Apache Full"
ufw enable
ufw status
Open your browser and enter your VPS IP — if you see the "Apache2 Ubuntu Default Page", Apache is working correctly

Step 3 — Install MySQL

MySQL is the database management system that powers most web applications, including WordPress, Laravel, and others

apt install mysql-server -y

Start and enable MySQL

systemctl enable mysql
systemctl start mysql

Run the security hardening script (highly recommended)

mysql_secure_installation

The script will ask several questions. Answer as follows for maximum security

  1. VALIDATE PASSWORD component — Type Y and select level 2 (STRONG)
  2. Change root password — Type Y and set a complex password
  3. Remove anonymous users — Y
  4. Disallow root login remotely — Y
  5. Remove test database — Y
  6. Reload privilege tables — Y

Create a database and user for your website (replace mydb, myuser, and StrongPass!1 with your actual values)

mysql -u root -p

CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'StrongPass!1';
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 4 — Install PHP

PHP 8.2 is the recommended version for modern web applications, with necessary extensions included

apt install php8.2 php8.2-cli php8.2-fpm php8.2-mysql php8.2-xml \
  php8.2-curl php8.2-mbstring php8.2-zip php8.2-gd php8.2-intl -y

Check PHP version

php -v

Enable PHP-FPM module in Apache and necessary modules

a2enmod proxy_fcgi setenvif
a2enconf php8.2-fpm
systemctl restart apache2

Test that PHP works with Apache by creating a test file

echo "" > /var/www/html/info.php
Do not forget to delete the info.php file immediately after testing. This file exposes critical system information to unauthorized users: rm /var/www/html/info.php

Step 5 — Configure Virtual Host

Virtual Host allows Apache to host multiple domains on the same VPS, with each website's directory and configuration separate

Create a directory for your domain (replace example.com with your actual domain)

mkdir -p /var/www/example.com/public_html
chown -R www-data:www-data /var/www/example.com
chmod -R 755 /var/www/example.com

Create a test index.php file

cat < /var/www/example.com/public_html/index.php
Welcome to example.com";
EOF

Create the Virtual Host configuration file

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

Paste the following content into the file


    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html

    
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    

    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined

Enable Virtual Host and mod_rewrite, then restart Apache

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

Step 6 — Install SSL with Let's Encrypt (Certbot)

SSL makes your website use HTTPS, which is essential for both security and SEO

apt install certbot python3-certbot-apache -y

Request a certificate for your domain (Certbot will modify the Virtual Host automatically)

certbot --apache -d example.com -d www.example.com

Follow the steps shown by Certbot, enter your email, accept the agreement, and choose to redirect HTTP to HTTPS automatically

Verify that Auto-Renewal is working

systemctl status certbot.timer
certbot renew --dry-run
Certbot will automatically renew your SSL certificate 30 days before expiration. You don't need to renew it manually every 90 days

Step 7 — Optimize PHP for Production

Edit important PHP values in php.ini for performance and security

nano /etc/php/8.2/fpm/php.ini

Search and modify these values

upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 120
memory_limit = 256M
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log

Restart PHP-FPM to apply the new settings

systemctl restart php8.2-fpm
systemctl reload apache2

Step 8 — Basic MySQL Optimization

For a 2 GB RAM VPS, modify MySQL values in /etc/mysql/mysql.conf.d/mysqld.cnf

nano /etc/mysql/mysql.conf.d/mysqld.cnf

Add or modify the [mysqld] section

[mysqld]
innodb_buffer_pool_size = 512M
innodb_log_file_size = 128M
max_connections = 100
query_cache_type = 0
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2
systemctl restart mysql

Summary and Checklist

If you've completed all steps, your LAMP Stack on Ubuntu 22.04 VPS should be ready to use. Verify with the checklist below

Recommended Next Steps

LAMP Stack is a solid foundation for self-hosted web hosting. If you need higher performance, you can switch from Apache to Nginx (called LEMP Stack) or use Nginx as a Reverse Proxy alongside Apache later

Verify LAMP Stack is Working Correctly

# Check all services
systemctl status apache2
systemctl status mysql
systemctl status php8.2-fpm

# Test Apache + PHP working together
echo "" > /var/www/html/test.php
curl -s http://localhost/test.php | grep "PHP Version"
rm /var/www/html/test.php

# Check SSL certificate
certbot certificates

# Test Virtual Host
curl -H "Host: example.com" http://localhost/
Don't forget to delete test.php: The phpinfo() file exposes all system information to malicious users. Delete it immediately after testing: rm /var/www/html/test.php

Common Issues & Troubleshooting

Error: "403 Forbidden" when accessing website

Usually caused by missing index file in DocumentRoot or incorrect directory permissions:

# Check if index file exists in folder
ls /var/www/example.com/public_html/

# Check ownership
ls -la /var/www/example.com/
# Should be www-data:www-data

# Fix permissions
chown -R www-data:www-data /var/www/example.com
chmod -R 755 /var/www/example.com

Error: PHP not working, shows raw PHP code instead

Apache is not connected to PHP-FPM or modules are not enabled:

# Check enabled modules
apache2ctl -M | grep -E "php|proxy_fcgi"

# Enable necessary modules
a2enmod proxy_fcgi setenvif
a2enconf php8.2-fpm
systemctl restart apache2

Error: "AH00558: apache2: Could not reliably determine the server's fully qualified domain name"

This is a warning, not a critical error. Fix easily with:

echo "ServerName localhost" >> /etc/apache2/apache2.conf
systemctl restart apache2

Error: Certbot failed "Error: Challenge failed for domain"

Your domain's A Record must point to your VPS IP, and port 80 must be accessible from outside:

# Check DNS
dig +short example.com
# Should display your VPS IP

# Check port 80 from outside
curl -s http://example.com/ | head -5

Error: MySQL "ERROR 1045 (28000): Access denied for user 'root'"

Ubuntu 20.04+ uses unix_socket authentication for root, requiring sudo:

# Correct method
sudo mysql

# Or create a new user for regular use
sudo mysql -e "CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';"
sudo mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost';"

Frequently Asked Questions

Can I host multiple websites on one VPS?

Yes! Apache Virtual Host is designed for this. Create separate .conf files for each domain in /etc/apache2/sites-available/ and enable them with a2ensite. Each domain can have its own DocumentRoot, SSL certificate, and PHP pool

What's the difference between LAMP and LEMP? Which should I choose?

LAMP uses Apache as the web server, while LEMP uses Nginx. Nginx is faster for static files and concurrent connections. Apache is more flexible with .htaccess support. For beginners, LEMP (Nginx) is recommended, but if your application requires .htaccess (WordPress or CodeIgniter), LAMP is better

Let's Encrypt SSL expires every 90 days. Do I need to renew it manually?

Certbot automatically sets up systemd timer or cron for auto-renewal when installed. Verify with systemctl status certbot.timer and test with certbot renew --dry-run. If the timer is active, you don't need to renew manually