Table of Contents
How to Set Up LAMP Stack on Your VPS: A Complete Step-by-Step Guide
If you are looking to host dynamic websites or web applications on your own server, setting up a LAMP stack VPS is one of the most reliable and widely used approaches available today. LAMP stands for Linux, Apache, MySQL, and PHP — four open-source technologies that work seamlessly together to create a powerful web hosting environment. Whether you are a developer, a small business owner, or a hobbyist, this guide will walk you through everything you need to know to get your LAMP stack VPS up and running efficiently.
What Is a LAMP Stack and Why Use It on a VPS?
A LAMP stack is a collection of four core software components that together provide a complete environment for hosting and serving web applications. Each letter in the acronym represents one of these components:
- Linux – The operating system that forms the foundation of the server environment.
- Apache – The web server software responsible for handling HTTP requests and serving web pages.
- MySQL – The relational database management system used to store and manage application data.
- PHP – The server-side scripting language used to generate dynamic content.
Using a LAMP stack VPS gives you full control over your hosting environment, allowing you to configure, optimise, and scale your server according to your specific requirements. Unlike shared hosting, a Virtual Private Server (VPS) provides dedicated resources, improved performance, and enhanced security — making it the ideal platform for running a LAMP stack.
Prerequisites Before You Begin
Before diving into the installation process, there are a few things you will need to have in place:
- A VPS running Ubuntu 20.04 or 22.04 LTS (this guide uses Ubuntu, though similar steps apply to Debian and CentOS)
- Root or sudo access to your server
- A basic understanding of the Linux command line
- An SSH client such as PuTTY (Windows) or the built-in terminal (macOS/Linux)
Once you have these in place, you are ready to begin setting up your LAMP stack VPS.
Step 1: Update Your Server
The first step is always to ensure your server’s package list and installed packages are up to date. Log in to your VPS via SSH and run the following commands:
sudo apt update sudo apt upgrade -y
This ensures that you are working with the latest software versions and that any known security vulnerabilities have been patched before you install new software.
Step 2: Install Apache Web Server
Apache is the most widely used web server in the world and serves as the backbone of your LAMP stack VPS. To install Apache, run:
sudo apt install apache2 -y
Once installed, Apache should start automatically. You can verify this by running:
sudo systemctl status apache2
Configure the Firewall for Apache
If you are using UFW (Uncomplicated Firewall), you will need to allow HTTP and HTTPS traffic through the firewall:
sudo ufw allow in "Apache Full"
You can then navigate to your server’s IP address in a web browser to confirm that Apache is running. You should see the default Apache welcome page.
Step 3: Install MySQL Database Server
MySQL is the database component of your LAMP stack VPS. It allows your web applications to store and retrieve data efficiently. Install MySQL with the following command:
sudo apt install mysql-server -y
Secure Your MySQL Installation
After installation, it is highly recommended that you run the MySQL security script to remove insecure default settings:
sudo mysql_secure_installation
This script will prompt you to set a root password, remove anonymous users, disallow remote root login, and remove the test database. Follow the on-screen prompts carefully, choosing options that best suit your security requirements.
Create a Database and User
To create a dedicated database and user for your web application, log in to MySQL:
sudo mysql -u root -p
Then run the following SQL commands, replacing the placeholders with your own values:
CREATE DATABASE mydatabase; CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword'; GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost'; FLUSH PRIVILEGES; EXIT;
Step 4: Install PHP
PHP is the scripting language that ties your LAMP stack VPS together, enabling your web server to process dynamic content and communicate with your database. Install PHP along with the Apache PHP module and the MySQL PHP extension:
sudo apt install php libapache2-mod-php php-mysql -y
Verify the PHP Installation
To confirm that PHP is working correctly with Apache, create a simple PHP info file:
sudo nano /var/www/html/info.php
Add the following content to the file:
<?php phpinfo(); ?>
Save and close the file, then open your browser and navigate to http://your-server-ip/info.php. You should see a detailed PHP information page, confirming that PHP is installed and functioning correctly. Remember to delete this file after testing for security reasons:
sudo rm /var/www/html/info.php
Step 5: Configure Apache Virtual Hosts
If you plan to host multiple websites on your LAMP stack VPS, you will want to configure Apache virtual hosts. This allows Apache to serve different websites from the same server.
Create a Directory for Your Website
sudo mkdir -p /var/www/mywebsite/public_html sudo chown -R $USER:$USER /var/www/mywebsite/public_html
Create a Virtual Host Configuration File
sudo nano /etc/apache2/sites-available/mywebsite.conf
Add the following configuration, replacing mywebsite.com with your actual domain name:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName mywebsite.com
ServerAlias www.mywebsite.com
DocumentRoot /var/www/mywebsite/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable the new virtual host and reload Apache:
sudo a2ensite mywebsite.conf sudo systemctl reload apache2
Step 6: Install an SSL Certificate
Securing your LAMP stack VPS with HTTPS is essential for modern web hosting. You can obtain a free SSL certificate from Let’s Encrypt using Certbot:
sudo apt install certbot python3-certbot-apache -y sudo certbot --apache -d mywebsite.com -d www.mywebsite.com
Follow the on-screen instructions to complete the SSL installation. Certbot will automatically update your Apache configuration to redirect HTTP traffic to HTTPS.
Tips for Optimising Your LAMP Stack VPS
Once your LAMP stack is up and running, there are several steps you can take to improve performance and security:
- Enable Apache caching – Use mod_cache to reduce server load and improve response times.
- Install PHP extensions – Depending on your application, you may need additional PHP extensions such as php-curl, php-gd, or php-mbstring.
- Regularly update your software – Keep Linux, Apache, MySQL, and PHP updated to protect against security vulnerabilities.
- Monitor server resources – Use tools like htop or Netdata to keep an eye on CPU, memory, and disk usage.
- Set up automated backups – Ensure your data is safe by scheduling regular backups of your files and databases.
For more expert guides on managing your VPS and web hosting environment, visit the DA-Manager Blog, where you will find a wealth of tutorials and tips to help you get the most out of your server.
Conclusion
Setting up a LAMP stack VPS may seem daunting at first, but by following the steps outlined in this guide, you can have a fully functional web hosting environment up and running in a relatively short amount of time. From installing Apache and MySQL to configuring PHP and securing your server with SSL, each step builds upon the last to create a robust, scalable, and secure platform for your websites and web applications. With your LAMP stack VPS now in place, you are well-equipped to host everything from simple WordPress blogs to complex custom-built web applications.














