LoginSignup
0
1

More than 3 years have passed since last update.

h2oの設定(Raspbian buster)

Last updated at Posted at 2020-04-27

1 はじめに

 自宅サーバーRaspbian3 B(RAM:1GB,ストレージ32GBmicroSDカード)で立ち上げました。Webはh2o、証明書はcertbotでフリーの証明書を取得、fcgiwrapでcgiやり放題にしました。目的は個人的にpythonやrubyで記述したスクリプトをWeb上で動かしたかっただけです。
以下の内容は自分のサイトにも公開してあります。

2 サーバーソフトh2oのインストール

$sudo apt-get install h2o

3 ドキュメントルートの設定

/home/hogehoge/public_html/下を公開する設定

/etc/h2o/h2o.confを 編集

server-name: "h2o (Debian)"
user: www-data
access-log: "|rotatelogs -l -f -L /var/log/h2o/access.log -p /usr/share/h2o/compress_logs /var/log/h2o/access.log.%Y-%m-%d 86400"
error-log: "|rotatelogs -l -f -L /var/log/h2o/error.log -p /usr/share/h2o/compress_logs /var/log/h2o/error.log.%Y-%m-%d 86400"
pid-file: /run/h2o.pid

hosts:
  "hogehoge.mydns.jp:80":
    listen:
     port: 80
    paths:
      /:
        file.dir: /home/hogehoge/public_html
        redirect:
            url: /index.html/
            internal: YES
            status: 307
      /server-status:
        status: ON

冒頭のserver-nameはdefaultのままです。
user: はwww-dataに変更しました。
file.dir:にwebのルートディレクトリを記述します。
h2o.confはレベルによって字下げをきちんとしないと文法エラーになります。
チェック方法は

# h2o  -c h2o.conf  -t

エラーがなかったら起動します。

# systemctl  restart h2o

4 cgiを動かす設定

fcgiwrapをインストールします。

$ sudo apt-get install fcgiwrap

/etc/h2o/h2o.conf

server-name: "h2o (Debian)"
user: www-data
access-log: "|rotatelogs -l -f -L /var/log/h2o/access.log -p /usr/share/h2o/compress_logs /var/log/h2o/access.log.%Y-%m-%d 86400"
error-log: "|rotatelogs -l -f -L /var/log/h2o/error.log -p /usr/share/h2o/compress_logs /var/log/h2o/error.log.%Y-%m-%d 86400"
pid-file: /run/h2o.pid

hosts:
  "hogehoge.mydns.jp:80":
    listen:
     port: 80
    paths:
      /:
        file.dir: /home/hogehoge/public_html
        redirect:
            url: /index.html/
            internal: YES
            status: 307
        file.custom-handler:
            extension: .cgi
            fastcgi.spawn:
                command: "exec /usr/sbin/fcgiwrap"

      /server-status:
        status: ON

ちなみに以下は動作確認のためのサンプルスクリプト
(わざわざclassを記述しているけど覚えたてで使ってみたかっただけ)

#!/usr/bin/env python3.7
class htmlput():
    def __init__(self):
        print('Content-type: text/html \n\n')
    def html_send(self,msg_str):
        txt='''
        <html>
        <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <title>pythonでwebページを表示</title>
        </head>
        <body link="#0000EE" vlink="#551A8B" text="#000000" bgcolor="#ffff99" alink="#EE0000">
        {}
        <br>
        </body>
        </html>
        '''
        print(txt.format(msg_str))

htm=htmlput()
htm.html_send('python ためし<br>')

5 BASIC認証の設定

/home/hogehoge/puclic_html/kisho のところだけBASIC認証する設定です。
まずmrubyをインストールします。

$ sudo apt-get install mruby

/etc/h2o/h2o.confファイルの設定

hosts:
  "hogehoge.mydns.jp:80":
    listen:
     port: 80
    paths:
      /:
        file.dir: /home/hogehoge/public_html
        redirect:
            url: /index.html/
            internal: YES
            status: 307
        file.custom-handler:
            extension: .cgi
            fastcgi.spawn:
                command: "exec /usr/sbin/fcgiwrap"
      /kisho:
        mruby.handler: |
            require "htpasswd.rb"
            acl {
            use Htpasswd.new("/home/hogehoge/.htpasswd", "realm-name")
            }
        file.dir: /home/hogehoge/public_html/kisho
        file.custom-handler:
            extension: .cgi
            fastcgi.spawn:
                command: "exec /usr/sbin/fcgiwrap"
      /server-status:
        status: ON

上のリストに"/home/hogehoge/.htpasswd", "realm-name"
と記述しましたがhogehogeのホームディレクトリでユーザidとパスワードを
次のように設定します。

$ htpasswd -nbm ユーザid   パスワード  > .htpasswd
$ sudo chown web-data .htpasswd
$ sudo chmod 400 .htpasswd

あとは h2o -c h2o.conf -t でh2o.confのチェックがとおったらh2oを再起動する。

6 certbotでSSL化

certbotを使えば無料で認証局CAの証明書がもらえます。以前はletsenryptというパッケージだったらしい。
インストール

# apt-get install certbot

SSL証明書を発行してもらう前に必ずサーバーを起動してく。それから以下のコマンドを実行

# certbot certonly --webroot -w /home/hogehoge/public_html/ -d hogehoge.mydns.jp --agree-tos

成功すると
/etc/letsencrypt/live/[FQDN]/ の下に以下のファイルができる。

privkey.pem : the private key for your certificate.
fullchain.pem: the certificate file used in most server software.
chain.pem : used for OCSP stapling in Nginx >=1.3.7.
cert.pem : will break many server configurations, and should not be used

もし鍵を削除する場合は

# certbot delete --cert-name hogehoge.mydns.jp

次に/etc/h2o/h2o.confの設定

server-name: "h2o (Debian)"
user: www-data
access-log: "|rotatelogs -l -f -L /var/log/h2o/access.log -p /usr/share/h2o/compress_logs /var/log/h2o/access.log.%Y-%m-%d 86400"
error-log: "|rotatelogs -l -f -L /var/log/h2o/error.log -p /usr/share/h2o/compress_logs /var/log/h2o/error.log.%Y-%m-%d 86400"
pid-file: /run/h2o.pid
file.index: ['index.html']

listen: 80
listen:
  port: 443
  ssl:
    certificate-file: /etc/letsencrypt/live/hogehoge.mydns.jp/fullchain.pem
    key-file: /etc/letsencrypt/live/hogehoge.mydns.jp/privkey.pem

hosts:
  # httpでアクセスがあったらhttpsにリダイレクト
  "hogehoge.mydns.jp:80":
    paths:
      "/":
        redirect:
          url:  https://hogehoge.mydns.jp/
          status: 301

  "hogehoge.mydns.jp:443":
    paths:
      "/":
        file.dir: /home/hogehoge/public_html
        file.custom-handler:
          extension: .cgi
          fastcgi.spawn:
            command: "exec /usr/sbin/fcgiwrap"
      "/kisho":
        mruby.handler: |
          require "htpasswd.rb"
          acl {
          use Htpasswd.new("/home/hogehoge/.htpasswd", "realm-name")
          }
        file.dir: /home/hogehoge/public_html/kisho
        file.custom-handler:
          extension: .cgi
          fastcgi.spawn:
            command: "exec /usr/sbin/fcgiwrap"
      /server-status:
        status: ON

あとはちゃんと443ポートを開けておくことを忘れずに

証明書の有効期限は90日なのでcrontabで月1回更新を登録

#毎月3日の2時1分に証明書を更新する
01 02 03 * * /usr/bin/certbot renew && /bin/systemctl restart h2o
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1