Skip to content

Google/Youtube/Wiki/Instagram等 一键镜像部署脚本 —— Zmirror

Google/Youtube/Wiki/Instagram等 一键镜像部署脚本 —— Zmirror

本文最后更新于 2017年3月3日 20:58 可能会因为没有更新而失效。如已失效或需要修正,请留言!

最近有人推荐我一个一键镜像部署脚本,我试了试发现还是很方便的。

不过也发现了一些问题,但是还是很不错的,而且Github上面写的还挺详细。


其他的一些镜像Google的教程:

  • 另一种利用Nginxi反向代理来简单镜像 HTTP(S)网站 的方法
  • 一种非常方便 反向代理(镜像)Google 的 Nginx Google 扩展

示例镜像

作者设置了密码,虽然这个密码没有固定的答案,所以你们输入:Github,就行了。

前置需求

1、一台墙外VPS,虚拟化技术 OpenVZ/Xen/KVM 均可。

2、操作系统:

  • 支持的操作系统:
    • Ubuntu 14.04/15.04(不支持HTTP2)/15.10/16.04+
    • Debian 8 (不支持HTTP/2)
    • 不支持 CentOS/RHEL/Windows/Fedora/Arch/…
  • 推荐的操作系统:
    • Ubuntu 16.04 x86_64
  • 全新(刚安装完成)的操作系统。如果系统中有其他东西,可能会产生冲突
  • root权限

3、域名

  • 每个镜像要求一个三级域名(类似于g.zmirrordemo.com这样的,有三部分,两个点)
  • 域名已经正常解析到VPS IP
提示:如果你没有自己的域名, 请看教你申请.tk/.ml/.cf/.gq/.ga等免费域名

安装步骤

没有SSL证书 (如果不懂, 请使用这个)

以下教程中,示例域名为: xxx.xxx ,示例邮箱为: [email protected] ,示例VPS IP为: 233.233.233.233 ,你们不要傻傻的跟着填。

注意:安装前请先解析好域名,否则SSL证书可能申请失败!
  1. sudo apt-get -y update && sudo apt-get -y install python3 git
  2. git clone https://github.com/aploium/zmirror-onekey.git –depth=1
  3. cd zmirror-onekey
  4. sudo python3 deploy.py

运行后会自动开始安装各个依赖,可能需要等几分钟,等安装完毕后会提示你添加镜像:

  1. [20:58:25] [INFO] executing: service apache2 start
  2. [20:58:27] [INFO] Load last installation’s settings successfully
  3. [20:58:27] [INFO] ———————-
  4. Please select mirror you want to deploy?
  5. select one mirror a time, you could select zero or more mirror(s)
  6. 1. Google (include scholar, image, zh_wikipedia)
  7. 2. twitter (PC ONLY)
  8. 3. twitter (Mobile ONLY)
  9. 4. youtube (PC ONLY)
  10. 5. youtube (Mobile ONLY)
  11. 6. instagram
  12. 0. Go to next steps. (OK, I have selected all mirror(s) I want to deploy)
  13. input 0-6:

这是提示你添加镜像,Twitter 和 Youtube区分PC和手机的,而且域名也不能相同。

你输入对应的数字,比如 1 ,Google,然后会提示你输入Google镜像的域名。

  1. Please input *your* domain for this mirror (google):
注意:在这整个步骤中,输入的任何文本都无法 退车键 删除,所以不要打错字,否则就必须重来了。

输入域名后回车,会检测你的域名解析是否正确,但是好像脚本有问题,会识别本地IP为 127.0.0.1 ,所以这一步直接输入,然后回车。

注意:安装前请先解析好域名,否则SSL证书可能申请失败!
  1. [20:59:05] [WARNING] Sorry, your domain(google.xxx.xxx)’s ip does not equals to this machine’s ip.
  2. domain’s ip is: 233.233.233.233
  3. this machine’s ip is: 127.0.0.1
  4. Continue anyway? (y/N):

回车后又会回到上面添加镜像的提示,你可以继续添加镜像或者输入 0 来继续下一步,回车后会提醒你输入一个邮箱地址(需要真实)这个是申请SSL证书用的。

  1. Please input your email (because letsencrypt requires an email for certification)

