在 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境下,快速配置通常有两种方式:一键脚本(最快) 和 手动配置(更可控)。下面按常见场景给你一个实用速查方案。
yum install -y wget
wget http://soft.vpser.net/lnmp/lnmp2.0.tar.gz
tar -xzf lnmp2.0.tar.gz
cd lnmp2.0
./install.sh lnmp
安装过程中会提示:
✅ 推荐组合(稳定)
安装完成后:
lnmp status
lnmp vhost add
按提示输入:
example.com)/home/wwwroot/example.com)✅ 自动完成:
yum install -y nginx
systemctl enable nginx
systemctl start nginx
apt update
apt install -y nginx
yum install -y mysql-server
systemctl start mysqld
mysql_secure_installation
yum install -y mariadb mariadb-server
systemctl start mariadb
mysql_secure_installation
yum install -y php php-fpm php-mysql php-json php-gd php-mbstring
systemctl start php-fpm
systemctl enable php-fpm
⚠️ 注意 PHP-FPM 监听方式:
127.0.0.1:9000unix:/run/php-fpm/www.sock编辑站点配置:
vim /etc/nginx/conf.d/example.conf
示例配置:
server {
listen 80;
server_name example.com;
root /var/www/example;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重载 Nginx:
nginx -t
systemctl reload nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
yum install -y certbot python3-certbot-nginx
certbot --nginx -d example.com
| 问题 | 原因 |
|---|---|
| 访问 PHP 下载 | Nginx 未解析 PHP |
| 502 Bad Gateway | PHP-FPM 未启动 |
| 403 Forbidden | 目录权限问题 |
| 404 伪静态失效 | try_files 未配置 |
✅ 生产环境
CentOS 7/8 + Nginx + MariaDB + PHP 8.1
✅ WordPress / Laravel
LNMP 一键包 + PHP 8.0+
如果你愿意,我可以根据你的 系统版本(CentOS / Ubuntu) 或 用途(WordPress / Laravel / API) 给你一份 定制版 LNMP 配置方案。