侠客是放在阿里云的轻量应用服务器上的,我直接用的LAMP镜像。这几天感觉博客访问越来越慢,并且WP的后台提示我该升级PHP版本了。侠客的PHP版本还是5.x,确实要升级一下。升级php不知道又会遇到什么问题,还是换台服务器比较好。 这台服务器是在阿里云的北京机房,系统是ubuntu18.04。 首先更新系统。
apt-get update
apt-get upgrade
apt-get dist-upgrade
安装php
apt-get install php
php -v(查看版本)
安装php自动把apache也安装了,很奇怪。 安装Mysql
apt-get install mysql-server (中间会让输入root密码)
apt-get install mysql-client
#查看是否安装成功
mysql -u root -p
#回车,输入密码,也可能不需要输密码
select version();
exit; #退出mysql环境
打印phpinfo,查看php信息,如php.ini路径
#新建php文件并写入下述语句,通过浏览器访问文件
php echo phpinfo(); ?
浏览器访问服务器ip,可看到apache默认页面内容,页面上会显示出网页文件的根目录、配置文件路径等内容。
# It works!
This is the default welcome page used to test the correct
operation of the Apache2 server after installation on Ubuntu systems.
It is based on the equivalent page on Debian, from which the Ubuntu Apache
packaging is derived.
If you can read this page, it means that the Apache HTTP server installed at
this site is working properly. You should **replace this file** (located at
`/var/www/html/index.html`) before continuing to operate your HTTP server.
If you are a normal user of this web site and don't know what this page is
about, this probably means that the site is currently unavailable due to
maintenance.
If the problem persists, please contact the site's administrator.
# Configuration Overview
Ubuntu's Apache2 default configuration is different from the
upstream default configuration, and split into several files optimized for
interaction with Ubuntu tools. The configuration system is
**fully documented in
/usr/share/doc/apache2/README.Debian.gz**. Refer to this for the full
documentation. Documentation for the web server itself can be
found by accessing the **manual** if the **apache2-doc**
package was installed on this server.
The configuration layout for an Apache2 web server installation on Ubuntu systems is as follows:
/etc/apache2/
|– apache2.conf
| -- ports.conf |-- mods-enabled | |-- *.load |
– *.conf
|– conf-enabled
| -- *.conf |-- sites-enabled |
– *.conf
* **apache2.conf** is the main configuration file. It puts the pieces together by including all remaining configuration files when starting up the web server.
* **ports.conf** is always included from the main configuration file. It is used to determine the listening ports for incoming connections, and this file can be customized anytime.
* Configuration files in the **mods-enabled/**, **conf-enabled/** and **sites-enabled/** directories contain particular configuration snippets which manage modules, global configuration fragments, or virtual host configurations, respectively.
* They are activated by symlinking available configuration files from their respective \*-available/ counterparts. These should be managed by using our helpers **a2enmod, a2dismod,** **a2ensite, a2dissite,** and **a2enconf, a2disconf** . See their respective man pages for detailed information.
* The binary is called apache2. Due to the use of environment variables, in the default configuration, apache2 needs to be started/stopped with **/etc/init.d/apache2** or **apache2ctl**. Calling **/usr/bin/apache2** directly will not work with the default configuration.
# Document Roots
By default, Ubuntu does not allow access through the web browser to _any_ file apart of those located in /var/www, **public_html** directories (when enabled) and /usr/share (for web applications). If your site is using a web document root located elsewhere (such as in /srv) you may need to whitelist your document root directory in /etc/apache2/apache2.conf.
The default Ubuntu document root is /var/www/html. You can make your own virtual hosts under /var/www. This is different to previous releases which provides better security out of the box.
# Reporting Problems
Please use the ubuntu-bug tool to report bugs in the Apache2 package with Ubuntu. However, check [existing bug reports](https://bugs.launchpad.net/ubuntu/+source/apache2) before reporting a new bug.
Please report bugs specific to modules (such as PHP and others) to respective packages, not to the web server itself.
现在我要做的是修改配置文件,绑定侠客的域名。 为了不影响侠客在迁移过程中的访问,我先不修改xiake.me的解析ip,而先在本地的hosts文件中把xiake.me指向新的服务器的ip地址。
windows的hosts文件路径
C:\Windows\System32\drivers\etc
修改apache的配置文件,绑定xiake.me
cd /var/www
ls -l
mkdir xiake
ls -l
cd xiake
echo "hello world" > index.html
cd /etc/apache2/sites-available
vim xiake.conf
写入如下代码
ServerName www.xiake.me
ServerAdmin webmaster@localhost
DocumentRoot /var/www/xiake
ErrorLog ${APACHE\_LOG\_DIR}/error.log
CustomLog ${APACHE\_LOG\_DIR}/access.log combined
ServerName xiake.me
ServerAdmin webmaster@localhost
DocumentRoot /var/www/xiake
ErrorLog ${APACHE\_LOG\_DIR}/error.log
CustomLog ${APACHE\_LOG\_DIR}/access.log combined
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
更好的方式:
# https://httpd.apache.org/docs/2.4/vhosts/name-based.html
ServerName www.xiake.me
ServerAlias xiake.me
ServerAdmin webmaster@localhost
DocumentRoot /var/www/xiake
ErrorLog ${APACHE\_LOG\_DIR}/error.log
CustomLog ${APACHE\_LOG\_DIR}/access.log combined
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
保存后,启用配置
a2ensite xiake.conf
systemctl reload apache2
这时通过浏览器访问xiake.me会看到一句“hello world”。 安装phpMyAdmin
apt install phpmyadmin
创建符号链接
ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
访问“你的ip/phpmyadmin”测试。这里的用户名和密码是root和你的MySQL密码。 登录phpmyadmin出现错误
mysqli_real_connect(): (HY000/1698): Access denied for user 'root'@'localhost'
进入 /etc/php/7.2/apache2/php.ini 文件,去掉extension=mysqli前的分号“;”
vim检索:/mysqli,回车,跳到下一个按小写n,上一个是大写N。
按照 https://askubuntu.com/questions/763336/cannot-enter-phpmyadmin-as-root-mysql-5-7 的解决方案
#连接mysql
sudo mysql --user=root mysql
#创建新用户aoyu
CREATE USER 'aoyu'@'localhost' IDENTIFIED BY '密码';
GRANT ALL PRIVILEGES ON *.* TO 'aoyu'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
使用新用户aoyu登录,成功,问题解决。 开启SSL 由于存在风险,我先用另一个域名shuxueben.cn测试。
# 我是完全按照这篇文章来做的。https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-18-04
安装ftp server,VSFTP
apt-get install vsftpd
systemctl start vsftpd
systemctl enable vsftpd
#创建用户,用户名:aoyuftp
useradd -m aoyuftp
#修改/设置密码
passwd aoyuftp #回车后输入密码,两次
#配置文件
cp /etc/vsftpd.conf /etc/vsftpd.conf.bake
> /etc/vsftpd.conf
vim /etc/vsftpd.conf
#写入如下内容
listen=NO
listen_ipv6=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=YES
chroot_local_user=YES
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=vsftpd
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO
pasv_enable=Yes
pasv_min_port=10000
pasv_max_port=10100
allow_writeable_chroot=YES
#保存后就可以用ftp客户端登录了,端口22
#我用的是filezilla
我在shuxueben.cn安装wordpress时,由于文件权限,需要手动修改wp-config.php。我们需要修改文件的用户和用户组为www-data。apache的用户和用户组可以在/etc/apache2/envvars文件中看到。
root@iZ2zeajx0vuldrne51da6bZ:/var/www/shuxueben# chown www-data:www-data -R *
修改wordpress固定链接,发现.htaccess文件不可写。手动添加后也不能正常访问网页。怀疑apache相关模块未开启。 在phpinfo页面中发现 mod_rewrite 已开启。修改apache配置文件。
# https://www.howtoing.com/how-to-rewrite-urls-with-mod_rewrite-for-apache-on-ubuntu-18-04
#写入
Options FollowSymLinks
AllowOverride All
Require all granted
#加载配置
systemctl reload apache2
新服务器上的准备工作差不多已经做完了。在备份原数据前先对博客进行一些优化,避免迁移到新服务器后出现问题。 把主题、插件清理一下;垃圾评论清理一下;回收站文章、草稿清理一下;数据库下载一个插件清理一下。做完之后感觉侠客变快了很多,主要是我把jetpack插件停用了。 备份数据库。
#使用阿里云的dms管理数据库
sudo /usr/local/mysql/bin/mysql -uroot -p77Bef3a468cc
GRANT ALL PRIVILEGES ON *.* TO 'root'@'101.37.74.67' IDENTIFIED BY '77Bef3a468cc' WITH GRANT OPTION;flush privileges;
将备份的数据库文件通过phpmyadmin导入新服务器数据库。 出现错误:
Warning in ./libraries/plugin_interface.lib.php#551
count(): Parameter must be an array or an object that implements Countable
Backtrace
phpmyadmin版本是4.6.6,与php7.2不兼容。需要升级phpmyadmin。
#参考http://bilibala.cc/archives/manually-upgrad-phpmyadmin-4-6-to-4-8/
mv /usr/share/phpmyadmin/ /usr/share/phpmyadmin.backup
#通过ftp软件上传从phpmyadmin.net下载的压缩包
unzip phpMyAdmin-4.9.0.1-all-languages.zip
mv phpMyAdmin-4.9.0.1-all-languages phpmyadmin
chown root:root -R phpmyadmin/
mv phpmyadmin /usr/share/
从浏览器访问phpmyadmin,已升级,但出现如下问题:
配置文件现在需要一个短语密码。
变量 $cfg['TempDir'] (./tmp/)无法访问。phpMyAdmin无法缓存模板文件,所以会运行缓慢。
#解决变量 $cfg['TempDir'] (./tmp/)无法访问。phpMyAdmin无法缓存模板文件,所以会运行缓慢。
#注意先备份
vim /usr/share/phpmyadmin/libraries/vendor_config.php
#修改如下:(可照着升级前的文件改)
define('CONFIG_DIR', '/etc/phpmyadmin/');
#再修改一句:
define('TEMP_DIR', '/var/lib/phpmyadmin/tmp/');
#解决配置文件现在需要一个短语密码。
vim /usr/share/phpmyadmin/libraries/config.default.php
#其中某一句随便写一些字符
$cfg['blowfish_secret'] = 'dajiangdongqulangtaojinqiangufengliurenwu';
#大功告成,清理文件
rm -f /usr/share/phpmyadmin/libraries/vendor_config.php.backup
rm -rf /usr/share/phpmyadmin.backup
我用阿里云的dms导出数据库,导入数据库的时候提示错误。我又用了一个wp插件,用插件备份数据库,虽然有警告但导入成功。 迁移文件。
mv ./shuxueben/wordpress-5.2.2-zh_CN.tar.gz ./xiake
cd xiake
tar xzf wordpress-5.2.2-zh_CN.tar.gz
rm -f wordpress-5.2.2-zh_CN.tar.gz
chown www-data:www-data -R wordpress
mv wordpress/ mine
好吧,太烦了。我直接重装了wordpress,然后导入xml文件。 为侠客开启https
certbot --apache -d xiake.me -d www.xiake.me
SQL查询批量替换图片链接
UPDATE xk_posts SET post_content = REPLACE( post_content, 'https://www.xiake.me/mine/wp-content/uploads', 'https://www.xiake.me/wp-content/uploads' )