The LEMP Stack On Ubuntu (Linux, Nginx, MySQL, PHP) is a powerhouse for hosting dynamic websites and applications. By combining Nginx’s speed, MySQL’s reliability, and PHP’s flexibility, you can deploy scalable projects on Ubuntu 24.04 with ease. In this guide, you’ll learn to set up, secure, and test a LEMP stack from scratch—perfect for developers and sysadmins.
If you want to setup LAMP to your Ubuntu system follow this guide to implement it.
Table of Contents
Prerequisites
Before starting, ensure you have:
- An Ubuntu 24.04 server.
- A domain (e.g., yourdomain.com) pointing to your server’s IP.
- SSH access to the server with a sudo user.
Update your server:
sudo apt update && sudo apt upgrade -y
Installing LEMP Stack
Step 1: Install Nginx
Nginx is the high-performance web server that will handle HTTP requests.
1. Install Nginx:
sudo apt install nginx -y
2.Start and enable Nginx:
sudo systemctl start nginx
sudo systemctl enable nginx
3. Verify the installation:
sudo systemctl status nginx
Look for Active: active (running) in the output.
4. Allow HTTP traffic through the firewall:
sudo ufw allow 'Nginx HTTP'
Step 2: Install MySQL
MySQL will manage your application’s databases.
1. Install MySQL Server:
sudo apt install mysql-server -y
2. Start MySQL and enable auto-start:
sudo systemctl start mysql
sudo systemctl enable mysql
3. Harden MySQL security:
sudo mysql_secure_installation
Follow prompts to set a root password, remove anonymous users, and disable remote root access.
4. Verify MySQL is running:
sudo systemctl status mysql
Step 3: Install PHP and PHP-FPM
PHP processes dynamic content, while PHP-FPM connects it to Nginx.
1. Install PHP and essential modules:
sudo apt install php php-fpm php-mysql php-cli -y
2. Check the PHP version:
php -v
Example Output:
PHP 8.3.6 (cli)
3. Start PHP-FPM (replace 8.3 with your PHP version):
sudo systemctl start php8.3-fpm
sudo systemctl enable php8.3-fpm
4. Confirm PHP-FPM status:
sudo systemctl status php8.3-fpm
Step 4: Configure Nginx to Use PHP
Link Nginx with PHP-FPM to serve dynamic content.
1. Remove the default Nginx config:
sudo rm /etc/nginx/sites-enabled/default
2. Create a new config file for your domain:
sudo nano /etc/nginx/sites-available/yourdomain.com
3. Add this configuration (replace yourdomain.com):
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}
4. Enable the site and test configurations:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Step 5: Secure the Server with SSL
Encrypt traffic using Let’s Encrypt SSL certificates.
1. Install Certbot:
sudo apt install python3-certbot-nginx -y
2. Generate SSL certificates:
sudo certbot --nginx --agree-tos --redirect --email your@email.com -d yourdomain.com
3. Enable auto-renewal:
sudo certbot renew --dry-run
Step 6: Test PHP and MySQL Integration
Verify the stack works by creating a test app.
1. Create a MySQL database and user:
CREATE DATABASE sample_db; sCREATE USER 'app_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL ON sample_db.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
2. Create a PHP test file:
sudo nano /var/www/html/db_test.php
3. Add this code (update credentials):
<?php
$db_host = 'localhost';
$db_user = 'app_user';
$db_pass = 'StrongPassword123!';
$db_name = 'sample_db';
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "LEMP Stack is Working!";
}
?>
Visit http://yourdomain.com/db_test.php in your browser. You’ll see “LEMP Stack is Working!” if successful.
Optimizing PHP-FPM Performance
Adjust PHP-FPM settings for better resource management:
1. Edit the PHP-FPM pool config:
sudo nano /etc/php/8.3/fpm/pool.d/www.conf
2. Tweak these values based on your server’s RAM:
pm = dynamic
pm.max_children = 25
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 10
3. Restart PHP-FPM:
sudo systemctl restart php8.3-fpm
Final Checklist
✅ Nginx serves static and PHP content.
✅ MySQL is secured with a root password.
✅ SSL encryption is active via Certbot.
✅ PHP connects to MySQL without errors.
Conclusion
You’ve successfully installed and secured a LEMP stack on Ubuntu 24.04! This setup forms the backbone for hosting WordPress, Laravel, or custom PHP apps. For advanced configurations, explore Nginx’s official docs or PHP optimization tips.
FAQs
1. What is the difference between LAMP and LEMP?
- LAMP uses Apache as the web server, while LEMP uses Nginx (pronounced “Engine-X”).
- Nginx is known for its high performance and ability to handle more concurrent connections, making it ideal for high-traffic websites.
2. Why is PHP-FPM required in LEMP?
- PHP-FPM (FastCGI Process Manager) is a PHP extension that allows Nginx to process PHP files.
- Unlike Apache, Nginx doesn’t have built-in PHP processing, so PHP-FPM acts as a bridge between Nginx and PHP.
3. How do I check if Nginx is running?
Run the following command:
sudo systemctl status nginx
Look for Active: active (running) in the output. If it’s not running, start it with:
sudo systemctl start nginx
4. How do I secure MySQL after installation?
Run the MySQL secure installation script:
sudo mysql_secure_installation
This will guide you through setting a root password, removing anonymous users, and disabling remote root login.
5. Why isn’t my PHP file executing in the browser?
Ensure PHP-FPM is running:
sudo systemctl status php8.3-fpm
Check your Nginx configuration for the correct PHP-FPM socket path:
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
Restart Nginx after making changes:
sudo systemctl restart nginx