使用这个简单的方法来迁移一个网站以及管理防火墙配置。

你有过把一个 wordpress 网站迁移到一台新主机上的需求吗?我曾经迁移过好多次,迁移过程相当简单。当然,的的市场时候我都不会用通用的推荐方法,这次也不例外 —— 我用更简单的方法,这才是我推荐的方法。

这个迁移方法没有破坏性,因此如果出于某些原因你需要还原到原来的服务器上,很容易可以实现。

一个 wordpress 网站的组成部分

运行一个基于 wordpress 的网站有三个重要组成部分:wordpress 本身,一个 web 服务器,如 apache(我正在用),以及 mariadb。mariadb 是 mysql 的一个分支,功能相似。

业界有大量的 web 服务器,由于我使用了 apache 很长时间,因此我推荐用 apache。你可能需要把 apache 的配置方法改成你用的 web 服务器的方法。

初始配置

我使用一台 linux 主机作为防火墙和网络路由。在我的网络中 web 服务器是另一台主机。我的内部网络使用的是 c 类私有网络地址范围,按 无类别域间路由classless internet domain routing(cidr)方式简单地记作 192.168.0.0/24。

对于防火墙,相比于更复杂的 firewalld,我更喜欢用非常简单的 iptables。这份防火墙配置中的一行会把 80 端口(http)接收到的包发送给 web 服务器。在 /etc/sysconfig/iptables 文件中,你可以在注释中看到,我添加了规则,把其他入站服务器连接转发到同一台服务器上合适的端口。

# reroute ports for inbound connections to the appropriate web/email/etc server.
# httpd goes to 192.168.0.75
-a prerouting -d 45.20.209.41/255.255.255.248 -p tcp -m tcp --dport 80 \
  -j dnat --to-destination 192.168.0.75:80

我使用命名虚拟主机named virtual host来配置原来的 apache web 服务器,因为我在这个 httpd 实例上运行着多个网站。使用命名虚拟主机配置是个不错的方法,因为(像我一样)未来你可能会在运行其他的网站,这个方法可以使其变得容易。

/etc/httpd/conf/httpd.conf 中需要迁移的虚拟主机的网站相关部分请参考下面代码。这个片段中不涉及到 ip 地址的修改,因此在新服务器上使用时不需要修改。

<virtualhost *:80>
   servername www.website1.org
   serveralias server.org
documentroot "/var/website1/html"
   errorlog "logs/error_log"
   serveradmin me@website1.org
<directory "/var/website1/html">
      options indexes followsymlinks
allowoverride none
      require all granted
</directory>
</virtualhost>

在迁移之前,你需要在 httpd.conf 的最顶端附近找到 listen 声明并修改成类似下面这样。这个地址是服务器的真实私有 ip 地址,不是公开 ip 地址。

listen 192.168.0.75:80

你需要修改新主机上 listen 的 ip 地址。

前期工作

准备工作分为以下三步:

  • 安装服务
  • 配置防火墙
  • 配置 web 服务器

安装 apache 和 mariadb

如果你的新服务器上还没有 apache 和 mariadb,那么就安装它们。wordpress 的安装不是必要的。

dnf -y install httpd mariadb

新服务器防火墙配置

确认下新服务器上的防火墙允许访问 80 端口。你_每台_电脑上都有一个防火墙,对吗?大部分现代发行版使用的初始化配置包含的防火墙会阻止所有进来的网络流量,以此来提高安全等级。

下面片段的第一行内容可能已经在你的 iptables 或其他基于防火墙的网络过滤器中存在了。它标识已经被识别为来自可接受来源的入站包,并绕过后面的其它 input 过滤规则,这样可以节省时间和 cpu 周期。片段中最后一行标识并放行 80 端口新进来的请求到 httpd 的连接。

-a input -m state --state related,established -j accept
<删节>
# http
-a input -p tcp -m state --state new -m tcp --dport 80 -j accept

