LEMP 环境(Nginx+MySQL+PHP)搭建指南 Print

  • 50

LEMP(Linux + Nginx + MySQL + PHP-FPM)是高性能 Web 架构的首选方案。

1. 安装 Nginx

sudo apt update
sudo apt install nginx
sudo systemctl enable nginx
sudo systemctl start nginx

2. 安装 MySQL

sudo apt install mysql-server
sudo mysql_secure_installation

3. 安装 PHP-FPM

sudo apt install php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip

4. Nginx 虚拟主机配置

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

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo systemctl reload nginx

5. 性能优化

# Nginx worker 进程数设为 CPU 核心数
worker_processes auto;

# 启用 gzip 压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml;

Was this answer helpful?

« Back