Setting Up a LEMP Stack (Linux, Nginx, MySQL, PHP-FPM)

Setting Up a LEMP Stack

LEMP (Linux, Nginx, MySQL/MariaDB, PHP-FPM) offers better performance than LAMP for high-traffic sites because Nginx handles concurrent connections more efficiently than Apache.

Step 1 — Install Nginx

apt update
apt install nginx -y
systemctl enable nginx

Step 2 — Install MariaDB

apt install mariadb-server -y
mysql_secure_installation

Step 3 — Install PHP-FPM

apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip -y

Step 4 — Configure Nginx to Use PHP-FPM

nano /etc/nginx/sites-available/yourdomain.com
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/yourdomain.com;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
}

Enable and test:

ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx

Step 5 — Test PHP

mkdir -p /var/www/yourdomain.com
echo "<?php phpinfo(); ?>" > /var/www/yourdomain.com/info.php

Visit http://yourdomain.com/info.php to verify. Delete the file after testing.

  • 0 Корисниците го најдоа ова како корисно
Дали Ви помогна овој одговор?

Понудени резултати

Managing Users and Groups

Managing Users and Groups on Linux Proper user management is essential for server security. Avoid...

Managing Services with systemctl

Managing Services with systemctl systemctl is the standard tool for managing services on modern...

Monitoring Server Resources

Monitoring Server Resources Keeping an eye on your server's resource usage helps you identify...

Scheduling Tasks with Cron

Scheduling Tasks with Cron Cron allows you to schedule commands to run automatically at set...

Installing and Configuring Nginx

Installing and Configuring Nginx Nginx is a high-performance web server and reverse proxy widely...