下面的示例 /etc/sysconfig/iptables 文件是 iptables 最少规则的例子,可以允许 ssh(端口 22)和 httpd(端口 80)连接。

*filter
:input accept [0:0]
:forward accept [0:0]
:output accept [0:0]
-a input -m state --state related,established -j accept
-a input -p icmp -j accept
-a input -i lo -j accept
# sshd
-a input -p tcp -m state --state new -m tcp --dport 22 -j accept
# http
-a input -p tcp -m state --state new -m tcp --dport 80 -j accept
# final disposition for unmatched packets
-a input -j reject --reject-with icmp-host-prohibited
-a forward -j reject --reject-with icmp-host-prohibited
commit

在新服务器主机上我需要做的就是在 /etc/sysconfig/iptables 文件的防火墙规则里添加上面片段的最后一行,然后重新加载修改后的规则集。

iptables-restore /etc/sysconfig/iptables

大部分基于红帽的发行版本,如 fedora,使用的是 firewalld。我发现对于它的适用场景(如家用、小到中型企业)而言,它过于复杂,因此我不用它。我建议你参照 firewalld 网页 来向 firewalld 添加入站端口 80。

你的防火墙及其配置可能跟这个有些差异,但最终的目的是允许新 web 服务器 80 端口接收 httpd 连接。

httpd 配置

在 /etc/httpd/conf/httpd.conf 文件中配置 httpd。像下面一样在 listen 片段中设置 ip 地址。我的新 web 服务器 ip 地址是 192.168.0.125

listen 192.168.0.125:80

复制(对应要迁移的网站的) virtualhost 片段,粘贴到新服务器上 httpd.conf 文件的末尾。

迁移过程

只有两组数据需要迁移到新服务器 —— 数据库本身和网站目录结构。把两个目录打包成 tar 文档。

cd /var ; tar -cvf /tmp/website.tar website1/
cd /var/lib ; tar -cvf /tmp/database.tar mysql/

把两个 tar 文件复制到新服务器。我通常会把这类文件放到 /tmp 下,这个目录就是用来做这种事的。在新服务器上运行下面的命令,把 tar 文档解压到正确的目录。

cd /var ; tar -xvf /tmp/website.tar
cd /var/lib ; tar -xvf /tmp/database.tar

wordpress 的所有文件都在 /var/website1 下,因此不需要在新服务器上安装它。新服务器上不需要执行 wordpress 安装过程。

这个目录就是需要迁移到新服务器上的全部内容。

最后一步是启动(或重启)mysqld 和 httpd 服务守护进程。wrodpress 不是一个服务,因此不使用守护进程的方式来启动。

systemctl start mysqld ; systemctl start httpd

启动之后,你应该检查下这些服务的状态。

systemctl status mysqld
● mariadb.service - mariadb 10.5 database server
    loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
    active: active (running) since sat 2021-08-21 14:03:44 edt; 4 days ago
        docs: man:mariadbd(8)
https://mariadb.com/kb/en/library/systemd/
   process: 251783 execstartpre=/usr/libexec/mariadb-check-socket (code=exited, status=0/success)
   process: 251805 execstartpre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/success)
   process: 251856 execstartpost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/success)
 main pid: 251841 (mariadbd)
      status: "taking your sql requests now..."
      tasks: 15 (limit: 19003)
    memory: 131.8m
        cpu: 1min 31.793s
    cgroup: /system.slice/mariadb.service