回车后就会限速你输入的邮箱,同时提示你是否对镜像设置密码,不使用输入 N 回车。

  1. [21:00:04] [INFO] Your email: [email protected]
  2. [21:00:04] [INFO] zmirror can provide simple verification via password
  3. just as you may have seen in zmirror’s demo sites (however, demo sites does not require correct answer)
  4. Do you want to protect your mirror by password? (y/N):

回车后会再次显示你的 邮箱和添加的镜像域名 ,确认无误后输入 Y 回车。

  1. [21:00:18] [INFO] ———————-
  2. [21:00:18] [INFO] Now, we are going to install, please check your settings here:
  3. Email: [email protected]
  4. Mirror: google Domain: google.xxx.xxx
  5. Mirror: youtubePC Domain: youtube.xxx.xxx
  6. Are these settings correct (Y/n)?

然后就会开始自动申请SSL并且配置文件了,等一切配置完毕之后,就会提示下面的内容。

  1. [21:00:59] [INFO] Restarting apache2
  2. [21:00:59] [INFO] executing: service apache2 restart
  3. [21:01:00] [INFO] Finishing…
  4. [21:01:02] [INFO] Congratulation!
  5. [21:01:02] [INFO] If apache is not running, please execute `sudo service apache2 restart`
  6. [21:01:02] [INFO] ———— mirrors ————
  7. Mirror: google URL: https://google.xxx.xxx/
  8. Mirror: youtubePC URL: https://youtube.xxx.xxx/
  9. [21:01:02] [INFO] ———— mirrors ————
  10. [21:01:02] [INFO] FAQs are here: http://tinyurl.com/zmirrorfaq
  11. [21:01:02] [INFO] For more information, please view zmirror’s github: https://github.com/aploium/zmirror/
  12. [21:01:02] [INFO] Contribution and Issues are more than welcomed.
  13. [21:01:02] [INFO] btw, if you feeling good, I’ll be grateful for your Star in github :)

如果没有提示出错,那么就一切正常安装完毕了。

这时候你就可以使用 https://google.xxx.xxx/ 来访问Google镜像,使用https://youtube.xxx.xxx/ 来访问PC的Youtube镜像了。

已有SSL证书

如果已有证书,希望使用自己提供的证书,而不是通过 let’s encrypt 获取。

请将上面代码中的第四行替换成下面的样子,在运行期间会提示你输入证书路径的:

  1. sudo apt-get -y update && sudo apt-get -y install python3 git
  2. git clone https://github.com/aploium/zmirror-onekey.git –depth=1
  3. cd zmirror-onekey
  4. sudo python3 deploy.py –i-have-cert
警告:不支持加密的私钥,如果私钥有密码加密,请先解密。

特性

  1. 支持一次部署多个镜像, 支持同VPS多镜像
  2. 自动安装 let’s encrypt 并申请证书, 启用HTTPS
  3. 自动添加 let’s encrypt 的定期renew脚本到crontab
  4. 启用HTTP/2 ps:Debian8和Ubuntu15.04不支持HTTP/2
  5. 启用HSTS

FAQ

  • 安装完成后各个程序的文件夹在哪?

zmirror:

安装在 /var/www/镜像名 文件夹下

镜像名为每个镜像的名字, 比如YoutubePC就是 /var/www/youtubePC

let’s encrypt:

本体在: /etc/certbot/

申请到的证书位置, 请看 certbot文档-where-are-my-certificates

Apache:

Apache的配置文件在 /etc/apache2/

其中各个站点的配置文件在 /etc/apache2/sites-enabled/

Apache日志文件在 /var/log/apache2/镜像名_后缀.log

后缀为 _error 的日志文件中, 同时包含了stdout的输出(无论是否是错误), 对debug会有帮助

  • 为什么安装的是Apache, 而不是Nginx, 我可以选择吗?

因为Apache的wsgi对python更友好,而且Nginx没有Visual Host功能。

在性能上,由于性能瓶颈是zmirror本身,所以Apache和Nginx之间的性能差距可以被忽略

目前一键脚本只能安装Apache,不支持Nginx, 也没有支持Nginx的计划,如果需要Nginx,请手动部署。

