将WordPress博客迁移到全新Aliyun服务器上

1. 购买Aliyun服务器并配置安全组

购买Aliyun服务器后,在Aliyun官网登录个人用户,然后打开云服务器ECS服务器——实例与镜像——实例——在实例列表中点击目标实例的管理——点击配置信息中的专有网络名称——点击安全组一栏中的数字1——在安全组列表中点击配置规则——点击快速创建规则,按如下方式创建规则:

网卡类型:内网
规则方向:入方向
授权策略:允许
常用端口(TCP):HTTP (80) HTTPS (443) 根据需要勾选相应的端口,我勾选了如上端口,或通过下方自定义端口方式进行设置。
自定义端口:TCP  80
优先级:1
授权类型:IPv4地址段访问
授权对象:0.0.0.0/0 

推荐使用自定义端口方法,每次开放一个端口。比如,开放21,22,25,80,443,3306、8080和9090等端口。
新版本的设置可以同时一次性设置多个端口。使用1-65535则可直接在安全组中开放所有端口。
我个人选择开放52000-53000端口。

创建以上安全组,表示允许通过相应的端口访问Aliyun服务器的公网IP。需要注意默认设置下,aliyun服务器使用ifconfig查看网卡时是没有公网IP网卡的,所以即使服务器系统中设置了开放80端口,外网也不能访问Aliyun服务器的80端口。以前的Aliyun服务器没有安全组设置,所有端口都是开放的,则使用起来更方便。

2. 进入Aliyun服务器, 安装启动httpd和mariadb软件并配置系统

配置Aliyun服务器,选择64位的CentOS 8.1操作系统。第一次启动Aliyun服务器后,登录root用户,配置ssh和root权限:

perl -i.bak -e 'while (<>) { if (/^root/) { print; print "chenlianfu   ALL=(ALL)       NOPASSWD:ALL\n"; last; } else { print } }' /etc/sudoers

# 修改/etc/ssh/sshd_config配置文件,使openssh远程登录更安全,更快速
perl -p -i -e 's/#RSAAuthentication/RSAAuthentication/' /etc/ssh/sshd_config
perl -p -i -e 's/#PubkeyAuthentication/PubkeyAuthentication/' /etc/ssh/sshd_config
perl -p -i -e 's/#AuthorizedKeysFile/AuthorizedKeysFile/' /etc/ssh/sshd_config
perl -p -i -e 's/.*PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
perl -p -i -e 's/.*Protocol\s+2.*/Protocol 2/' /etc/ssh/sshd_config
perl -p -i -e 's/.*ClientAliveInterval.*/ClientAliveInterval 60/' /etc/ssh/sshd_config
perl -p -i -e 's/.*ClientAliveCountMax.*/ClientAliveCountMax 10/' /etc/ssh/sshd_config
perl -p -i -e 's/.*UseDNS.*/UseDNS no/' /etc/ssh/sshd_config
perl -p -i -e 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
perl -p -i -e 's/.*GatewayPorts.*/GatewayPorts yes/' /etc/ssh/sshd_config
systemctl restart sshd.service

# 注意,一定要设置GatewayPorts参数值为yes,有利于使用其它端口直接通过本服务器为跳板登录内网服务器。否则,当使用Aliyun云服务器的反向隧道一步直接登录内网服务器会不成功。

使用yum安装系统软件:

yum install httpd mariadb mariadb-devel mariadb-server php php-mysqlnd php-json

开放系统防火墙端口

# 启动防火墙服务
systemctl start firewalld.service
systemctl enable firewalld.service

# 开启80/8080/3306端口
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=3306/tcp --permanent
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --add-port=52000-53000/tcp --permanent
# 加入--permanent参数,使永久生效。
# 重启防火墙服务
systemctl restart firewalld.service
# 重启后,再查看端口,则生效了。
firewall-cmd --list-ports

# 启动httpd和mariadb(mysqld)服务
systemctl start httpd.service
systemctl start mariadb.service
#设置服务开机启动
systemctl enable httpd.service
systemctl enable mariadb.service

初始化mysql数据库并启动mysql数据库 :

