Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Linux

ตั้งค่า NGINX + PHP-FPM High Performance Web Server (LEMP Stack) บน CentOS 7

LEMP Stack = Linux + NGINX (Static/Proxy) + MariaDB/MySQL + PHP-FPM (FastCGI Process Manager) เร็วกว่าและประหยัดหน่วยความจำกว่า LAMP (Apache + mod_php)

ติดตั้ง NGINX

yum install epel-release -y
yum update -y
rpm -Uvh https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# หรือสร้าง /etc/yum.repos.d/nginx.repo
yum install nginx -y
systemctl start nginx
systemctl enable nginx

ทดสอบ: เปิด IP Server ใน Browser ควรเห็น Welcome Page

ตั้งค่า Virtual Host (Server Block) สำหรับ Domain

สร้าง /etc/nginx/conf.d/woshub-linux.com.conf:

server {
    listen 80;
    server_name woshub-linux.com www.woshub-linux.com;
    return 301 https://woshub-linux.com$request_uri;
}
server {
    listen 443 ssl http2;
    server_name woshub-linux.com;
    root /var/www/woshub-linux.com;
    index index.php index.html index.htm;

    ssl_certificate /etc/letsencrypt/live/woshub-linux.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/woshub-linux.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:...'; # Strong Ciphers
    add_header Strict-Transport-Security 'max-age=604800';

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|woff)$ {
        expires max;
        access_log off;
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_buffers 4 256k;
        fastcgi_buffer_size 128k;
        fastcgi_read_timeout 180;
    }
}

ติดตั้ง PHP-FPM (Remi Repo for PHP 7.4+)

rpm -ivh https://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# Edit /etc/yum.repos.d/remi-php74.repo → enabled=1
yum install php-fpm php-cli php-mysql php-gd php-ldap php-odbc php-pdo php-opcache php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap php-zip -y

# Config /etc/php-fpm.d/www.conf
listen = /var/run/php-fpm/php-fpm.sock
listen.mode = 0660
listen.owner = nginx
listen.group = nginx
user = nginx
group = nginx

systemctl start php-fpm
systemctl enable php-fpm

Tuning สำหรับ High Load (ตัวอย่าง 1 vCPU, 1GB RAM)

NGINX (/etc/nginx/nginx.conf):

worker_processes 2;  # = CPU Cores
events {
    worker_connections 1024;  # 1024-4096
    use epoll;
    multi_accept on;
}
http {
    tcp_nodelay on;
    tcp_nopush on;
    gzip on;
    gzip_types ...;  # เพิ่ม MIME Types ให้ครบ
    keepalive_timeout 30;
    keepalive_requests 100;
    reset_timedout_connection on;
    client_body_timeout 10;
    send_timeout 2;
    client_max_body_size 2m;
}

PHP-FPM (/etc/php-fpm.d/www.conf):

pm = dynamic
pm.max_children = 18
pm.start_servers = 6
pm.min_spare_servers = 4
pm.max_spare_servers = 16
pm.max_requests = 400

คำนวณ: pm.max_children = (Total RAM - DB/OS RAM) / Memory per PHP Process (ประมาณ 30-50 MB/Process)

MariaDB – ใช้คำแนะนำจากบทความก่อนหน้า (my.cnf tuning)

หลังตั้งค่าเสร็จ: systemctl restart nginx php-fpm mariadb ทดสอบด้วย ab (ApacheBench) หรือ wrk