Table of Contents
How to Set Up LEMP Stack on Your VPS: A Complete Step-by-Step Guide
If you are looking to host high-performance web applications on a virtual private server, configuring a LEMP stack VPS is one of the smartest decisions you can make. LEMP stands for Linux, Nginx, MySQL, and PHP — a powerful combination of open-source technologies that work seamlessly together to deliver fast, reliable, and scalable web hosting environments. In this guide, we will walk you through every step of the setup process, from updating your server to testing your configuration.
What Is a LEMP Stack and Why Use It on a VPS?
Before diving into the technical steps, it is worth understanding what a LEMP stack actually is and why so many developers and system administrators prefer it over alternatives like LAMP (which uses Apache instead of Nginx).
The LEMP stack consists of four core components:
- Linux — The operating system that forms the foundation of your server environment
- Nginx — A lightweight, high-performance web server and reverse proxy
- MySQL — A widely used relational database management system (MariaDB is often used as a drop-in replacement)
- PHP — The server-side scripting language that powers countless web applications
Nginx is particularly well-suited for handling large volumes of concurrent connections with minimal resource usage, making a LEMP stack VPS an excellent choice for high-traffic websites, WordPress installations, and custom web applications.
Prerequisites Before You Begin
To follow this guide, you will need the following:
- A VPS running Ubuntu 22.04 LTS (though the steps are similar for Debian-based distributions)
- Root or sudo access to your server
- Basic familiarity with the Linux command line
- An SSH client such as PuTTY or a terminal application
If you are new to managing virtual private servers and want to learn more about server administration best practices, the DA Manager blog offers a wealth of helpful resources to get you started.
Step 1: Update Your Server
The very first thing you should do after logging into your VPS is update all existing packages. This ensures you are working with the latest security patches and software versions.
Run the following commands:
sudo apt update sudo apt upgrade -y
Once the update process is complete, you may be prompted to restart your server. It is good practice to do so before proceeding.
Step 2: Install Nginx
Nginx is the web server component of your LEMP stack VPS. It is available directly from Ubuntu’s default repositories, making installation straightforward.
sudo apt install nginx -y
Once installed, start and enable the Nginx service so that it launches automatically on system reboot:
sudo systemctl start nginx sudo systemctl enable nginx
You can verify that Nginx is running correctly by visiting your server’s IP address in a web browser. You should see the default Nginx welcome page.
Configuring the Firewall for Nginx
If you have UFW (Uncomplicated Firewall) enabled, you will need to allow HTTP and HTTPS traffic:
sudo ufw allow 'Nginx Full'
Step 3: Install MySQL
Next, you will install MySQL to handle your database management needs. Run the following command:
sudo apt install mysql-server -y
Once the installation is complete, it is strongly recommended that you run the built-in security script to remove default settings that could pose a security risk:
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. Answer yes to all prompts to ensure your database is properly secured.
Creating a New Database and User
For best practice, avoid using the root MySQL account for your web applications. Instead, create a dedicated database and user:
sudo mysql CREATE DATABASE mywebsite_db; CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'StrongPassword123!'; GRANT ALL PRIVILEGES ON mywebsite_db.* TO 'webuser'@'localhost'; FLUSH PRIVILEGES; EXIT;
Step 4: Install PHP
PHP is the final major component of your LEMP stack VPS. Unlike Apache, Nginx does not process PHP natively, so you will need to install PHP-FPM (FastCGI Process Manager) to handle PHP processing.
sudo apt install php-fpm php-mysql -y
You may also wish to install additional PHP extensions depending on your application’s requirements:
sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc php-zip -y
Step 5: Configure Nginx to Use PHP-FPM
Now that all components are installed, you need to configure Nginx to pass PHP requests to PHP-FPM. Create a new server block configuration file for your website:
sudo nano /etc/nginx/sites-available/mywebsite
Add the following configuration, replacing your_domain_or_IP with your actual domain name or server IP address:
server {
listen 80;
server_name your_domain_or_IP;
root /var/www/mywebsite;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Save and close the file, then enable the configuration by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
Test the Nginx configuration for syntax errors and reload the service:
sudo nginx -t sudo systemctl reload nginx
Step 6: Create a Web Root Directory and Test PHP
Create the document root directory and a test PHP file to confirm everything is working correctly:
sudo mkdir -p /var/www/mywebsite sudo nano /var/www/mywebsite/info.php
Add the following content to the file:
<?php phpinfo(); ?>
Visit http://your_domain_or_IP/info.php in your browser. If you see the PHP information page, your LEMP stack VPS is working correctly. Be sure to delete this file after testing, as it exposes sensitive server information:
sudo rm /var/www/mywebsite/info.php
Final Tips for Securing and Optimising Your LEMP Stack VPS
Now that your LEMP stack is up and running, there are several additional steps you should consider to keep your server secure and performing at its best:
- Install an SSL certificate — Use Let’s Encrypt and Certbot to enable HTTPS on your domain at no cost
- Enable Nginx caching — Configure FastCGI caching to reduce server load for dynamic content
- Set up regular backups — Automate database and file backups to protect your data
- Monitor server resources — Use tools such as htop or netdata to keep an eye on CPU, memory, and disk usage
- Keep software updated — Regularly apply security patches to all components of your stack
Conclusion
Setting up a LEMP stack VPS may seem daunting at first, but by following these steps methodically, you can have a fully functional and secure web server environment running in under an hour. The combination of Nginx’s speed, MySQL’s reliability, and PHP’s flexibility makes the LEMP stack one of the most popular choices for hosting modern web applications. Whether you are deploying a WordPress site, a Laravel application, or a custom web project, a properly configured LEMP stack VPS will give you the performance and control you need to succeed.














