To install a LEMP server on Ubuntu, you need to install and configure four components: Linux (the OS), Nginx (the web server), MySQL or MariaDB (the database), and PHP (the scripting language). This guide walks you through the entire process on a current Ubuntu LTS release, using the default repositories and standard configuration files.
What is a LEMP Stack?
LEMP is a popular software bundle for serving dynamic web applications. The acronym stands for Linux, Enginx (pronounced “engine-x”), MySQL (or MariaDB), and PHP. It is the Nginx-based alternative to the more traditional LAMP stack, which uses Apache as the web server. Nginx is known for its high performance under concurrent loads and its efficient handling of static files, making LEMP a common choice for production servers and WordPress sites.
Prerequisites
Before you begin, make sure you have:
- A server or local machine running a supported version of Ubuntu (e.g., 22.04 or 24.04 LTS).
- A non-root user with
sudoprivileges. - An updated package list: run
sudo apt update. - Access to a terminal and an internet connection.
Step 1: Install Nginx
Nginx is the web server that will handle HTTP requests and serve your site’s files. Install it with:
sudo apt install nginx -y
Once installed, the service should start automatically. Verify it is running with:
systemctl status nginx
If it is not active, start it with sudo systemctl start nginx. To enable it to start on boot, run sudo systemctl enable nginx.
You can test that Nginx is working by opening a web browser and visiting your server’s IP address (e.g., http://your_server_ip). You should see the default Nginx welcome page. If you are on a local machine, you can use http://localhost.
Step 2: Install MySQL or MariaDB
MySQL is a relational database management system. Ubuntu’s default repositories include both MySQL and MariaDB (a drop-in replacement). MariaDB is often preferred for its open-source licensing and performance, but either works. For MySQL, run:
sudo apt install mysql-server -y
For MariaDB, use:
sudo apt install mariadb-server -y
After installation, run the security script to set a root password and remove insecure defaults:
sudo mysql_secure_installation
Follow the prompts. The script will ask you to set a root password (or use auth_socket), remove anonymous users, disallow remote root login, and remove test databases. For a fresh install, answering “Y” to most prompts is safe.
Step 3: Install PHP and Required Extensions
PHP is the scripting language that will process dynamic content. Nginx does not natively process PHP, so you need PHP-FPM (FastCGI Process Manager) to handle PHP requests. Install PHP and common extensions with:
sudo apt install php-fpm php-mysql -y
You may also want additional extensions like php-curl, php-gd, php-mbstring, php-xml, and php-zip for popular applications like WordPress. Install them all at once:
sudo apt install php-curl php-gd php-mbstring php-xml php-zip -y
After installation, check the PHP-FPM service status:
systemctl status php*-fpm
The service name will include the PHP version, e.g., php8.3-fpm. Note the exact name, as you will need it in the Nginx configuration.
Step 4: Configure Nginx to Use PHP
Nginx needs a server block (similar to Apache’s virtual host) to serve your site and pass PHP requests to PHP-FPM. Create a new configuration file for your site:
sudo nano /etc/nginx/sites-available/example.com
Replace example.com with your actual domain or IP. Paste the following configuration, adjusting the server_name and root paths as needed:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
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/php8.3-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Note: The PHP-FPM socket path may vary by version. Check the actual socket file with ls /var/run/php/ and adjust the fastcgi_pass line accordingly.
Create the web root directory and a test PHP file:
sudo mkdir -p /var/www/example.com
sudo nano /var/www/example.com/index.php
Add the following line to the file:
<?php phpinfo(); ?>
Enable the site by creating a symbolic link in sites-enabled:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Test the configuration and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
Now visit http://example.com in your browser. You should see the PHP info page, confirming that Nginx and PHP are working together.
Common Problems and Fixes
Here are a few issues you might encounter:
- 403 Forbidden: Usually a permissions problem. Ensure the web root directory is readable by the Nginx user (
www-data). Runsudo chown -R www-data:www-data /var/www/example.comand set correct permissions. - 502 Bad Gateway: This means Nginx cannot communicate with PHP-FPM. Check that the
fastcgi_passsocket path matches the actual PHP-FPM socket, and that the service is running. - 404 Not Found: The
try_filesdirective may not be finding the file. Verify therootpath and that the file exists. - Port 80 already in use: If Apache or another service is running, stop it with
sudo systemctl stop apache2or change Nginx’s listen port.
Securing Your LEMP Server
After the basic installation, you should take some security steps:
- Configure a firewall: Allow SSH (port 22) and HTTP/HTTPS (ports 80/443) with
sudo ufw allow 'Nginx Full'andsudo ufw enable. - Set up SSL/TLS: Use Let’s Encrypt with Certbot to enable HTTPS. Install with
sudo apt install certbot python3-certbot-nginxand runsudo certbot --nginx. - Remove the phpinfo file: The
index.phpwithphpinfo()should be deleted after testing, as it reveals sensitive server information. - Keep packages updated: Regularly run
sudo apt update && sudo apt upgradeto patch security vulnerabilities.
Frequently Asked Questions
What is the difference between LEMP and LAMP?
LEMP uses Nginx as the web server, while LAMP uses Apache. Nginx is event-driven and handles concurrent connections more efficiently, making it faster for static files and high-traffic sites. Apache is more flexible with per-directory configuration via .htaccess files. Both are viable; the choice depends on your specific needs and familiarity.
Can I use MariaDB instead of MySQL?
Yes, MariaDB is a drop-in replacement for MySQL and is often the default on many Linux distributions. It is fully compatible with MySQL commands and data. The installation steps are nearly identical, and most applications work with either.
How do I change the PHP version?
Ubuntu’s repositories may offer multiple PHP versions. You can install a specific version (e.g., php8.2-fpm) and then update your Nginx configuration to point to the corresponding socket. Use update-alternatives to set the default PHP CLI version if needed.
Why is my PHP code not executing?
If your browser shows the raw PHP code instead of executing it, Nginx is not passing the request to PHP-FPM. Check that the location ~ \.php$ block is present and that the fastcgi_pass path is correct. Also ensure the file has a .php extension and is in the web root.
How do I set up multiple websites on one server?
Create separate server blocks in /etc/nginx/sites-available/ for each domain, each with its own server_name and root. Enable them with symbolic links in sites-enabled. Nginx will route requests based on the domain name in the Host header.
Do I need to install a control panel?
No, a control panel is optional. Many administrators prefer managing the server via command line for full control and lower overhead. Panels like Webmin or Virtualmin can simplify management but add complexity and potential security risks.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- How to Factory Reset iPad If You Forgot Password
- Ubuntu in Windows Store: Which Editions Work?
- Secure Guest Account on Windows 11: Step-by-Step
- Stop Outlook From Changing Hyperlink Font
- Hiberfil.sys: What It Is & How to Delete It
- How to Make a Mask on Snapchat: Step-by-Step