Install a LAMP Stack on Any VPS in 10 Minutes
Introduction
Setting up a web server from scratch might seem daunting, especially if you’re new to server administration. However, deploying a LAMP stack—which stands for Linux, Apache, MySQL, and PHP—can be surprisingly straightforward when you follow the right steps.
In this guide, I’ll walk you through the process of installing and configuring a complete LAMP stack on any Virtual Private Server (VPS) in just 10 minutes. Whether you’re using DigitalOcean, Linode, AWS, or any other VPS provider, these instructions will work across most Linux distributions with minimal adjustments.
By the end of this tutorial, you’ll have a fully functional web server capable of hosting PHP applications, WordPress sites, or custom web projects.
What is a LAMP Stack?
Before diving into the installation, let’s briefly review what makes up a LAMP stack:
- Linux: The operating system that runs on your server
- Apache: The web server software that delivers web pages to visitors
- MySQL/MariaDB: The database system that stores your website’s data
- PHP: The programming language that processes dynamic content
This powerful combination powers approximately 78% of all websites with a known server-side programming language, making it one of the most popular and well-supported web hosting environments available.
Prerequisites
Before we begin, you’ll need:
- A VPS with at least 1GB RAM (2GB recommended for production environments)
- Root or sudo access to your server
- A basic understanding of the command line
- SSH access to your server
- A domain name pointed to your server’s IP address (optional but recommended)
This guide primarily focuses on Ubuntu/Debian-based systems, but I’ll include notes for CentOS/RHEL users where commands differ.
Let’s get started!
Step 1: Update Your System
Always begin by updating your system packages to ensure you have the latest security patches and software versions.
Connect to your server via SSH:
ssh username@your_server_ip
Update your package lists and upgrade existing packages:
For Ubuntu/Debian:
sudo apt update
sudo apt upgrade -y
For CentOS/RHEL:
sudo yum update -y
This step ensures your server has the latest security patches and package versions before installing new software.
Step 2: Install Apache Web Server
Apache is one of the most popular web servers worldwide, known for its reliability and flexibility.
Install Apache
For Ubuntu/Debian:
sudo apt install apache2 -y
For CentOS/RHEL:
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
Verify the Installation
After installation, Apache should start automatically on Ubuntu/Debian systems. You can verify this by checking its status:
sudo systemctl status apache2 # For Ubuntu/Debian
sudo systemctl status httpd # For CentOS/RHEL
You should see “active (running)” in the output, indicating that Apache is running correctly.
Test Apache
Test that Apache is working by opening your server’s IP address in a web browser:
http://your_server_ip
You should see the default Apache welcome page, which confirms that the web server is running properly.
Configure Firewall (if enabled)
If you have a firewall enabled on your server, you’ll need to allow HTTP (port 80) and HTTPS (port 443) traffic:
For UFW (Ubuntu/Debian):
sudo ufw allow 'Apache Full'
For FirewallD (CentOS/RHEL):
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Step 3: Install MySQL/MariaDB
Next, we’ll install the database system. Both MySQL and MariaDB work well in a LAMP stack. MariaDB is a community-developed fork of MySQL that offers some performance improvements and additional features.
Install MySQL/MariaDB
For Ubuntu/Debian (MySQL):
sudo apt install mysql-server -y
For Ubuntu/Debian (MariaDB):
sudo apt install mariadb-server -y
For CentOS/RHEL (MariaDB):
sudo yum install mariadb-server mariadb -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
Secure Your Database Installation
After installation, it’s crucial to secure your database by setting a root password and removing test databases and anonymous users:
sudo mysql_secure_installation
Follow the prompts to:
- Set a root password (if not already set)
- Remove anonymous users
- Disallow root login remotely
- Remove the test database
- Reload privilege tables
Answer “Y” (yes) to all questions for maximum security.
Verify Database Connection
Check that you can connect to your database server using the root password you set:
sudo mysql -u root -p
After entering your password, you should see the MySQL/MariaDB prompt. Type exit
to return to the shell.
Step 4: Install PHP
PHP is the programming language that processes dynamic content for your web applications.
Install PHP and Common Extensions
For Ubuntu/Debian:
sudo apt install php libapache2-mod-php php-mysql php-common php-cli php-json php-curl php-mbstring php-xml php-zip php-gd -y
For CentOS/RHEL:
sudo yum install php php-mysqlnd php-pdo php-common php-cli php-json php-curl php-mbstring php-xml php-zip php-gd -y
This command installs PHP along with several commonly used extensions that many web applications require.
Verify PHP Installation
Create a test PHP file to verify the installation:
sudo echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/phpinfo.php
Now visit this page in your web browser:
http://your_server_ip/phpinfo.php
You should see the PHP information page showing all the details about your PHP installation and enabled modules.
⚠️ Security Note: Remember to remove this file after testing as it reveals sensitive information about your server configuration:
sudo rm /var/www/html/phpinfo.php
Configure PHP (Optional)
For better performance and security, you might want to adjust some PHP settings. Open the PHP configuration file:
For Ubuntu/Debian (Apache):
sudo nano /etc/php/8.2/apache2/php.ini
Replace 8.2
with your installed PHP version if different.
For CentOS/RHEL:
sudo nano /etc/php.ini
Recommended changes include:
; Increase memory limit for larger applications
memory_limit = 256M
; Increase max upload size
upload_max_filesize = 64M
post_max_size = 64M
; Set recommended timezone (replace with your timezone)
date.timezone = America/New_York
; Disable showing PHP version for security
expose_php = Off
Save the file and restart Apache for the changes to take effect:
sudo systemctl restart apache2 # For Ubuntu/Debian
sudo systemctl restart httpd # For CentOS/RHEL
Step 5: Test Your LAMP Stack
Let’s create a simple test page to verify all components of your LAMP stack are working together.
Create a new PHP file that connects to your database:
sudo nano /var/www/html/lamp_test.php
Add the following content:
<?php
$servername = "localhost";
$username = "root";
$password = "your_mysql_root_password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected to MySQL successfully!<br>";
// Server information
echo "<h2>LAMP Stack Test</h2>";
echo "<p>PHP Version: " . phpversion() . "</p>";
echo "<p>Web Server: " . $_SERVER['SERVER_SOFTWARE'] . "</p>";
echo "<p>Database: " . mysqli_get_server_info($conn) . "</p>";
echo "<p>Operating System: " . php_uname('s') . " " . php_uname('r') . "</p>";
$conn->close();
?>
Replace your_mysql_root_password
with the actual password you set during the MySQL secure installation.
Now visit this page in your browser:
http://your_server_ip/lamp_test.php
If everything is working correctly, you should see information about your LAMP stack components.
⚠️ Security Note: Remember to remove this test file after confirming your setup works:
sudo rm /var/www/html/lamp_test.php
Step 6: Set Up Virtual Hosts (Optional)
If you plan to host multiple websites on your server, setting up Apache virtual hosts is essential. Let’s create a basic virtual host configuration for a domain.
Create Directory Structure
First, create a directory for your site:
sudo mkdir -p /var/www/yourdomain.com/public_html
Set appropriate permissions:
sudo chown -R $USER:www-data /var/www/yourdomain.com
sudo chmod -R 755 /var/www/yourdomain.com
Create a Sample Page
Create a sample index.html file:
nano /var/www/yourdomain.com/public_html/index.html
Add some basic HTML:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to yourdomain.com</title>
</head>
<body>
<h1>Success! Your virtual host is working!</h1>
<p>This page is being served from your new virtual host configuration.</p>
</body>
</html>
Create Virtual Host Configuration
Now, create the virtual host configuration file:
For Ubuntu/Debian:
sudo nano /etc/apache2/sites-available/yourdomain.com.conf
For CentOS/RHEL:
sudo nano /etc/httpd/conf.d/yourdomain.com.conf
Add the following configuration:
<VirtualHost *:80>
ServerAdmin webmaster@yourdomain.com
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/yourdomain.com_error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain.com_access.log combined
<Directory /var/www/yourdomain.com/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
For CentOS/RHEL, replace ${APACHE_LOG_DIR}
with /var/log/httpd
.
Enable the Virtual Host
For Ubuntu/Debian:
sudo a2ensite yourdomain.com.conf
sudo systemctl reload apache2
For CentOS/RHEL:
sudo systemctl restart httpd
If your domain’s DNS is properly configured to point to your server, you should now be able to access your site at http://yourdomain.com
.
Step 7: Secure Your Server
Now that your LAMP stack is up and running, it’s important to implement some basic security measures.
Install and Configure SSL with Let’s Encrypt
Secure your site with free SSL certificates from Let’s Encrypt:
For Ubuntu/Debian:
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
For CentOS/RHEL:
sudo yum install certbot python3-certbot-httpd -y
sudo certbot --httpd -d yourdomain.com -d www.yourdomain.com
Follow the prompts to complete the SSL certificate installation.
Set Up Regular Security Updates
Configure automatic security updates to keep your server protected:
For Ubuntu/Debian:
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure -plow unattended-upgrades
For CentOS/RHEL:
sudo yum install dnf-automatic -y
sudo systemctl enable --now dnf-automatic.timer
Set Up Fail2Ban to Prevent Brute Force Attacks
Install Fail2Ban to protect against brute force login attempts:
For Ubuntu/Debian:
sudo apt install fail2ban -y
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
For CentOS/RHEL:
sudo yum install epel-release -y
sudo yum install fail2ban -y
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
Create a basic configuration file:
sudo nano /etc/fail2ban/jail.local
Add the following content:
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 5
[sshd]
enabled = true
[apache-auth]
enabled = true
[apache-badbots]
enabled = true
Restart Fail2Ban to apply the changes:
sudo systemctl restart fail2ban
Performance Tuning (Optional)
For better performance, especially on production servers, consider implementing these optimizations:
Enable Apache MPM Event and HTTP/2
For Ubuntu/Debian:
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event http2
sudo systemctl restart apache2
Install and Configure PHP-FPM
PHP-FPM typically offers better performance than the default mod_php:
For Ubuntu/Debian:
sudo apt install php-fpm -y
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.2-fpm # Replace with your PHP version
sudo systemctl restart apache2
Install and Configure Redis for Caching
Redis can significantly improve performance for dynamic applications:
sudo apt install redis-server php-redis -y
sudo systemctl enable redis-server
sudo systemctl start redis-server
Troubleshooting Common Issues
Here are solutions to some common issues you might encounter:
Apache Won’t Start
Check for configuration errors:
sudo apachectl configtest
Check error logs:
sudo tail -f /var/log/apache2/error.log # Ubuntu/Debian
sudo tail -f /var/log/httpd/error_log # CentOS/RHEL
PHP Files Download Instead of Executing
Make sure PHP is properly installed and configured with Apache:
sudo a2enmod php8.2 # Replace with your PHP version
sudo systemctl restart apache2
Database Connection Issues
Verify MySQL/MariaDB is running:
sudo systemctl status mysql # Ubuntu/Debian
sudo systemctl status mariadb # CentOS/RHEL
Check username/password and permissions:
sudo mysql -u root -p
mysql> SELECT user, host FROM mysql.user;
Maintenance Best Practices
To keep your LAMP stack running smoothly:
- Regular backups: Set up automated database and file backups
# Quick database backup example mysqldump -u root -p --all-databases > all_databases_backup.sql
- Monitor logs regularly:
sudo tail -f /var/log/apache2/error.log sudo tail -f /var/log/mysql/error.log
- Update packages monthly:
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian sudo yum update -y # CentOS/RHEL
- Monitor server resource usage:
sudo apt install htop -y htop
Conclusion
Congratulations! You’ve successfully set up a complete LAMP stack on your VPS in about 10 minutes. Your server is now ready to host PHP applications, WordPress sites, or custom web projects.
This guide covered the essentials of installing and configuring Apache, MySQL/MariaDB, and PHP, along with basic security measures and performance optimizations. For production environments, you may want to implement additional security hardening and performance tuning based on your specific needs.
Remember that server administration is an ongoing process—regular updates, monitoring, and backups are crucial for maintaining a secure and efficient web server.
Next Steps
Now that your LAMP stack is up and running, you might want to:
- Install a content management system like WordPress, Drupal, or Joomla
- Set up phpMyAdmin for easier database management
- Implement a robust backup solution
- Configure server monitoring tools
- Set up a firewall with more detailed rules
Let me know in the comments if you’d like to see tutorials on any of these topics!
Resources: