在 LNMP 环境(Linux + Nginx + MySQL + PHP) 中“添加网站”,通常指 新建一个站点(虚拟主机),包含:
Nginx 站点配置 + 网站目录 +(可选)数据库 +(可选)PHP 支持。
下面按 通用标准做法 一步一步说明,适用于 LNMP 一键包 / 手工编译环境。
假设你已经具备:
example.com)mkdir -p /www/wwwroot/example.com
chown -R www:www /www/wwwroot/example.com
chmod -R 755 /www/wwwroot/example.com
⚠️ 说明:
www 是 Nginx/PHP 的运行用户(视环境可能是 nginx、php-fpm)echo "<?php phpinfo(); ?>" > /www/wwwroot/example.com/index.php
vim /etc/nginx/conf.d/example.com.conf
server {
listen 80;
server_name example.com www.example.com;
root /www/wwwroot/example.com;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\. {
deny all;
}
}
⚠️ 注意:
fastcgi_pass 路径需根据你的 PHP-FPM 实际配置调整:
/run/php/php-fpm.sock127.0.0.1:9000nginx -t
systemctl reload nginx
mysql -uroot -p
CREATE DATABASE example_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'StrongPassword';
GRANT ALL PRIVILEES ON example_db.* TO 'example_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
yum install certbot python3-certbot-nginx -y
# 或
apt install certbot python3-certbot-nginx -y
certbot --nginx -d example.com -d www.example.com
证书会自动续期 ✅
如果你使用的是 LNMP 一键安装包:
lnmp vhost add
按提示输入:
✅ 自动完成 Nginx + PHP + 数据库配置
index 文件不存在fastcgi_pass 路径错误systemctl status php-fpm
SCRIPT_FILENAME 配置错误✅ 创建网站目录
✅ 配置 Nginx 虚拟主机
✅ 检查 PHP-FPM
✅ 配置数据库(可选)
✅ 配置 HTTPS(推荐)
如果你愿意,可以告诉我:
我可以给你 完全贴合你服务器的配置示例 ✅