Skip to content

Instantly share code, notes, and snippets.

@fortune
Last active December 7, 2018 09:14
Show Gist options
  • Save fortune/219c85856e4bc86480283f9ba23737ba to your computer and use it in GitHub Desktop.
Save fortune/219c85856e4bc86480283f9ba23737ba to your computer and use it in GitHub Desktop.
uwsgi を使って簡易 HTTP および HTTPS サーバを立てる

uwsgi を使って簡易 HTTP および HTTPS サーバを立てる

Ubuntu Server 18.04 で試す。

uwsgi のインストール

$ sudo pip3 install uwsgi

SSL ライブラリとリンクしているかどうかを確認する。

$ type uwsgi
uwsgi is /usr/local/bin/uwsgi

$ ldd /usr/local/bin/uwsgi
	linux-vdso.so.1 (0x00007ffd74d8e000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f06c8b11000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f06c8773000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f06c856f000)
	libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f06c833d000)
	libpython3.6m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0 (0x00007f06c7c89000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f06c7898000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f06c908b000)
	libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f06c767b000)
	libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f06c7478000)

これを見ると、SSL ライブラリにリンクしていないので、HTTPS で uwsgi を使うことができない。そこで、libssl-dev をインストールして uwsgi を入れ直す。

$ sudo apt-get install libssl-dev
$ sudo pip3 uninstall uwsgi
$ sudo pip3 install uwsgi
$ ldd /usr/local/bin/uwsgi

	linux-vdso.so.1 (0x00007ffe58df8000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f31e66e8000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f31e634a000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f31e6146000)
	libssl.so.1.1 => /usr/lib/x86_64-linux-gnu/libssl.so.1.1 (0x00007f31e5edc000)
	libcrypto.so.1.1 => /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 (0x00007f31e5a64000)
	libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f31e5832000)
	libpython3.6m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0 (0x00007f31e517e000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f31e4d8d000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f31e6c75000)
	libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f31e4b70000)
	libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f31e496d000)

libssl.so と libcrypto.so にリンクしていることが確認できる。

SSL 証明書を取得する。

Letsencrypt で取得する。fortunefield.comwww.fortunefield.com の2つをカバーする証明書を取得することにするので、予め、ネームサーバの DNS 設定で fortunefield.com の A レコードにこのサーバの IP アドレスをセットし、www.fortunefield.com の CNAME レコードに fortunefield.com を セットしておく。

その上で、(必要なら)letsencrypt の証明書取得用のツールをインストールして、SSL 証明書を取得する。

$ sudo apt install certbot
$ sudo certbot certonly  --standalone -d fortunefield.com -d www.fortunefield.com
$ sudo su
# ls -l /etc/letsencrypt/live/fortunefield.com/
total 4
-rw-r--r-- 1 root root 543 Nov  1 05:41 README
lrwxrwxrwx 1 root root  40 Nov  1 05:41 cert.pem -> ../../archive/fortunefield.com/cert1.pem
lrwxrwxrwx 1 root root  41 Nov  1 05:41 chain.pem -> ../../archive/fortunefield.com/chain1.pem
lrwxrwxrwx 1 root root  45 Nov  1 05:41 fullchain.pem -> ../../archive/fortunefield.com/fullchain1.pem
lrwxrwxrwx 1 root root  43 Nov  1 05:41 privkey.pem -> ../../archive/fortunefield.com/privkey1.pem

uwsgi を起動する

まず、静的ファイルを用意しておく。

$ mkdir html
$ echo hello >html/index.txt

HTTP と HTTPS の両方を受け付けるように uwsgi を起動する。

$ sudo su
# uwsgi --http 0.0.0.0:80 --https 0.0.0.0:443,/etc/letsencrypt/live/fortunefield.com/fullchain.pem,/etc/letsencrypt/live/fortunefield.com/privkey.pem --static-map /static=$PWD/html/ --master --processes 5

これで、

http://fortunefield.com/static/index.txt

http://www.fortunefield.com/static/index.txt

https://fortunefield.com/static/index.txt

https://www.fortunefield.com/static/index.txt

にアクセスできる。--processes オプションで worker プロセスの数を指定している。ここでは 5 を指定したので、最大5つのリクエストを同時処理できるということになる。--http-workers のような似たオプションもあるのだが、これだと並列にならなかった。また、--enable-threads も並列処理には役に立たなかった。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment