目前迁移前期准备已完成,现在需要将所有代码,放入已经准备好的 Apache + PHP 环境。
首先在 /home 目录下新建一个 GetTestAccount 目录用作我的 Web 启动目录,并将代码使用(SecureCRSecureFXPortable64)全数放入其中。
mkdir /home/GetTestAccount
cd /home/GetTestAccount/
代码全部拷入如下图,(PS:我的代码很简单,就是用 PHPExcel 实现下载数据并输出到 Excel 文件,所以没有用框架之类,请温柔吐槽)
修改 Apache Web 默认目录指向新建的这个目录中,需编辑 httpd.conf ,并更改 DocumentRoot 配置
vi /usr/local/apache2/conf/httpd.conf
修改以下配置指向新目录
DocumentRoot "/home/GetTestAccount"
<Directory "/home/GetTestAccount">
保存后重启 Apache 服务,使设置生效
systemctl restart httpd
在浏览器中输入地址 http://10.102.7.169/ 进行访问,发现它把我的文件目录给列出来了,所以需要再次修改 httpd.conf 配置,把默认页面给设置成我的索引页面,然后再次重启 Apache 服务后访问
注意:文件名区分大小写
<IfModule dir_module>
DirectoryIndex Index.php
</IfModule>
这一次页面虽然出现了,但是出现了中文乱码。
网上有朋友说将 php.ini 配置文件中的 default_charset = “UTF-8” 改成 default_charset = Off,这样可以解决页面中文字符乱问题的,但如果 SQL Server 用 UTF-8 (默认也是 UTF-8 ),并将查询结果直接打印出来,也会出现乱码。
在 Windows 编辑文本时,使用的是 gb2312 保存的文本,所以根据这一点入手,直接把我们的代码文件重新编码成 UTF-8 的格式
iconv Index.php -f gb2312 -t UTF-8 -o Index.php
用 file -i 文件名 可以查看文件编码
[root@localhost GetTestAccount]# file -i Index.php
Index.php: text/html; charset=utf-8
再次重新访问时,发现已经能够正常解析,并且数据库打印出的结果也不会出现乱码,但我现在只是转换了 Index.php 文件,其他文件还是 gb2312 编码,可以使用批量转换软件进行转换,也可以使用批量转换脚本
我这里使用 Shell 脚本进行批量转换,创建脚本前最好是先把想要转换的文件先打出来看看对不对,然后在进行脚本编写,find 命令这里不再描述
[root@localhost GetTestAccount]# find /home/GetTestAccount -maxdepth 1 -name "*.php"
/home/GetTestAccount/AccDel.php
/home/GetTestAccount/AccNew.php
/home/GetTestAccount/AccNewAdd.php
/home/GetTestAccount/BackAdmin.php
/home/GetTestAccount/DelAcc.php
/home/GetTestAccount/DownAdmin.php
/home/GetTestAccount/EmpDel.php
/home/GetTestAccount/EmpEdit.php
/home/GetTestAccount/EmpSave.php
/home/GetTestAccount/Index.php
/home/GetTestAccount/NewRole.php
/home/GetTestAccount/NewRoleAdd.php
/home/GetTestAccount/PHPExcel.php
/home/GetTestAccount/Role.php
/home/GetTestAccount/RoleAdd.php
/home/GetTestAccount/RoleDel.php
/home/GetTestAccount/Save.php
/home/GetTestAccount/SetAcc.php
/home/GetTestAccount/Type.php
/home/GetTestAccount/TypeAdd.php
/home/GetTestAccount/UpAcc.php
/home/GetTestAccount/View.php
/home/GetTestAccount/ViewAccDel.php
/home/GetTestAccount/Conn.php
/home/GetTestAccount/Down.php
确认我们需要更改的文件后,开始编写 Shell 脚本,并把脚本放在其他位置,我放在了 /home/ 目录下
cd /home/
vi gbtoutf.sh
加入以下信息,目录需要填正确,因为里面有一行 rm 删除语句,如果不放心,可以剔出来转换完成之后,单独使用
find /home/GetTestAccount -maxdepth 1 -name "*.php" | while read line;
do
echo $line
iconv -f gb2312 -t UTF-8 $line > ${line}.utf8
mv $line ${line}.gb2312
mv ${line}.utf8 $line
rm -rf /home/GetTestAccount/*.gb2312
done
保存之后使用 chmod 命令让脚本可执行,然后执行命令
chmod 744 gbtoutf.sh
./gbtoutf.sh
脚本执行后悔把刚才的文件目录打印出来,可以看一下是否是你所需的文件
[root@localhost home]# ./gbtoutf.sh
/home/GetTestAccount/AccDel.php
/home/GetTestAccount/AccNew.php
/home/GetTestAccount/AccNewAdd.php
/home/GetTestAccount/BackAdmin.php
/home/GetTestAccount/Conn.php
/home/GetTestAccount/DelAcc.php
/home/GetTestAccount/Down.php
/home/GetTestAccount/DownAdmin.php
/home/GetTestAccount/EmpDel.php
/home/GetTestAccount/EmpEdit.php
/home/GetTestAccount/EmpSave.php
/home/GetTestAccount/NewRole.php
/home/GetTestAccount/NewRoleAdd.php
/home/GetTestAccount/PHPExcel.php
/home/GetTestAccount/Role.php
/home/GetTestAccount/RoleAdd.php
/home/GetTestAccount/RoleDel.php
/home/GetTestAccount/Save.php
/home/GetTestAccount/SetAcc.php
/home/GetTestAccount/Type.php
/home/GetTestAccount/TypeAdd.php
/home/GetTestAccount/UpAcc.php
/home/GetTestAccount/View.php
/home/GetTestAccount/ViewAccDel.php
转换完毕,无报错,用 file -i 文件名 随机查看一下文件编码,发现已经转换成功。
[root@localhost GetTestAccount]# file -i Index.js
Index.js: text/plain; charset=iso-8859-1
[root@localhost GetTestAccount]# file -i Role.php
Role.php: text/html; charset=utf-8