LAMP迁移LNMP Nginx多站点配置全流程

发布于:2025-07-18 ⋅ 阅读:(20) ⋅ 点赞:(0)

前言

之前服务器使用的是 LAMP环境,想充分利用服务器资源,再运行另外一个站点

在LAMP环境下应该是也可以实现多站点,

但考虑到nginx轻量性,以及当前主要开发生产用到nginx比较多,配置好后也方便学习

于是决定进行更换

这里面有一个多用户权限的问题,最好统一基于已有的用户进行设置如www用户;目前下面的方案能够实现想要的效果,但可能在规范和安全上做的并不好

备份与停止服务

备份,备份网站,备份配置文件

# 备份网站目录(假设默认路径是 /var/www/html)
sudo cp -r /var/www/html /var/www/html_backup

# 备份 Apache 配置文件
sudo cp -r /etc/apache2 /etc/apache2_backup   # Debian/Ubuntu
sudo cp -r /etc/httpd /etc/httpd_backup       # CentOS/RHEL

停止 apache 服务

# 停止 apache服务
sudo systemctl stop apache2     # Ubuntu/Debian
sudo systemctl stop httpd       # CentOS

# 禁止开机自启
sudo systemctl disable apache2    # Ubuntu/Debian
sudo systemctl disable httpd      # CentOS

# 更老版本centos 会提示 httpd,service is not a native service, redirecting to sbin/chkconfid Executing /sbin/chkconfid httpd off
# 也是执行成功了

# 检查是否禁用
/sbin/chkconfig --list httpd

# httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off 全是off 禁止开机自启动成功

# 如需卸载 可执行
# ubuntu/debian
sudo apt remove --purge apache2
sudo apt autoremove

# centos
sudo yum remove httpd

nginx安装与配置

nginx 编译安装

下载源代码 nginx: download ;然后解压

geoip 安装 ;参考

configure

配置很多 自行搜索添加

./configure --prefix=/www/server/nginx --conf-path=/www/server/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_sub_module --with-pcre --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_realip_module --with-cc-opt="-O2 -pipe -fPIC" --with-ld-opt="-Wl,-Bsymbolic-functions -Wl,--as-needed"

configuring 完成后 会输出 configuration summary

然后 构建安装

make
make install

配置服务

配置服务 nginx.service 放到 /etc/systemd/system 下

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/www/server/nginx/logs/nginx.pid
ExecStartPre=/www/server/nginx/sbin/nginx -t
ExecStart=/www/server/nginx/sbin/nginx
ExecReload=/www/server/nginx/sbin/nginx -s reload
ExecStop=/www/server/nginx/sbin/nginx -s stop
AmbientCapabilities=CAP_NET_BIND_SERVICE
PrivateTmp=true
User=root
Group=root

[Install]
WantedBy=multi-user.target

注意修改路径 根据你的安装路径进行调整

注意这里的 user group 都使用的 root,后面的 nginx 主配置中 设置 user www;

服务以root用户启动,后面设置 user www; 才能生效

注意 AmbientCapabilities=CAP_NET_BIND_SERVICE ;没添加会提示 nginx 绑定端口失败;更多处理方法见 下面参考博客;

启动服务

systemctl daemon-reload

systemctl start nginx

ps -aux | grep nginx

下面展示是基于后面的 nginx 配置【设置了 user www;】启动的效果

一个主进程 root用户,一个工作线程 www 用户

后续过程涉及多次 nginx 配置修改,需要重启验证,下面的命令会多次用到

nginx -t

systemctl status nginx

systemctl reload nginx

systemctl stop nginx

systemctl start nginx

ps -aux | grep nginx

php-fpm多站点配置

phf-fpm介绍

php-fpm 是 PHP FastCGI Process Manager 的缩写,是 PHP 提供的一种高性能的 FastCGI 实现 ,专门用于处理 PHP 请求。

在 Web 开发中,Web 服务器(如 Nginx、Apache)本身无法直接解析和执行 PHP 文件。为了让 Web 服务器能够运行 PHP 脚本,就需要一个中间程序来处理这些请求 —— 这就是 FastCGI 的作用。

FastCGI 是一种协议标准,用于 Web 服务器与后端应用服务器之间的通信。

php-fpm 是实现这个协议的工具,主要功能有

功能 描述
接收 PHP 请求 从 Nginx 或 Apache 接收 PHP 脚本请求
解析并执行 PHP 脚本 把 PHP 代码转换为 HTML 页面或 JSON 数据
管理进程池 控制 PHP 进程数量,提高并发性能
支持多站点配置 每个网站可以使用不同的用户、权限和配置
支持 Unix Socket 和 TCP 更灵活地与 Web 服务器通信
日志记录与错误监控 记录慢脚本、错误日志等信息
[浏览器] → [Nginx] → [PHP-FPM] → [MySQL / Redis / 其他服务]

用户发出访问,nginx接收请求,nginx通过fastcgi_pass 转发给 php-fpm,php-fpm执行php脚本,连接数据库,生成html内容,php-fpm 返回结果给nginx,由nginx返回给浏览器。

多站点配置

php-fpm.conf.default

全局配置 php-fpm.conf

