A LEMP stack (Linux, Nginx, MySQL, PHP) is a powerful alternative to LAMP, offering better performance for high-traffic websites. In this guide, we’ll walk you through setting up a LEMP stack on Ubuntu 20.04 step by step.
Prerequisites
Before starting, ensure you have:
- Ubuntu 20.04 server (with sudo access)
- Firewall enabled (ufw)
- Domain or server IP (for testing)
Step 1: Install Nginx (Web Server)
Nginx is a fast, lightweight web server. Install it with:
bash
Allow Nginx Through Firewall
sudo ufw allow 'Nginx HTTP' sudo ufw status # Verify the rule is active
Test Nginx Installation
Visit your server’s IP in a browser:
http://your_server_ip
You should see the Nginx welcome page.
Step 2: Install MySQL (Database)
MySQL stores your website data. Install it with:
sudo apt install mysql-server -y
Secure MySQL Installation
Run the security script:
sudo mysql_secure_installation
Follow prompts to:
- Set a root password (recommended)
- Remove anonymous users
- Disable remote root login
- Remove test databases
Test MySQL Login
sudo mysql -u root -p
Type exit to quit.
Step 3: Install PHP (Processing)
PHP processes dynamic content. Install php-fpm
(FastCGI) and MySQL support:
sudo apt install php-fpm php-mysql -y
Verify PHP version:
php -v
(Should show PHP 7.4+ on Ubuntu 20.04)
Step 4: Configure Nginx to Use PHP
1. Create a Website Directory
sudo mkdir /var/www/your_domain sudo chown -R $USER:$USER /var/www/your_domain
2. Create an Nginx Server Block
sudo nano /etc/nginx/sites-available/your_domain
Paste this config (replace your_domain
with your actual domain/IP):
server { listen 80; server_name your_domain www.your_domain; root /var/www/your_domain; 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/php7.4-fpm.sock; } location ~ /\.ht { deny all; } }
Save (Ctrl+X, Y, Enter).
3. Enable the Site & Test
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/ sudo nginx -t # Check for errors sudo systemctl reload nginx
Step 5: Test PHP Processing
Create a test PHP file:
nano /var/www/your_domain/info.php
Add:
<?php phpinfo(); ?>
Visit in browser:
http://your_domain/info.php
You should see PHP configuration details.
Remove the file after testing (security risk):
sudo rm /var/www/your_domain/info.php
Step 6 (Optional): Test MySQL Connection from PHP
1. Create a Test Database & User
sudo mysql -u root -p
Run in MySQL:
CREATE DATABASE example_db; CREATE USER 'example_user'@'%' IDENTIFIED BY 'strong_password'; GRANT ALL ON example_db.* TO 'example_user'@'%'; FLUSH PRIVILEGES; EXIT;
2. Create a PHP Script to Query MySQL
nano /var/www/your_domain/test_db.php
Paste:
<?php
$user = "example_user";
$pass = "strong_password";
$db = "example_db";
try {
$conn = new PDO("mysql:host=localhost;dbname=$db", $user, $pass);
echo "<h2>Connected to MySQL successfully!</h2>";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
Visit:
http://your_domain/test_db.php
If successful, you’ll see a “Connected to MySQL” message.
Conclusion
You’ve successfully installed a LEMP stack on Ubuntu 20.04!
Nginx – Fast web server
MySQL – Database for dynamic content
PHP-FPM – Efficient PHP processing
Need help? Drop a comment below! A big thank you for exploring TechsBucket! Your visit means a lot to us, and we’re grateful for your time on our platform. If you have any feedback or suggestions, we’d love to hear them.