cat << EOF > /etc/my.cnf
[client]
port                    = 3306
socket                  = /var/lib/mysql/mysql.sock
[mysqld]
port                    = 3306
socket                  = /var/lib/mysql/mysql.sock
skip-external-locking
key_buffer_size         = 384M
max_allowed_packet      = 1M
table_open_cache        = 512
sort_buffer_size        = 2M
read_buffer_size        = 2M
read_rnd_buffer_size    = 8M
myisam_sort_buffer_size = 64M
thread_cache_size       = 8
query_cache_size        = 32M
thread_concurrency      = 8
log-bin=mysql-bin
server-id               = 1
[mysqldump]
quick
max_allowed_packet      = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size         = 256M
sort_buffer_size        = 256M
read_buffer             = 2M
write_buffer            = 2M
[mysqlhotcopy]
interactive-timeout
EOF

systemctl restart mariadb.service
/usr/bin/mysql_secure_installation

3. 配置httpd来搭建网站

主要为3个人搭建了个人博客网站。首先在Aliyun服务器中创建3个人的账户信息:

cat <<EOF > users.txt
chenlianfu
zhengyue
wuchangsong
EOF

#创建3个账户并随即生成密码,修改家目录权限和apache用户权限
for i in `cat users.txt`
do
    useradd $i &> /dev/null
    create_random_passwd.pl $i
    chmod 750 /home/$i
    usermod -aG $i apache
done

修改httpd配置文件/etc/httpd/conf/httpd.conf,修改几处内容:

# 将文件夹权限设置宽松,有利于展示其它生信软件的网页结果
perl -i -e 'while (<>) { $mo = 1 if m#<Directory />#; $mo = 0 if m#<Files \".ht\*\">#; s/Require all denied/#Require all denied/ if $mo == 1; print; }' /etc/httpd/conf/httpd.conf
# 使.pl .sh .py等文件的内容直接在浏览器中展示
perl -p -i -e 's/(AddType text\/html .shtml)/$1\nAddType text\/plain .pl\nAddType text\/plain .py\nAddType text\/plain .sh/' /etc/httpd/conf/httpd.conf
# 使.cgi文件能在浏览器中运行程序生成网页结果
perl -p -i -e 's/#AddHandler cgi-script .cgi/AddHandler cgi-script .cgi/' /etc/httpd/conf/httpd.conf
# 重启网页服务,使配置文件的修改生效
systemctl restart httpd.service

根据需要展示的文件夹信息,在etc/httpd/conf/httpd.conf尾部增加:

Alias /public "/home/public"
<Directory "/home/public">
    AllowOverride None
    Options Indexes MultiViews FollowSymLinks ExecCGI
    Order allow,deny
    Allow from all
</Directory>

根据建站需要添加建站信息,在/etc/httpd/conf/httpd.conf尾部增加:

#以下是3个人的博客网站
<VirtualHost *:80>
    DocumentRoot /home/chenlianfu/wordpress
    ServerName www.chenlianfu.com
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot /home/zhengyue/wordpress
    ServerName zhengyue90.com
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot /home/wuchangsong/wordpress
    ServerName www.wuchangsong.com
</VirtualHost>

#以下是我的网址导航页面网站
<VirtualHost *:80>
    DocumentRoot /home/chenlianfu/homepage
    ServerName homepage.chenlianfu.com
</VirtualHost>
<Directory "/var/www/chenlianfu_homepage">
    AllowOverride None
    Options MultiViews FollowSymLinks ExecCGI
    Order allow,deny
    Allow from all
</Directory>

在/etc/httpd/conf.d目录下分别生成对3个网站对应的数据文件夹的权限配置文件,用于禁止一些不好的IP(博客中出现垃圾评论的网址)对网站的访问。例如,生成名为deny_chenlianfu.conf的文件,其内容:

<Directory "/home/chenlianfu/wordpress">
    AllowOverride None
    Options MultiViews FollowSymLinks ExecCGI
    Order allow,deny
    Allow from all
    Deny from 1.9.8.6
    Deny from 100.42.17.90
    Deny from 101.4.136.34
    ...
    Deny from 95.85.80.82
    Deny from 95.85.80.86
    Deny from 98.174.90.36
</Directory>

最后,重启httpd服务:

systemctl restart httpd.service

4. 迁移软件安装文件夹和Mysql数据库

WordPress数据分两部分:第一部分是WordPress软件安装文件夹;第二部分数据位于Mysql数据库中。

直接将第一部分WordPress软件安装文件夹copy到Aliyun新服务器上,再修改一些权限:

chmod 775 /home/chenlianfu/wordpress
cd /home/chenlianfu/wordpress
find ./ -perm 755 -exec chmod 775 {} \;
find ./ -perm 644 -exec chmod 664 {} \;

将原来wordpress的mysql数据库复制到/var/lib/mysql/目录下,再修改一些权限:

