How to Install the LEMP Stack on Ubuntu or Debian: A Step-by-Step Guide
As a tech blogger and IT expert, I often get asked about setting up efficient web servers. The LEMP stack—comprised of Linux, NGINX, MySQL/MariaDB, and PHP—is a powerhouse for hosting dynamic websites. It offers superior performance and scalability compared to traditional stacks like LAMP. In this guide, we’ll walk through installing LEMP on Ubuntu or Debian, ensuring you end up with a fully functional web server ready for deployment.
Why Choose the LEMP Stack?
Before diving into the installation, let’s explore why LEMP is a go-to choice. NGINX excels in handling high traffic with low resource usage, MariaDB provides a robust database alternative to MySQL, and PHP powers dynamic content. This combination is ideal for blogs, e-commerce sites, and applications, making it perfect for developers and sysadmins alike.
Step-by-Step Installation Guide
We’ll start from the basics to ensure a smooth setup. Make sure you have a fresh Ubuntu or Debian installation with sudo privileges. All commands are run in the terminal.
1. Update the Package Repository
The first step is to refresh your system’s package list. This ensures you’re installing the latest versions of software, reducing security risks and compatibility issues.
sudo apt update && sudo apt upgrade
Run this command to update and upgrade your packages. It might take a few minutes depending on your internet speed.
2. Install and Configure NGINX
NGINX is the web server in the LEMP stack, known for its speed and efficiency in serving static content.
sudo apt install nginx -y
Once installed, start and enable NGINX to run on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
Verify the status:
sudo systemctl status nginx
If it shows active (running), proceed.
Additionally, you should check the firewall status on your system. If the firewall is Active, make sure that ports 80 (HTTP) and 443 (HTTPS) are allowed so the web server can accept incoming connections. If these ports are blocked, users will not be able to access your website through a web browser.
sudo ufw status
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload
Test NGINX by visiting your server’s IP in a browser; you should see the default welcome page.
3. Install and Secure MariaDB
MariaDB serves as your database system. Install it with:
sudo apt install mariadb-server mariadb-client -y
Start and enable the service:
sudo systemctl start mariadb
sudo systemctl enable mariadb
Check the status:
sudo systemctl status mariadb
Secure your installation by running the security script:
sudo mysql_secure_installation
Follow the prompts: set a root password, remove anonymous users, and delete the test database. Log in to verify:
sudo mysql -u root -p
Once inside, check databases with SHOW DATABASES; to confirm everything works.
4. Install and Configure PHP
PHP handles dynamic content. Install it along with necessary extensions:
sudo apt install php-fpm php-mysql -y
Check the version:
php -v
Start and enable PHP-FPM:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
Verify status:
sudo systemctl status php-fpm
Now, configure NGINX to work with PHP. Edit the default site configuration:
sudo nano /etc/nginx/sites-available/default
Add or modify the index line to include index.php, and in the location block, add:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; # Adjust version as needed
}
Save, exit, and test the configuration:
sudo nginx -t
If it’s error-free, restart NGINX:
sudo systemctl restart nginx
Create a test file:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php
Visit http://your-server-ip/phpinfo.php in your browser to confirm PHP is working.
Best Practices and Common Troubleshooting
To get the most out of your LEMP stack, follow these best practices: Always keep your server updated with sudo apt update && sudo apt upgrade. Use strong passwords for databases and enable SSL for secure connections. Regularly back up your databases and configurations.
Common issues include firewall blocks—ensure ports 80 and 443 are open. If PHP files aren’t processing, double-check the NGINX configuration for syntax errors. For MariaDB access problems, verify the root password and authentication method. If you encounter dependency errors, try sudo apt install -f to fix broken packages.
Conclusion
By following this guide, you’ve successfully set up a LEMP stack on Ubuntu or Debian, empowering you to host dynamic websites with ease. This setup is scalable and performant, making it a favorite for modern web development. Experiment with your new server, and remember to secure it properly. If you found this helpful, share your experiences in the comments!
Read more
- Install Docker and Portainer on Raspberry Pi
- How to Install LEMP Stack on Ubuntu/Debian
- Understanding TrueNAS RAID Levels: Stripe, Mirror, RAIDZ, and dRAID Explained
- Samba – Part 3 – Set Up Samba with Separate Read/Write Access for Each User
- Samba – Part 2 – Share a Folder with Read and Write Access in LAN