Introduction:
The LEMP stack, which stands for Linux, Nginx, MySQL, and PHP, is a popular combination of software components for hosting dynamic websites and web applications. In this guide, we will walk you through the step-by-step process of installing a LEMP stack on a Debian 11 server. This powerful stack will enable you to serve web content efficiently and securely.
Prerequisites:
Before we begin, make sure you have the following:
- A Debian 11 server with root or sudo access.
- A terminal or SSH client to connect to your server.
Step 1: Update the System
Start by logging into your server and updating the package list to ensure you are working with the latest software:
sudo apt update
sudo apt upgrade
Step 2: Install Nginx
Nginx is a high-performance web server that will serve as the front-end of our LEMP stack. Install it using the following command:
sudo apt install nginx
Once installed, start the Nginx service and enable it to start on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
You can check the status of Nginx to ensure it’s running:
sudo systemctl status nginx
Step 3: Install MySQL
MySQL is a widely used relational database management system. Install it with the following command:
sudo apt install mysql-server
During the installation, you’ll be prompted to set a root password for MySQL. Make sure to choose a strong password and keep it secure.
Start the MySQL service and enable it to start on boot:
sudo systemctl start mysql
sudo systemctl enable mysql
To secure your MySQL installation, run the following command and follow the on-screen instructions:
sudo mysql_secure_installation
Step 4: Install PHP
PHP is a scripting language that allows you to generate dynamic web content. Install PHP and necessary extensions:
sudo apt install php-fpm php-mysql
Step 5: Configure Nginx to Use PHP
Create a new Nginx server block configuration for your website:
sudo nano /etc/nginx/sites-available/your-site
Replace “your-site” with your desired site name. In this file, add the following configuration:
server {
listen 80;
server_name your-domain.com www.your-domain.com;
root /var/www/your-site;
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 and exit the file.
Next, create a symbolic link to enable the site:
sudo ln -s /etc/nginx/sites-available/your-site /etc/nginx/sites-enabled/
Test the Nginx configuration:
sudo nginx -t
If no errors are reported, reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 6: Create a PHP Test File
To verify that PHP is working correctly, create a test PHP file in the website’s root directory:
sudo nano /var/www/your-site/info.php
Add the following PHP code:
<?php
phpinfo();
?>
Save and exit the file.
Step 7: Test Your LEMP Stack
Open a web browser and enter your server’s IP address or domain name in the address bar, followed by “/info.php” (e.g., http://your-domain.com/info.php). You should see a PHP info page displaying information about your PHP installation. This confirms that Nginx is correctly serving PHP files.
Conclusion:
Congratulations! You have successfully installed a LEMP stack on your Debian 11 server. You now have a powerful web server environment ready to host your websites and web applications. Remember to remove the PHP info file for security reasons once you’ve verified that PHP is working as expected.