chown -R mysql:mysql /var/lib/mysql/wordpress_chenlianfu/
chown -R apache:chenlianfu /home/chenlianfu/wordpress

5. 每天自动备份WordPress数据到邮箱

首先,搭建邮箱服务器

# 安装postfix软件
yum install -y postfix*

# 修改postfix配置文件
perl -p -i -e 's/.*myhostname = host.*/myhostname = chenlianfu.com/' /etc/postfix/main.cf
perl -p -i -e 's/inet_interfaces = localhost/inet_interfaces = all
/' /etc/postfix/main.cf
perl -p -i -e 's/localhost,\s*\n/localhost, chenlianfu\n/ if m/^mydestination/' /etc/postfix/main.cf
echo "message_size_limit = 100000000" >> /etc/postfix/main.cf

# 开放系统25号端口
firewall-cmd --add-port=25/tcp --permanent
systemctl restart firewalld.service

# 启动postfix服务
systemctl restart postfix.service 
systemctl enable postfix.service

然后,生成脚本程序,能备份WordPress并将数据发送到邮箱。

echo '/bin/tar -C /var/lib/mysql/ -zc -f /var/lib/mysql/wordpress_chenlianfu_mysql_$(date +%Y%m%d).tar.gz wordpress_chenlianfu/
/bin/date | /bin/mail -s wordpress_chenlianfu_mysql_$(date +%Y%m%d).tar.gz -a /var/lib/mysql/wordpress_chenlianfu_mysql_$(date +%Y%m%d).tar.gz chenllianfu@foxmail.com' > /root/bakup_wordpress_mysql.sh

echo '/bin/tar -C /home/chenlianfu -zc -f /home/chenlianfu/wordpress_chenlianfu_dir_$(date +%Y%m%d).tar.gz wordpress/
/bin/date | /bin/mail -s wordpress_chenlianfu_dir_$(date +%Y%m%d).tar.gz -a /home/chenlianfu/wordpress_chenlianfu_dir_$(date +%Y%m%d).tar.gz chenllianfu@foxmail.com' > /root/bakup_wordpress_dir.sh

chmod 755 bakup_wordpress_*

# 需要注意的是Aliyun禁止了25号端口对外发送数据。所以需要申请解封25号端口才能正常发送邮件。
# 可能申请不会成功,则考虑使用具有SSL加密功能的IMAP服务发送邮件(http://www.chenlianfu.com/?p=3227),需要邮箱支持。

最后,设置定时执行备份程序。

# 设置每天1点钟时备份mysql数据库数据,并发送到邮箱
# 设置每星期一1点5分钟时备份wordpress文件夹数据,并发送到邮箱
crontab -l > /root/.crontab
echo -e "0\t1\t*\t*\t*\t/root/bakup_wordpress_mysql.sh" >> /root/.crontab
echo -e "5\t1\t*\t*\t1\t/root/bakup_wordpress_dir.sh" >> /root/.crontab
sort /root/.crontab | uniq | crontab

6. 定时自动重启服务,让网站更稳定

# 生成一个重启网站服务的脚本文件
cat << EOF > /root/restart_webServer.sh
/bin/systemctl restart mariadb.service
/bin/systemctl restart httpd.service
/bin/systemctl restart postfix.service
EOF
chmod 755 /root/restart_webServer.sh

# 设置每小时零分钟时运行一次重启程序
crontab -l > /root/.crontab
echo -e "0\t*\t*\t*\t*\t/root/restart_webServer.sh" >> /root/.crontab
sort /root/.crontab | uniq | crontab

7. 服务器其它资料迁移

服务器中需要迁移的其它文件或文件夹:

/root/create_random_passwd.pl
/root/monitoring_netflow_of_IPs.pl
/root/monitoring_netflow_of_IPs.sh
/root/.certs/
/etc/mail.rc

# 根据需求决定是否copy以下文件,这些文件可能在上述流程中已经修改好了。
/etc/httpd/conf/httpd.conf
/etc/my.cnf

# 此外,各用户的家目录内容根据需要copy。

服务器需要安装的其它系统软件和服务启动项:

yum install -y nss-tools

# 先开启9090端口,再启用cockpit服务,则可以在浏览器中输入ip地址:9090登录服务器,监控服务器各项信息,并连接服务器终端进行操作。
firewall-cmd --add-port=9090/tcp --permanent
systemctl restart firewalld.service
systemctl enable --now cockpit.socket