手工部署 Zmirror 鏡像

部署環境

  • 一臺國外 VPS
  • Ubuntu 14.04
  • Apache

安裝步驟

初始化環境

  • 我在安裝過程當中讓升級 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, 在裏面加上本身的域名python

  • 在大約第 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>
相關文章
相關標籤/搜索