Files
IT-Docs/WordPress网站迁移记录.md

98 lines
3.5 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网站迁移记录
目标: 将阿里云上的 WordPress 博客(域名 blog.hayashiei.top)完整迁移至腾讯云新服务器(新域名 jp.hayashiei.top)。
## 步骤 1:在阿里云源服务器上进行完整备份
1. 创建备份目录并定位网站文件
mkdir -p ~/blog-backup
cd /var/www
(根据你的实际情况,你的博客文件在 /var/www/wordpress)
2. 打包网站文件
tar -czvf ~/blog-backup/wordpress_files.tar.gz wordpress/
• 备份数据库
先找出 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)
bash
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
此时,你应该能看到博客网站的内容。至此,网站迁移完成。