首页主机资讯LNMP在Debian上的多站点配置方法

LNMP在Debian上的多站点配置方法

时间2025-11-19 10:57:04发布访客分类主机资讯浏览1503
导读:在 Debian 上搭建 LNMP 多站点的标准做法 一 环境准备与安装 更新索引并安装组件(以 Debian 11/12 为例,PHP 版本以仓库可用为准): sudo apt update sudo apt install ngi...

在 Debian 上搭建 LNMP 多站点的标准做法


一 环境准备与安装

  • 更新索引并安装组件(以 Debian 11/12 为例,PHP 版本以仓库可用为准):
    • sudo apt update
    • sudo apt install nginx mysql-server php-fpm php-mysql
  • 启动并设为开机自启:
    • sudo systemctl enable --now nginx php*-fpm
  • 防火墙放行 HTTP/HTTPS(如使用 UFW):
    • sudo ufw allow ‘Nginx Full’
  • 说明:Debian 的 Nginx 默认启用 sites-available/sites-enabled 机制,便于多站点管理。

二 为每个站点准备目录与数据库

  • 创建站点目录与权限:
    • sudo mkdir -p /var/www/site1 /var/www/site2
    • sudo chown -R www-data:www-data /var/www/site1 /var/www/site2
    • sudo chmod -R 755 /var/www/site1 /var/www/site2
  • 创建数据库与用户(示例为 site1、site2):
    • mysql -u root -p
    • CREATE DATABASE site1; CREATE USER ‘site1user’@‘localhost’ IDENTIFIED BY ‘StrongPass1!’; GRANT ALL PRIVILEGES ON site1.* TO ‘site1user’@‘localhost’; FLUSH PRIVILEGES;
    • CREATE DATABASE site2; CREATE USER ‘site2user’@‘localhost’ IDENTIFIED BY ‘StrongPass2!’; GRANT ALL PRIVILEGES ON site2.* TO ‘site2user’@‘localhost’; FLUSH PRIVILEGES;
    • EXIT;
  • 说明:为每个站点使用独立数据库与用户,便于权限隔离与维护。

三 配置 Nginx 多站点

  • 站点一:/etc/nginx/sites-available/site1
    • server { listen 80; server_name site1.com www.site1.com; root /var/www/site1; index index.php index.html; location / { try_files $uri $uri/ =404; } location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 按实际 PHP 版本调整 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
    • }
  • 站点二:/etc/nginx/sites-available/site2
    • server { listen 80; server_name site2.com www.site2.com; root /var/www/site2; index index.php index.html; location / { try_files $uri $uri/ =404; } location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 按实际 PHP 版本调整 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
    • }
  • 启用站点与检查语法:
    • sudo ln -s /etc/nginx/sites-available/site1 /etc/nginx/sites-enabled/
    • sudo ln -s /etc/nginx/sites-available/site2 /etc/nginx/sites-enabled/
    • sudo nginx -t & & sudo systemctl reload nginx
  • 说明:核心是为每个 server 块设置唯一的 server_name 与 root,PHP 通过 Unix 套接字与 Nginx 通信。

四 域名解析与本地测试

  • 正式环境:在域名 DNS 管理将 site1.comsite2.com(含 www)解析到服务器公网 IP
  • 本地测试(可选):编辑 /etc/hosts
    • 127.0.0.1 site1.com www.site1.com
    • 127.0.0.1 site2.com www.site2.com
  • 访问测试:在浏览器打开 http://site1.com 与 http://site2.com,确认首页与 PHP 正常。

五 常见问题与进阶建议

  • PHP-FPM 版本与套接字路径
    • 确认已安装对应版本:apt install php7.4-fpm(或 8.x)
    • 套接字常见路径:/var/run/php/php7.4-fpm.sock 或 /run/php/php7.4-fpm.sock
    • 若使用 TCP 方式:fastcgi_pass 127.0.0.1:9000; (需确保 php-fpm 监听端口)
  • 权限与安全
    • 目录属主建议 www-data:www-data,权限 755;上传目录可单独设为 775 并限制执行权限。
    • 禁止访问敏感文件:location ~ /.ht { deny all; }
  • 日志与排错
    • 站点日志:/var/log/nginx/site1.access.log、site1.error.log(建议在 server 块中自定义 access_log/error_log 便于定位)
    • 常用排错:sudo nginx -t、sudo systemctl status nginx、sudo systemctl status php7.4-fpm、tail -f /var/log/nginx/error.log
  • 后续进阶
    • 启用 HTTPS/TLS:使用 certbot 为多个域名申请并自动续期证书(Let’s Encrypt)。
    • 性能与安全:配置 OPcache、限制请求方法、开启 security headers、静态资源缓存等。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: LNMP在Debian上的多站点配置方法
本文地址: https://pptw.com/jishu/750964.html
Debian下如何优化LNMP的启动速度 如何在Debian上搭建LNMP测试环境

游客 回复需填写必要信息