Files
IT-Docs/WordPress网站迁移+Xray代理部署.md

233 lines
9.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# WordPress网站迁移+Xray代理部署
整个流程分为两大阶段:
第一阶段:网站迁移 (从阿里云到腾讯云)
第二阶段:高级部署 (在同一台服务器上,为博客网站和Xray代理配置双域名分流)
## 第一阶段:网站迁移 (阿里云 → 腾讯云)
目标: 将阿里云上的 WordPress 博客(域名 blog.hayashiei.top)完整迁移至腾讯云新服务器(新域名 jp.hayashiei.top)。
### 步骤 1:在阿里云源服务器上进行完整备份
#### (1)创建备份目录并定位网站文件
mkdir -p ~/blog-backup
cd /var/www
(根据你的实际情况,你的博客文件在 /var/www/wordpress)
#### 2)打包网站文件
bash
tar -czvf ~/blog-backup/wordpress_files.tar.gz wordpress/
#### 3)备份数据库
先找出 WordPress 使用的数据库名:
grep DB_NAME /var/www/wordpress/wp-config.php
导出数据库(假设数据库名为 wp_tyo):
mysqldump -u root -p wp_tyo > ~/blog-backup/wp_tyo.sql
#### 4)备份 Nginx 配置文件
cp /etc/nginx/conf.d/blog.conf ~/blog-backup/
#### 5)打包所有备份文件
cd ~
tar -czvf blog_hayashiei_backup.tar.gz blog-backup/
### 步骤 2:将备份包传输到腾讯云目标服务器
在阿里云服务器上执行(替换 你的腾讯云IP):
scp ~/blog_hayashiei_backup.tar.gz root@你的腾讯云IP:~/
### 步骤 3:在腾讯云目标服务器上恢复网站
#### (1)登录腾讯云服务器并解压备份包
ssh root@你的腾讯云IP
tar -xzvf blog_hayashiei_backup.tar.gz
cd blog-backup
#### (2)安装与源服务器一致的环境 (Nginx 1.22, PHP 8.2, MariaDB)
apt update
apt install -y nginx php8.2 php8.2-fpm php8.2-mysql php8.2-mbstring php8.2-xml php8.2-curl php8.2-zip mariadb-server
#### 3)恢复网站文件
mkdir -p /var/www
tar -xzvf wordpress_files.tar.gz -C /var/www/
#### 4)设置正确的文件权限
chown -R www-data:www-data /var/www/wordpress/
find /var/www/wordpress/ -type d -exec chmod 755 {} \;
find /var/www/wordpress/ -type f -exec chmod 644 {} \;
#### 5)恢复数据库
从 wp-config.php 中提取数据库信息:
grep -E "DB_NAME|DB_USER|DB_PASSWORD" /var/www/wordpress/wp-config.php
根据提取的信息,在 MariaDB 中创建相同的数据库和用户(以下为示例):
```
sql
mysql -u root -p
CREATE DATABASE wp_tyo CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wp_tyo'@'localhost' IDENTIFIED BY '你的密码';
GRANT ALL PRIVILEGES ON wp_tyo.* TO 'wp_tyo'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```
导入数据:
mysql -u root -p wp_tyo < wp_tyo.sql
#### 6)配置 Nginx 以使用新域名 jp.hayashiei.top
创建新的 Nginx 配置文件 /etc/nginx/conf.d/blog.conf,内容如下:
```nginx
server {
listen 80;
listen [::]:80;
server_name jp.hayashiei.top;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name jp.hayashiei.top;
root /var/www/wordpress;
index index.php index.html;
ssl_certificate /etc/letsencrypt/live/jp.hayashiei.top/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/jp.hayashiei.top/privkey.pem;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}
}
```
#### 7)申请 SSL 证书
apt install -y certbot python3-certbot-nginx
certbot --nginx -d jp.hayashiei.top
#### 8)修改 WordPress 配置中的域名
编辑 /var/www/wordpress/wp-config.php,将 WP_HOME 和 WP_SITEURL 改为新域名(如果存在):
php
define('WP_HOME', 'https://jp.hayashiei.top');
define('WP_SITEURL', 'https://jp.hayashiei.top');
#### 9)重启服务并测试
systemctl restart nginx php8.2-fpm
curl -k https://jp.hayashiei.top
此时,你应该能看到博客网站的内容。至此,网站迁移完成。
## 第二阶段:高级部署 (配置双域名分流:博客 + Xray)
目标: 在同一台腾讯云服务器上,让 jp.hayashiei.top 继续作为博客网站,同时新增一个域名 jpx.hayashiei.top 作为Xray代理入口,Nginx 在前端根据域名进行分流。
### 步骤 1:修改博客 Nginx 配置 (监听本地端口)
为了让 Nginx 的 stream 模块接管公网的 443 端口,我们需要将博客的配置改为只监听本地。
#### 1)编辑博客配置文件 /etc/nginx/conf.d/blog.conf,将 listen 指令修改为:
```nginx
server {
listen 127.0.0.1:8443 ssl http2;
listen [::1]:8443 ssl http2;
server_name jp.hayashiei.top;
#... 其他配置(包括 SSL 证书路径)保持不变 ...
}
```
(关键:删除了 listen 443 ssl;,改为只监听本地的 8443 端口)
### 步骤 2:配置 Nginx Stream 模块进行四层分流
#### 1)编辑主配置文件 /etc/nginx/nginx.conf
在 http 块之前添加 stream 配置。
(如果你的 nginx.conf 中没有 stream 模块的加载指令,请确保它已启用,例如通过 include /etc/nginx/modules-enabled/*.conf;)
在 events 块之后、http 块之前,加入以下 stream 配置:
```nginx
stream {
#根据 TLS 握手时的 SNI 域名决定后端服务器
map $ssl_preread_server_name $backend {
jp.hayashiei.top 127.0.0.1:8443; #博客网站
jpx.hayashiei.top 127.0.0.1:1443; #Xray 代理
default 127.0.0.1:8443; #默认走博客
}
server {
listen 443 reuseport;
listen [::]:443 reuseport;
ssl_preread on; #必须开启,才能读取 SNI 信息
proxy_pass $backend; #根据 map 的结果进行转发
}
}
```
#### 2)测试配置并重启 Nginx
nginx -t
systemctl restart nginx
### 步骤 3:配置 Xray (监听本地端口,使用 Reality 协议)
#### 1)安装 Xray (如果尚未安装)
bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install
#### 2)生成必要参数
#生成 UUID (用于客户端连接)
xray uuid
#生成 Reality 密钥对
xray x25519
#生成 Short ID (可选,建议使用)
openssl rand -hex 8
#### 3)配置 Xray 编辑 /usr/local/etc/xray/config.json,填入以下内容(替换所有占位符):
```json
{
"log": { "loglevel": "warning" },
"inbounds": [
{
"port": 1443,
"listen": "127.0.0.1",
"protocol": "vless",
"settings": {
"clients": [
{
"id": "你的-UUID-1",
"flow": "xtls-rprx-vision"
},
{
"id": "你的-UUID-2",
"flow": "xtls-rprx-vision"
}
],
"decryption": "none"
},
"streamSettings": {
"network": "tcp",
"security": "reality",
"realitySettings": {
"dest": "microsoft.com:443",
"serverNames": [
"microsoft.com",
"www.microsoft.com"
],
"privateKey": "你的-Private-Key",
"shortIds": [
"你的-Short-ID-1",
"你的-Short-ID-2"
]
}
}
}
],
"outbounds": [ { "protocol": "freedom" } ]
}
```
(关键:监听 127.0.0.1:1443dest 指向一个真实的外部网站如 microsoft.com)
#### 4)启动 Xray
systemctl start xray
systemctl enable xray
### 步骤 4:域名解析与最终验证
#### (1)DNS 解析:在域名管理后台,确保两个域名都解析到你的腾讯云服务器 IP 43.163.236.29。
jp.hayashiei.top → 43.163.236.29
jpx.hayashiei.top → 43.163.236.29
#### 2)验证博客网站
curl -k https://jp.hayashiei.top | head -20
应返回博客的 HTML 内容。
#### 3)验证代理端口转发
curl -k https://jpx.hayashiei.top
应返回 400 Bad Request (这是 Xray 对非代理请求的标准回应),说明请求已成功从 jpx.hayashiei.top:443 经由 Nginx 转发给了 Xray。
#### (4)客户端配置 (以你最终成功的配置为准)
地址:jpx.hayashiei.top
端口:443
协议:vless
UUID:你的-UUID
Flowxtls-rprx-vision
Encryptionnone
Securityreality
PublicKey:你的-Public-Key
Short ID:你的-Short-ID
SNIjp.hayashiei.top #关键:伪装成你的博客域名
Fingerprintchrome
## 后记与排错要点
1Nginx -t 的局限性:nginx -t 只能检查语法错误,无法检查 map 指令中的域名是否写对、后端服务是否存活。
(2)模块加载:如果 stream 配置报错,请确认 Nginx 已安装并加载了 stream 模块 (nginx -V 2>&1 | grep stream)。
(3)端口监听:务必用 ss -tlnp | grep -E ":443|:8443|:1443" 确认端口监听状态正确:443 由 Nginx 监听,8443 由 Nginx 监听,1443 由 Xray 监听。
(4)客户端配置:客户端对分享链接的解析严格程度不同。如果导入链接失败,请手动检查并补全所有参数,特别是 type=tcp 和 headerType=none 这类有默认值的参数。