DEV Community

Alan Varghese
Alan Varghese

Posted on

⚙️ How to Set Up a LEMP Server at Home (Step-by-Step Guide)

Want to host your own website or web app from home?

The LEMP stack (Linux, Nginx, MySQL, PHP) is a powerful combination used by developers and web admins to build fast, efficient, and scalable web servers.

In this guide, we’ll walk through setting up a LEMP server at home using Ubuntu. By the end, you’ll have a fully functional web server capable of running dynamic PHP websites — all powered by open-source tools.

What is the LEMP Stack?

Linux - Operating System.
EngineX (Nginx) - The web server that serves your site content.
MySQL - Database Management System.
PHP - Server-side scripting language that powers your site.

Unlike Apache (used in LAMP), Nginx handles more traffic efficiently, making LEMP ideal for modern, high-performance web applications.

Prerequisites

Before you begin, make sure you have:

  • A computer or home server running Ubuntu (or any Linux distro).

  • Terminal access with sudo privileges.

  • A stable internet connection.

  • Optional: Dynamic DNS service (like No-IP) if you want to access your server publicly.

Step 1: Update and Upgrade Your System

Always start with system updates to ensure everything is up-to-date:

sudo apt update && sudo apt upgrade -y

Step 2: Install Nginx

Install the Nginx web server:

sudo apt install nginx -y

Enable and start Nginx:

sudo systemctl enable nginx
sudo systemctl start nginx

Check Nginx status:

sudo systemctl status nginx

Now open your browser and go to:

http://localhost

or your local IP (eg: http://192.168.x.x) — you should see the Nginx Welcome Page.

Step 3: Install MySQL

Install MySQL database server:

sudo apt install mysql-server -y

Secure the MySQL installation:

sudo mysql_secure_installation

Follow the prompts to:

  • Set a root password

  • Remove anonymous users

  • Disallow remote root login

  • Remove test database

Login to MySQL:

sudo mysql -u root -p

Create a test database:

CREATE DATABASE test_db; EXIT;

Step 4: Install PHP

Install PHP and the required modules for Nginx to process PHP files:

sudo apt install php-fpm php-mysql -y

Check the PHP version:

php -v

Step 5: Configure Nginx to Use PHP

Open the default server block file:

sudo nano /etc/nginx/sites-available/default

Find and update it to look like this:

server { listen 80; server_name localhost; root /var/www/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.3-fpm.sock; } location ~ /\.ht { deny all; } }

💡 Note: Replace php8.3-fpm.sock with your actual PHP version if different (php8.1-fpm.sock, etc.).

Save and test the configuration:

sudo nginx -t

If there are no errors, reload Nginx:

sudo systemctl reload nginx

Step 6: Test PHP Processing

Create a PHP test file:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Then open in your browser:

http://localhost/info.php

You should see a page showing your PHP configuration — meaning Nginx and PHP are working perfectly!

Step 7: Set Up a Basic Firewall (Optional)

Enable UFW and allow Nginx traffic:

sudo ufw allow 'Nginx Full'
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status

Step 8: (Optional) Access Your Server Over the Internet

If you want to host your LEMP server publicly:

  1. Register a free hostname on No-IP.

  2. Configure port forwarding (HTTP 80, HTTPS 443) on your router.

  3. Access your site from anywhere using your domain, eg:
    http://myhomeserver.ddns.net

Conclusion

You’ve successfully set up a LEMP server at home!

Now you can:

  • Host your own website or blog.

  • Deploy WordPress manually.

  • Experiment with PHP-based projects locally.

Next Steps

  • Enable SSL with Let’s Encrypt for HTTPS.

  • Use Ansible to automate your LEMP deployment.

  • Secure your MySQL database and Nginx configuration.

Final Thoughts

The LEMP stack is lightweight, reliable, and perfect for learning real-world web hosting.

Building it manually gives you the confidence to handle both local and cloud-based deployments later — like the AWS-based LEMP and WordPress setups we did earlier.

Top comments (0)