[global]
pid = /www/server/php/74/var/run/php-fpm.pid
error_log = /www/server/php/74/var/log/php-fpm.log
log_level = notice
include = /www/server/php/74/etc/php-fpm.d/*.conf

[www]
listen = /tmp/php-cgi-74.sock
listen.backlog = 8192
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.status_path = /phpfpm_74_status
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
request_terminate_timeout = 100
request_slowlog_timeout = 30
slowlog = var/log/slow.log

在这里加入了一条 include

include = /www/server/php/74/etc/php-fpm.d/*.conf

用于加载各个站点配置

A站点 php-fpm.d/a.conf

[a]
listen = /var/run/php/a.sock
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www

B站点 php-fpm.d/b.conf

[b]
listen = /var/run/php/b.sock
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www

上面给出的是主要配置,其中最重要的是 listen 对应一个站点应用建立的 sock 通信文件

这里的 listen 也支持

; ‘ip.add.re.ss:port’ - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; ‘[ip:6:addr:ess]:port’ - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; ‘port’ - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
; ‘/path/to/unix/socket’ - to listen on a unix socket.

其他完整的配置 可以参考提供的 xxx.conf.default ,然后进行设置;建议直接拷贝后重命名为站点配置 在里面修改就行了

错误排查 主要通过查看日志 php-fpm.log 、slow.log

php-fpm 执行

pkill php-fpm

/www/server/php/74/sbin/php-fpm -y /www/server/php/74/etc/php-fpm.conf -c /www/server/php/74/etc/php.ini 

image-20250717110423055

创建的 sock

image-20250717110623908

主进程 master process

加载全局配置;监听sock文件或端口;管理子进程池 pool;

子进程池 pool

每个 pool 对应一个 PHP 应用(这里也就是 WordPress);每个 pool 可以设置不同用户、监听地址、资源限制等;子进程负责实际执行 PHP 脚本

nginx 多站点配置

nginx 安装路径 /www/server/nginx

conf/nginx.conf


#user  nobody;
user www;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    include /www/server/nginx/conf/site-enabled/*.conf;
    
}

原有的配置基本被删除掉

通过

include /www/server/nginx/conf/site-enabled/*.conf;

将启用的 站点引入起来

site-available\

A站点配置 a.conf

server {
    listen 443 ssl;
    server_name a.top www.a.top;

    root /www/wwwroot/a.top;
    index index.php index.html;

    access_log /var/log/nginx/a.top.access.log;
    error_log /var/log/nginx/a.top.error.log;

    ssl_certificate /www/server/panel/vhost/cert/a.top/fullchain.pem;
    ssl_certificate_key /www/server/panel/vhost/cert/a.top/privkey.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/a.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi.conf;
    }

    location ~ /\.ht {
        deny all;
    }
    
    location ~ ^/wp-config\.php$ {
        deny all;
    }
	# 防止上传目录执行php文件
    location ~ ^/wp-content/uploads/.*\.php$ {
        deny all;
    }
}

server {
    listen 80;
    server_name a.top www.a.top;

    return 301 https://$host$request_uri;
}

B站点配置 b.conf

server {
    listen 80;
    server_name b.top;

    root /www/wwwroot/b.top;
    index index.php index.html;

    access_log /var/log/nginx/b.access.log;
    error_log /var/log/nginx/b.error.log;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/b.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi.conf;
    }

    location ~ /\.ht {
        deny all;
    }

    location ~ ^/(uploads|files)/.*\.php$ {
        deny all;
    }
}

注意配置文件中 fastcgi_pass 使用的 sock 要 和 php-fpm 中站点使用的sock文件 一致

conf/site-enabled

ln /www/server/nginx/site-avaiable/a.conf /www/server/nginx/conf/site-enabled/

ln /www/server/nginx/site-avaiable/b.conf /www/server/nginx/conf/site-enabled/

会在 conf/site-enabled 建立两个软链接 指向 我们 需要管理的网站 nginx 配置,在nginx.conf 主配置那里通过 include 进行了引入

这种分离式配置,可以分别设置不同网站的配置,通过软链接进行启用 方便管理,无需全都放在nginx.conf 主配置中 变得很长难以查询管理

nginx ssl 配置

具体参考 A站点 a.conf

ssl_certificate /www/server/panel/vhost/cert/a.top/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/a.top/privkey.pem;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_certificate 
ssl_certificate_key

设置 ssl 证书和私钥 文件路径

这里要确保 nginx 能够对 证书和私钥有读取权限

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

是 ssl 相关配置 协议版本, 套件,缓存,超时等

然后

listen 443 ssl;

启用 ssl

如果通过浏览器测试 提示 502 或 curl 测试 提示 curl: (35) SSL received a record that exceeded the maximum permissible length

检查是不是listen忘记添加了 ssl;

nginx 安全策略

针对 wordpress 添加的规则

# 禁止 /wp-content/uploads/ *.php
location ~ ^/wp-content/uploads/.*\.php$ {
	deny all;
}

AI提供的更细致的规则 可控参考设置

# 1. 禁止访问 .php 文件
location ~ ^/wp-content/uploads/.*\.php$ {
    deny all;
}

# 2. 禁止访问敏感扩展名
location ~ ^/wp-content/uploads/.*\.(sh|pl|exe|bat|cgi|phps|phar)$ {
    deny all;
}

# 3. 图片等资源正常访问
location ~ ^/wp-content/uploads/.*\.(jpg|jpeg|png|gif|webp|mp4|ogg|ogv|webm)$ {
    expires 30d;
    add_header Cache-Control "public";
}

# 4. 禁止访问 wp-config.php
location ~ ^/wp-config\.php$ {
    deny all;
}

# 5. 禁止访问 .htaccess 等隐藏文件
location ~ /\. {
    deny all;
}

参考

  1. CentOS下为Nginx安装GeoIP扩展

  2. nginx常见问题(四):端口无权限_nginx: emerg bind() to 0.0.0.0:80 failed (13: pe

  3. nginx多站点独立配置 Include引入

  4. www.sock failed (13: Permission denied)

  5. 从零手写实现 nginx-16-nginx.conf 支持配置多个 server


网站公告

今日签到

点亮在社区的每一天
去签到