0
点赞
收藏
分享

微信扫一扫

nginx-nfs文件分布式

转角一扇门 23小时前 阅读 1

一、NFS 服务端配置

1. 安装 NFS 服务器所需软件包

# 安装 NFS 服务器和依赖组件
yum install -y nfs-utils rpcbind

# 验证安装
rpm -qa | grep nfs
rpm -qa | grep rpcbind

2. 配置 NFS 共享目录

cat > /etc/exports << 'EOF'
# NFS 共享配置
# 格式: 共享目录 客户端IP/网段(权限选项)

/usr/local/nginx 192.168.3.0/24(rw,sync,no_root_squash,no_all_squash)
/usr/local/nginx/html 192.168.3.0/24(rw,sync,no_root_squash,no_all_squash)

# 可选:为特定IP设置不同权限
# /usr/local/nginx/html 192.168.3.11(rw,sync) 192.168.3.12(ro,sync)
EOF

权限选项说明:

  • rw:读写权限
  • ro:只读权限
  • sync:同步写入,数据更安全
  • async:异步写入,性能更好
  • no_root_squash:信任客户端root用户
  • root_squash:将root用户映射为匿名用户
  • no_all_squash:不映射所有用户
  • all_squash:将所有用户映射为匿名用户

3. 启动并启用 NFS 服务

# 设置服务开机自启
systemctl enable rpcbind nfs-server
systemctl enable nfs-lock nfs-idmap

# 启动服务
systemctl start rpcbind nfs-server
systemctl start nfs-lock nfs-idmap

# 重新加载exports配置
exportfs -r
exportfs -v  # 查看当前共享情况

# 检查服务状态
systemctl status rpcbind nfs-server

# 开放NFS相关端口
firewall-cmd --permanent --add-service=nfs
firewall-cmd --permanent --add-service=mountd
firewall-cmd --permanent --add-service=rpc-bind
firewall-cmd --reload

# 或者直接关闭防火墙(测试环境)
systemctl stop firewalld
systemctl disable firewalld

二、NFS 客户端配置

1. 安装 NFS 客户端软件

# 安装NFS客户端工具
yum install -y nfs-utils

# 创建本地挂载点(可选)
mkdir -p /data/nfs/shared

2. 挂载 NFS 共享目录

临时挂载(重启后失效):

# 创建本地目录
mkdir -p /usr/local/nginx
mkdir -p /usr/local/nginx/html

# 挂载NFS共享
mount -t nfs 192.168.3.11:/usr/local/nginx /usr/local/nginx
mount -t nfs 192.168.3.11:/usr/local/nginx/html /usr/local/nginx/html

# 验证挂载
df -h | grep nfs
mount | grep nfs

永久挂载(配置 /etc/fstab):

# 备份原fstab文件
cp /etc/fstab /etc/fstab.bak

# 添加自动挂载配置
echo "192.168.3.11:/usr/local/nginx /usr/local/nginx nfs defaults 0 0" >> /etc/fstab
echo "192.168.3.11:/usr/local/nginx/html /usr/local/nginx/html nfs defaults 0 0" >> /etc/fstab

# 测试挂载
mount -a

# 验证挂载
df -h | grep nfs
ls /usr/local/nginx/html

#!/bin/bash
# nfs-mount.sh

NFS_SERVER="192.168.3.11"
MOUNT_POINTS=(
    "/usr/local/nginx"
    "/usr/local/nginx/html"
)

for point in "${MOUNT_POINTS[@]}"; do
    # 创建挂载点目录
    mkdir -p $point
    
    # 检查是否已挂载
    if ! mountpoint -q $point; then
        echo "挂载 $NFS_SERVER:$point 到 $point"
        mount -t nfs $NFS_SERVER:$point $point
        
        if [ $? -eq 0 ]; then
            echo "挂载成功"
        else
            echo "挂载失败"
        fi
    else
        echo "$point 已经挂载"
    fi
done

# 显示挂载结果
echo "当前NFS挂载情况:"
df -h | grep nfs

三、WordPress Nginx 配置

1. 主站点配置

创建或编辑 /usr/local/nginx/conf/vhost/wordpress.conf

server {
    listen 80;
    server_name wp.wordpress;
    
    # 网站根目录(通过NFS共享)
    root /usr/local/nginx/html/wordpress;
    index index.php index.html index.htm;
    
    # 根请求处理
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
    # 上传目录通过NFS共享
    location /wp-content/uploads/ {
        # 使用alias而不是root
        alias /data/nfs/shared/wp-content/uploads/;

    # PHP处理
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

    }
 }

2. 第二个站点配置

创建或编辑 /usr/local/nginx/conf/vhost/wordpress2.conf

server {
    listen 80;
    server_name wp2.wordpress;
    
    # 共享的网站根目录
    root /usr/local/nginx/html/wordpress;
    index index.php index.html index.htm;
    
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
    # 共享上传目录
    location /wp-content/uploads/ {
        alias /data/nfs/shared/wp-content/uploads/;
    }
    
    # PHP处理
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

3. 重新加载 Nginx 配置

# 测试配置文件语法
/usr/local/nginx/sbin/nginx -t

# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload

# 或者重启Nginx
systemctl restart nginx

-- 检查并更新站点URL
SELECT * FROM wp_options WHERE option_name IN ('siteurl', 'home');

-- 或者让WordPress自动检测
UPDATE wp_options SET option_value = '' WHERE option_name = 'siteurl';
UPDATE wp_options SET option_value = '' WHERE option_name = 'home';

举报

相关推荐

0 条评论