Introduction:
A LEMP stack (Linux, Nginx, MySQL/MariaDB, PHP) is a popular server configuration for hosting websites and web applications. LEMP uses Nginx (pronounced “engine-X”) instead of Apache as the web server. In this guide, we will go through the steps to install and configure a LEMP stack on a Debian 10 (Buster) server.
Prerequisites:
- A server running Debian 10
- A user with
sudo
privileges - Basic familiarity with the command line
Step 1: Update Your System
It’s always a good practice to update the system before installing any new packages. Run the following commands to update the package list and upgrade existing packages:
sudo apt update
sudo apt upgrade
Step 2: Install Nginx
Nginx is a fast and lightweight web server that will handle serving web pages in the LEMP stack. To install Nginx, use the following command:
sudo apt install nginx
After the installation, you can check the status of Nginx to make sure it’s running:
sudo systemctl status nginx
You should see an output showing that the service is active and running.
2.1 Adjust the Firewall
If you are using a firewall (such as UFW), you will need to allow HTTP and HTTPS traffic. You can do this with the following commands:
sudo ufw allow 'Nginx Full'
You can verify that the rules were added successfully by typing:
sudo ufw status
To confirm that Nginx is working, visit your server’s IP address in a browser:
http://your-server-ip
You should see the default Nginx welcome page.
Step 3: Install MySQL or MariaDB
The next component of the LEMP stack is MySQL or MariaDB, which will handle the database operations for your website or web application. In this guide, we will use MariaDB, a drop-in replacement for MySQL.
To install MariaDB, run the following command:
sudo apt install mariadb-server mariadb-client
3.1 Secure MariaDB
After installing MariaDB, it’s important to secure it by running the mysql_secure_installation
script. This script helps you set up a root password, remove insecure defaults, and lock down access.
Run the script:
sudo mysql_secure_installation
You will be prompted with several questions:
- Set root password?: Press
Y
and set a secure root password. - Remove anonymous users?: Press
Y
. - Disallow root login remotely?: Press
Y
. - Remove test database and access to it?: Press
Y
. - Reload privilege tables now?: Press
Y
.
This will improve the security of your MariaDB installation.
3.2 Test MariaDB Installation
To verify that MariaDB is installed and running, you can log into the MariaDB shell:
sudo mysql -u root -p
You will be prompted for the root password you set during the secure installation process. Once you’re inside the MariaDB shell, you can exit by typing:
exit
Step 4: Install PHP
PHP is the programming language used to process dynamic content in your web application. In the LEMP stack, PHP runs alongside Nginx to serve PHP scripts.
To install PHP and some common extensions, run the following command:
sudo apt install php-fpm php-mysql
- php-fpm: This is the PHP FastCGI Process Manager, which works with Nginx to serve PHP files.
- php-mysql: This allows PHP to communicate with MySQL or MariaDB databases.
Step 5: Configure Nginx to Use PHP Processor
By default, Nginx does not support PHP. We need to configure Nginx to pass PHP requests to the PHP processor (php-fpm
).
5.1 Edit the Nginx Configuration File
Open the default server block file in Nginx to modify its configuration:
sudo nano /etc/nginx/sites-available/default
Look for the section that begins with location ~ \.php$ {
and modify it as follows:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
Make sure to uncomment (remove #
) or add the above lines if they are missing.
5.2 Test Nginx Configuration
After making these changes, test the Nginx configuration to ensure there are no syntax errors:
sudo nginx -t
If the test is successful, restart Nginx to apply the changes:
sudo systemctl restart nginx
Step 6: Test PHP Processing with Nginx
To verify that Nginx can process PHP correctly, create a PHP test file in the web root directory:
sudo nano /var/www/html/info.php
Add the following PHP code to the file:
<?php
phpinfo();
?>
Save and close the file.
Now, visit the following URL in your browser:
http://your-server-ip/info.php
You should see a page displaying detailed information about your PHP installation. If you see this page, PHP is working correctly with Nginx.
Once you’ve confirmed that PHP is working, delete the info.php
file as it can expose sensitive information about your server:
sudo rm /var/www/html/info.php
Step 7: Create a Database (Optional)
If your website or application requires a MySQL or MariaDB database, you can create one using the MariaDB shell.
First, log into the MariaDB shell as the root user:
sudo mysql -u root -p
Once inside, you can create a new database, user, and grant the necessary privileges. Replace database_name
, username
, and password
with your desired values:
CREATE DATABASE database_name;
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
FLUSH PRIVILEGES;
EXIT;
This will create a new database and user that you can use for your web application.
Step 8: Configure Nginx Server Blocks (Optional)
If you’re hosting multiple websites, you can set up server blocks (similar to virtual hosts in Apache) for each domain.
8.1 Create Directory for the Website
First, create a directory for your website:
sudo mkdir -p /var/www/yourdomain.com/html
sudo chown -R $USER:$USER /var/www/yourdomain.com/html
Create a sample index.html
file to test the server block:
nano /var/www/yourdomain.com/html/index.html
Add some basic HTML content:
<html>
<head>
<title>Welcome to Your Domain!</title>
</head>
<body>
<h1>Success! The LEMP stack is working.</h1>
</body>
</html>
8.2 Create a New Server Block
Next, create a new server block configuration file for your domain:
sudo nano /etc/nginx/sites-available/yourdomain.com
Add the following configuration to the file:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com/html;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Save and close the file.
8.3 Enable the Server Block
To enable the new server block, create a symbolic link in the sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
Then, test the Nginx configuration:
sudo nginx -t
If the test is successful, restart Nginx:
sudo systemctl restart nginx
Your website should now be accessible by visiting http://yourdomain.com
in a web browser.
Conclusion:
By following this guide, you have successfully installed and configured a LEMP stack (Linux, Nginx, MariaDB, PHP) on a Debian 10 server. This stack is a powerful and efficient combination for hosting dynamic websites and web applications. With Nginx as the web server and PHP processing in place, you can now deploy and manage websites on your server. Additionally, setting up server blocks allows you to host multiple websites on the same server.