Install LAMP Stack on VPS Ubuntu Correctly
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
- Ubuntu 22.04 LTS VPS (minimum 1 GB RAM, 2 GB recommended)
- VPS public IP and a domain with A Record pointing to that IP
- SSH access to VPS as
rootor user withsudoprivileges - Ports 80 and 443 open in Firewall
sudo to any commands requiring system-level privilegesStep 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
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
- VALIDATE PASSWORD component — Type
Yand select level 2 (STRONG) - Change root password — Type
Yand set a complex password - Remove anonymous users —
Y - Disallow root login remotely —
Y - Remove test database —
Y - 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
rm /var/www/html/info.phpStep 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
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
- Apache is running and accessible at
http://IPorhttps://example.com - MySQL is installed and
mysql_secure_installationcompleted - PHP 8.2 with all necessary extensions installed
- Virtual Host configured separately from default site
- Let's Encrypt SSL installed and HTTPS working normally
- Certbot Auto-Renewal is active
info.phpdeleted- Firewall (ufw) open only for necessary ports (22, 80, 443)
Recommended Next Steps
- Install phpMyAdmin to manage databases via web interface (configure additional authentication)
- Install Redis to use as a caching layer to speed up your website
- Configure fail2ban to protect against brute-force SSH and Apache attacks
- Install WordPress or other PHP frameworks on the Virtual Host you created
- Set up automatic backup of databases and web files regularly
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/
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