手工部署Zmirror镜像
2016年11月7日
教程参考Github作者的Wiki
部署Tumblr的写在了这里
部署环境
安装步骤
初始化环境
- 我在安装过程中让升级
grub2
, 我选择不不升级(应该怎么选都不影响)
sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&
sudo apt-get update &&
sudo apt-get upgrade -y &&
sudo apt-get dist-upgrade -y &&
sudo apt-get install build-essential patch binutils make devscripts nano libtool libssl-dev libxml2 libxml2-dev software-properties-common python-software-properties dnsutils git wget curl python3 python3-pip iftop -y &&
sudo python3 -m pip install -U flask requests cchardet fastcache
安装 Apache2
LC_ALL=C.UTF-8 sudo add-apt-repository -y ppa:ondrej/apache2 &&
sudo apt-key update &&
sudo apt-get update &&
sudo apt-get upgrade -y &&
sudo apt-get install apache2 -y &&
sudo a2enmod rewrite mime include headers filter expires deflate autoindex setenvif ssl http2 &&
sudo apt-get install libapache2-mod-wsgi-py3 -y
安装 zmirror – Google
- 假设将 zmirror 安装到
/var/www/zmirror
,本教程以部署 Google 镜像为例,即 使用这个配置文件more_configs/config_google_and_zhwikipedia.py
cd /var/www &&
git clone https://github.com/aploium/zmirror &&
cd zmirror &&
chown -R www-data . &&
chgrp -R www-data . &&
cp more_configs/config_google_and_zhwikipedia.py config.py
配置 zmirror – Google
需要手动修改 config.py
, 在里面加上自己的域名
- 在大约第38行开始处, 的
# ############## Local Domain Settings ##############
my_host_name = '127.0.0.1'
my_host_scheme = 'http://'
- 修改为如下, 修改两行, 添加一行
- 请将其中的
g.caisan.ml
替换为你是自己实际的域名 - 新添加的
verbose_level = 2
这一行, 把 zmirror 的日志级别设置为Warning
, 减少日志产生量. - 默认是3级, 会产生大量debug日志
- 请将其中的
# ############## Local Domain Settings ##############
my_host_name = 'g.caisan.ml'
my_host_scheme = 'https://' # 注意把上面这行的http改成https
verbose_level = 2
安装 let’s encrypt 并获得证书
- 证书来源
本教程使用let’s encrypt证书, 获取非常快, 但是有效期只有90天, 到期前需要重新获取
请将下面脚本中g.caisan.ml
域名修改为你自己的域名, 修改后能直接复制进去运行
为保证兼容性, 本教程使用standalone模式获取证书, 所以需要先停掉apache(包含在下面脚本中了)
sudo service apache2 stop &&
cd ~ &&
git clone https://github.com/certbot/certbot &&
cd certbot &&
./certbot-auto certonly --agree-tos -t --standalone -d g.caisan.ml
- 生成的证书存储在
/etc/letsencrypt/live/g.caisan.ml/
配置 Apache2
cd /etc/apache2/conf-enabled &&
wget https://gist.githubusercontent.com/aploium/8cd86ebf07c275367dd62762cc4e815a/raw/29a6c7531c59590c307f503b186493e559c7d790/h5.conf
设置网站配置文件
- 然后进入
/etc/apache2/sites-enabled
文件夹,创建一个google.conf
文件, 内容如下(记得修改对应的域名和文件夹等东西)
<IfModule mod_ssl.c>
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
<VirtualHost *:443>
# 域名, 记得修改成你自己的
ServerName g.caisan.ml
# 这个没用的
ServerAdmin root@localhost
# 下面两个log文件路径也建议按实际修改
# 默认保存在 /var/log/apache2/ 文件夹下
# ErrorLog 中包含了zmirror产生的stdout输出, 若需要debug可以看它
ErrorLog ${APACHE_LOG_DIR}/zmirror-google_ssl_error.log
CustomLog ${APACHE_LOG_DIR}/zmirror-google_ssl_access.log combined
# ##### WSGI 这部分是重点 ######
WSGIDaemonProcess zmirror_google user=www-data group=www-data threads=16
#这是刚刚安装的zmirror的路径
WSGIScriptAlias / /var/www/zmirror/wsgi.py
WSGIPassAuthorization On
# 给予zmirror文件夹权限
<Directory /var/www/zmirror>
WSGIProcessGroup zmirror_google
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
# ######### SSL部分 这部分告诉Apache你的证书和私钥在哪 #########
# 下面使用的是刚刚let's encrypt给我们的证书, 你也可以用别的
SSLEngine on
# 私钥
SSLCertificateFile /etc/letsencrypt/live/g.caisan.ml/cert.pem
# 证书
SSLCertificateKeyFile /etc/letsencrypt/live/g.caisan.ml/privkey.pem
# 证书链
SSLCertificateChainFile /etc/letsencrypt/live/g.caisan.ml/chain.pem
# HTTP/2
<IfModule http2_module>
Protocols h2 h2c http/1.1
</IfModule>
</VirtualHost>
</IfModule>
重启 Apache2
sudo service apache2 restart
设置重定向
- 在
/etc/apache2/sites-enabled/000-default.conf
中加入以下设置, 使得HTTP能自动跳转到HTTPS- 加在
<VirtualHost> </VirtualHost>
括起来范围的里面
- 加在
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</IfModule>