The LEMP Stack (Linux, Nginx, MySQL/MariaDB, and PHP) is a set of free and open-source software applications for hosting and developing PHP web applications. This guide walks you through installing the LEMP Stack on Ubuntu 24.04 “Noble Numbat” and configuring it to secure MariaDB and create Nginx server block settings for hosting websites or domain names.
Prerequisites
Ensure you have Ubuntu 24.04 “Noble Numbat” with root user or non-root user with sudo privileges. Also, make sure the UFW (Uncomplicated Firewall) is enabled.
Installing Nginx Web Server
Nginx is a popular web server known for handling high traffic with minimal resources. We’ll start by installing Nginx on Ubuntu.
Update your package list:
sudo apt update
Install Nginx:
sudo apt install nginx
Verify Nginx status:
sudo systemctl is-enabled nginx sudo systemctl status nginx
Nginx should be ‘enabled‘ and ‘active (running)‘.
Configure UFW to allow HTTP and HTTPS:
sudo ufw allow 'Nginx Full'
Verify UFW rules:
sudo ufw status
Visit http://192.168.5.30/ to check Nginx installation. You should see the default ‘index.html‘ page.
Installing MariaDB Server
Next, install MariaDB, a reliable database management system.
sudo apt install mariadb-server
Verify MariaDB status:
sudo systemctl is-enabled mariadb sudo systemctl status mariadb
MariaDB should be ‘enabled‘ and ‘active (running)‘.
Secure MariaDB by running:
sudo mariadb-secure-installation
Follow the prompts, entering a new root password and securing your installation by removing unnecessary users and databases.
Installing PHP-FPM
PHP-FPM is crucial for processing PHP scripts. Install it along with necessary extensions:
sudo apt install php-fpm php-mysql php-curl php-gd php-json php-intl php-bcmath php-opcache php-apcu php-mbstring php-fileinfo php-xml php-soap php-tokenizer php-zip
Check PHP-FPM service status:
sudo systemctl is-enabled php8.3-fpm sudo systemctl status php8.3-fpm
Locate the PHP-FPM sock file:
ss -pl | grep php
You should find it at ‘/run/php/php8.3-fpm.sock‘
Integrating Nginx with PHP-FPM
To integrate PHP-FPM with Nginx, modify the Nginx server block configuration:
sudo nano /etc/nginx/sites-available/default
Edit the config to point to the correct sock file:
# pass PHP scripts to FastCGI server location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.3-fpm.sock; }
Verify Nginx syntax:
sudo nginx -t
Restart Nginx to apply changes:
sudo systemctl restart nginx
Create a PHP info file to ensure integration:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
Visit http://192.168.5.30/info.php to check for successful integration.
Creating Nginx Server Block (Virtual Host)
A server block, or virtual host, allows you to manage multiple domains. We’ll create a new block for a hypothetical site “newsite.com”.
Create a new directory and index page:
mkdir -p /var/www/newsite/public_html echo "Welcome to newsite.com" > /var/www/newsite/public_html/index.html
Set ownership to ‘www-data’:
sudo chown -R www-data:www-data /var/www/newsite
Create a new server block configuration:
sudo nano /etc/nginx/sites-available/newsite
Insert the configuration below:
server { listen 80; server_name newsite.com; root /var/www/newsite/public_html; index index.html; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.3-fpm.sock; } location / { try_files $uri $uri/ =404; } }
Activate the server block by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/newsite /etc/nginx/sites-enabled/
Test Nginx syntax and restart:
sudo nginx -t sudo systemctl restart nginx
Testing Your Server Block Configuration
To test, add your domain to your computer’s hosts file.
- Linux and MacOS: edit ‘/etc/hosts‘
- Windows: edit ‘C:\Windows\System32\drivers\etc\hosts‘
Include the following:
192.168.5.30 newsite.com
Visit http://newsite.com/ in your browser. You should see your custom ‘index.html’ page:
Conclusion
Congratulations! You’ve successfully installed and configured the LEMP stack on Ubuntu 24.04 “Noble Numbat”, secured your MariaDB server, and created an Nginx server block to host multiple websites.
FAQ
- What is a LEMP stack?
- A software bundle comprising Linux, Nginx, MySQL/MariaDB, and PHP used for hosting web applications.
- Why use PHP-FPM?
- PHP-FPM improves the performance of PHP-based websites by managing requests more efficiently.
- What is an Nginx server block?
- It’s a configuration mechanism within Nginx to host multiple domains on a single server, akin to Apache’s virtual hosts.
- How do I access my newly configured site?
- Add the site’s domain to your hosts file and use the relevant URL in a browser.
- How do I test if my server block is working?
- Access the domain address in a web browser. If configured correctly, it should display your custom page.