Skip to main content

How to install a LEMP stack on Debian 12

To set up a LEMP stack (Linux, Nginx, MySQL, PHP) on Debian 12, you'll need to follow these steps:

  1. Update the System: Log in to your Debian 12 server as a user with sudo privileges and update the system packages to the latest versions:

    sudo apt update
    sudo apt upgrade
    
  2. Install Nginx: Install the Nginx web server using the following command:

    sudo apt install nginx
    

    After installation, start the Nginx service and enable it to start on boot:

    sudo systemctl start nginx
    sudo systemctl enable nginx
    

    You can verify Nginx's status to ensure it's running without errors:

    sudo systemctl status nginx
    
  3. Install MySQL (MariaDB): Install the MySQL database server (Debian 12 uses MariaDB as a drop-in replacement for MySQL):

    sudo apt install mariadb-server
    

    After installation, start the MariaDB service and enable it to start on boot:

    sudo systemctl start mariadb
    sudo systemctl enable mariadb
    

    Secure your MariaDB installation by running the following script:

    sudo mysql_secure_installation
    

    This script will prompt you to set a root password, remove anonymous users, disallow remote root login, and more. Follow the prompts and answer accordingly.

  4. Install PHP: Install PHP and the required PHP modules for common applications:

    sudo apt install php php-fpm php-mysql
    
  5. Configure Nginx to Use PHP-FPM: Next, you'll configure Nginx to use PHP-FPM to process PHP files. Create a new server block configuration for your website:

    sudo nano /etc/nginx/sites-available/example.com
    

    Replace "example.com" with your domain or website name. In this file, add the following configuration (you can modify it according to your needs):

    server {
        listen 80;
        server_name example.com www.example.com;
        root /var/www/html;
    
        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/php-fpm.sock;
        }
    }
    

    Save the file and create a symbolic link to enable the server block:

    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
    

    Test the Nginx configuration and restart the service:

    sudo nginx -t
    sudo systemctl restart nginx
    
  6. Test PHP-FPM: Create a PHP info file to test if PHP-FPM is working correctly:

    echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
    

    Open a web browser and visit http://your_server_ip/info.php. You should see the PHP info page with PHP configuration details.

  7. Firewall (Optional): If you have a firewall enabled, make sure to allow HTTP (port 80) and HTTPS (port 443) traffic to your server to access your website.

That's it! You now have a LEMP stack (Linux, Nginx, MySQL, PHP) set up on your Debian 12 server. You can now deploy your web applications and websites.