十分钟把 WordPress 迁移到新 Debian 服务器

开始迁移前

开始迁移前,要备份网站文件及 MySQL 数据库。假设你的网站文件位于 /var/www/wordpress,数据库名是 wordpress。执行下面的命令,归档网站文件:

 # tar czf ~/www.tgz wordpress -C /var/www

现在,你就把网站文件保存到了 /root/www.tgz。你现在可以尝试解压(确保没有输错)然后下载到本地。然后,我们备份 MySQL 数据库:

 # mysqldump wordpress > ~/wordpress.sql

你可以简单浏览一下文件,都是一些 SQL 语句。然后,下载之。

在新服务器上安装相关服务

安装 PHP

 # apt install -y php-{fpm,dom,curl,dom,mysql,gd,imagick,mbstring,intl,zip}

安装后,php-fpm 应该已经默认启动,如果没有运行:

 # systemctl start php7.4-fpm

安装 Mariadb

 # apt install -y mariadb-server mariadb-client 

然后,你应该修改一些安全选项(输入按照下面来就行):

 #  mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

安装 Caddy

Caddy 是一个非常友好的 Web 服务端,安装前需要新增 apt 源:

 # apt install -y debian-keyring debian-archive-keyring apt-transport-https
 # curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
 # curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list

然后,你就可以用 apt 安装 Caddy:

 # apt update && apt install -y caddy

配置你的服务器

配置 MySQL (Mariadb)

首先,使用 mysql 命令,创建新用户和新数据库(留意命令中的变量):

# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5340 to server version: 3.23.54

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> CREATE DATABASE wordpress;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON wordpress.* TO "wordpressusername"@"localhost" IDENTIFIED BY "PLACE_YOUR_PASSWORD_HERE";
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec) 

mysql> EXIT
Bye

然后导入先前备份的数据库:

 # mysql wordpress < /path/to/wordpress.sql

配置 Caddy

先确保对应的域名指向了你的服务器 IP,然后在 /etc/caddy/Caddyfile 中写以下内容:

your.domain.tld {
	root * /var/www/wordpress

	file_server
	php_fastcgi unix//run/php/php-fpm.sock
}

然后重载 Caddy:

 # systemctl restart caddy

配置 WordPress

首先,将你的网站文件备份解包到 /var/www/wordpress

 # tar xzvf /path/to/www.tgz -C /var/www

然后重设目录权限:

 # chown -R www-data:www-data /var/www/wordpress

如果你修改了数据库参数,则修改对应 WordPress 配置文件。

至此,你应该完成了 WordPress 的迁移!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注