手动部署可以参考 zmirror wiki

  • 安装的Apache版本?

在Ubuntu中,使用的是 PPA:ondrej/apache2 理论上应该是最新版,或者接近最新版(2.4.23+)

在Debian8中,使用系统的 apt-get 安装,版本比较旧,所以Debian不支持HTTP/2

  • Let’s encrypt 证书自动更新?

安装脚本会自动创建定期更新证书的脚本,脚本位置为 /etc/cron.weekly/zmirror-letsencrypt-renew.sh

  • 证书有效期为什么只有90天?

主要是因为Let’s encrypt认为,证书的申请和部署可以自动化时,90天足够了。

具体可以看这个官方说明(可能需要自备梯子)。

本安装脚本会在linux定时任务(crontab)中加入自动续期的脚本,不用担心证书过期。

即使自动续期脚本万一失效了,let’s encrypt也会在快要过期时邮件通知你。

  • 其他高级功能, 比如说CDN, 在哪?

这个脚本只提供最基础的部署,高级功能需要手动配置。

请看 config_default.py custom_func.sample.py 中的说明

如果想用CDN,可以看这个教程使用七牛作为zmirror镜像CDN

警告:

如果你想要修改 config_default.py 中的某项设置,请不要直接修改。

而应该将它复制到 config.py 中,然后修改 config.py 里的设置。

config.py 中的设置会覆盖掉 config_default.py 中的同名设置。

除非你是开发者,否则无论如何都不应该修改 config_default.py

  • 网速太慢?

可以尝试使用一些单边的优化加速方法,传送门

或者换一个网速快的VPS,比如:Vultr,注册后送20美元,5美元用5个月。

Vultr是KVM,支持锐速。

  • 证书获取失败

脚本使用Let’s encrypt(certbot)来获取证书。

certbot 会在本地 Listen 80 或者 443 端口,然后由远程授权服务器根据域名的A记录来访问本机

当远程服务器成功连接到本机的certbot客户端后, 就会颁发证书。

详细流程请看官方文档How It Works

证书获取失败最有可能的原因是域名记录设置后尚未来得及生效,域名DNS记录的生效通常需要数分钟以上,最长可达72小时。

对于这种情况, 除了等待以外是没有什么办法的。

本脚本在默认的5次尝试失败后,会提示是否一直尝试下去,如果你确认DNS记录已经正常设置,请在提示

  1. max retries exceed, do you want to continue retry infinity?(Y/n)

时,选择,一般数分钟内就能成功。

如果不能确定是否正常设置,可以访问 https://www.whatsmydns.net/,这个网站可以在全球范围内查询A记录。

如果查询出的A记录与你的IP相同,就表示设置成功了,此时只需要让脚本自行尝试即可。

如果此时仍然多次尝试失败,请看下面的手动运行lets encrypt获取证书部分。

手动运行lets encrypt获取证书

如果能确认DNS记录已经设置正常, 但是仍然无法获取证书, 请尝试手动运行letsencrypt获取证书:

  1. sudo service apache2 stop
  2. sudo /etc/certbot/certbot-auto certonly –standalone -d “你的域名1”
  3. sudo /etc/certbot/certbot-auto certonly –standalone -d “你的域名2”
  4. sudo service apache2 start

或者(如果上面的仍然失败)

  1. sudo apt-get install letsencrypt
  2. sudo service apache2 stop
  3. sudo letsencrypt certonly –standalone -d “你的域名1”
  4. sudo letsencrypt certonly –standalone -d “你的域名2”
  5. sudo service apache2 start

并在手动获取证书成功后再次运行本脚本

更新zmirror

请运行以下代码(假设zmirror-onekey是本脚本文件夹):

  1. cd zmirror-onekey
  2. git pull
  3. sudo python3 deploy.py –upgrade-only

注意: 更新zmirror以后会自动重启Apache

本教程转载自Github:https://github.com/aploium/zmirror-onekey

转载请超链接注明:逗比根据地 » Google/Youtube/Wiki/Instagram等 一键镜像部署脚本 —— Zmirror
责任声明:本站一切资源仅用作交流学习,请勿用作商业或违法行为!如造成任何后果,本站概不负责!

Comments are closed, but trackbacks and pingbacks are open.