└─251841 /usr/libexec/mariadbd --basedir=/usr
aug 21 14:03:43 simba.stmarks-ral.org systemd[1]: starting mariadb 10.5 database server...
aug 21 14:03:43 simba.stmarks-ral.org mariadb-prepare-db-dir[251805]: database mariadb is probably initialized in /var/lib/mysql already, n>
aug 21 14:03:43 simba.stmarks-ral.org mariadb-prepare-db-dir[251805]: if this is not the case, make sure the /var/lib/mysql is empty before>
aug 21 14:03:44 simba.stmarks-ral.org mariadbd[251841]: 2021-08-21 14:03:44 0 [note] /usr/libexec/mariadbd (mysqld 10.5.11-mariadb) startin>
aug 21 14:03:44 simba.stmarks-ral.org systemd[1]: started mariadb 10.5 database server.
systemctl status httpd
● httpd.service - the apache http server
   loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   drop-in: /usr/lib/systemd/system/httpd.service.d
└─php-fpm.conf
      active: active (running) since sat 2021-08-21 14:08:39 edt; 4 days ago
        docs: man:httpd.service(8)
   main pid: 252458 (httpd)
      status: "total requests: 10340; idle/busy workers 100/0;requests/sec: 0.0294; bytes served/sec: 616 b/sec"
        tasks: 278 (limit: 19003)
      memory: 44.7m
        cpu: 2min 31.603s
   cgroup: /system.slice/httpd.service
├─252458 /usr/sbin/httpd -dforeground
├─252459 /usr/sbin/httpd -dforeground
├─252460 /usr/sbin/httpd -dforeground
├─252461 /usr/sbin/httpd -dforeground
├─252462 /usr/sbin/httpd -dforeground
└─252676 /usr/sbin/httpd -dforeground
aug 21 14:08:39 simba.stmarks-ral.org systemd[1]: starting the apache http server...
aug 21 14:08:39 simba.stmarks-ral.org httpd[252458]: ah00112: warning: documentroot [/var/teststmarks-ral/html] does not exist
aug 21 14:08:39 simba.stmarks-ral.org httpd[252458]: server configured, listening on: port 80
aug 21 14:08:39 simba.stmarks-ral.org systemd[1]: started the apache http server.

最终的修改

现在所需的服务都已经运行了,你可以把 /etc/sysconfig/iptables 文件中 httdp 的防火墙规则改成下面的样子:

-a prerouting -d 45.20.209.41/255.255.255.248 -p tcp -m tcp --dport 80 \
  -j dnat --to-destination 192.168.0.125:80

然后重新加载设置的 iptables 规则。

iptables-restore /etc/sysconfig/iptables

由于防火墙规则是在防火墙主机上,因此不需要把外部 dns 入口改成指向新服务器。如果你使用的是内部 dns 服务器,那么你需要把 ip 地址改成内部 dns 数据库里的 a 记录。如果你没有用内部 dns 服务器,那么请确保主机 /etc/hosts 文件里新服务器地址设置得没有问题。

测试和清理

请确保对新配置进行测试。首先,停止旧服务器上的 mysqld 和 httpd 服务。然后通过浏览器访问网站。如果一切符合预期,那么你可以关掉旧服务器上的 mysqld 和 httpd。如果有失败,你可以把 iptables 的路由规则改回去到旧服务器上,直到问题解决。

之后我把 mysql 和 httpd 从旧服务器上删除了,这样来确保它们不会意外地被启动。

总结

就是这么简单。不需要执行数据库导出和导入的过程,因为 mysql 目录下所有需要的东西都已经复制过去了。需要执行导出/导入过程的场景是:有网站自己的数据库之外的数据库;mariadb 实例上还有其他网站,而你不想把这些网站复制到新服务器上。

迁移旧服务器上的其他网站也很容易。其他网站依赖的所有数据库都已经随着 mariadb 的迁移被转移到了新服务器上。你只需要把 /var/website 目录迁移到新服务器,添加合适的虚拟主机片段,然后重启 httpd。

我遵循这个过程把很多个网站从一个服务器迁移到另一个服务器,每次都没有问题。

via: https://opensource.com/article/21/9/migrate-wordpress

以上就是 wordpress网站迁移到新主机防火墙配置的详细内容,更多关于 wordpress网站迁移防火墙配置的资料请关注其它相关文章!