CTFdをnginxでデプロイする
部のサーバーでCTFdのデプロイにハマったのでその時のメモ
環境
$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_CODENAME=xenial DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS" $ python -V Python 2.7.12 $ nginx -V nginx version: nginx/1.13.7 built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5) built with OpenSSL 1.0.2g 1 Mar 2016 TLS SNI support enabled
すでにhttps://jyoken.net
にはWordPressが立ち上がってる
ここにサブドメインとしてhttps://ctf.jyoken.net
にCTFd
をインストールする
ちゃんとDNSのAレコードにctf
とサーバーのIPを登録しておく
nginxの設定
今、nginx.conf
は
$ cat /etc/nginx/nginx.conf
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http{ include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; keepalive_timeout 65; gzip on; include /etc/nginx/conf.d/*.conf; }
こんな感じになってる
Cloudflare
のSSL
を利用しているのでサーバー側の設定ではhttp
のみになっているが気にしなくていい
注意するのは以下の2点
user
はnginx
conf.d/*.conf
をインクルードしている
conf.d
以下は、
$ ls /etc/nginx/conf.d/ default.conf-disabled wordpress.conf
こんな感じでデフォルトの設定を無効にして(名前を変えて)、wordpress.conf
が有効になっている
ここにctf.conf
を追加していく
$ sudo vim /etc/nginx/conf.d/ctf.conf
server { listen 80; server_name ctf.jyoken.net; access_log /var/log/nginx/ctf_jyoken_access.log; error_log /var/log/nginx/ctf_jyoken_error.log; location / { include uwsgi_params; uwsgi_pass unix:///tmp/uwsgi.sock; } }
こんな感じ
CTFdの設定
$ cd /var/www $ sudo git clone https://github.com/CTFd/CTFd.git $ sudo chown -R nginx:nginx CTFd/ $ cd CTFd/ $ sudo su #←これ重要!(rootユーザーになってないとflaskのインポートでつまづく) (root) $ ./prepare.sh (root) $ exit $ sudo vim ctfd.ini
# UWSGI Configuration File # Install uwsgi (sudo apt-get install uwsgi), copy this file to # /etc/uwsgi/apps-available and then link it in /etc/uwsgi/apps-enabled # Only two lines below (commented) need to be changed for your config. # Then, you can use something like the following in your nginx config: # # # SERVER_ROOT is not / (e.g. /ctf) # location = /ctf { rewrite ^ /ctf/; } # location /ctf { # include uwsgi_params; # uwsgi_pass unix:/run/uwsgi/app/ctfd/socket; # } # # # SERVER_ROOT is / # location / { # include uwsgi_params; # wsgi_pass unix:/run/uwsgi/app/ctfd/socket; # } [uwsgi] # Where you've put CTFD chdir = /var/www/CTFd/ # If SCRIPT_ROOT is not / #mount = /ctf=wsgi.py # SCRIPT_ROOT is / mount = /=wsgi.py # You shouldn't need to change anything past here plugin = python module = wsgi master = true processes = 1 threads = 1 vacuum = true manage-script-name = true wsgi-file = wsgi.py callable = app die-on-term = true # If you're not on debian/ubuntu, replace with uid/gid of web user uid = nginx gid = nginx # add Socket socket = /tmp/uwsgi.sock
chdir = /var/www/ctfd/
のctfd
をCTFd
に変更uid
,gid
をnginx
に変更socket = /tmp/uwsgi.sock
を追加
こんな感じに編集する
uwsgiをインストールする
$ sudo apt install uwsgi uwsgi-plugin-python
uwsgiでテストしてみる
$ cd /var/www/CTFd/ $ sudo -u nginx uwsgi --ini ctfd.ini
ここでhttps://ctf.jyoken.net
にアクセスしてCTFdが表示されたらOK
確認し終わったらctr+c
で終了しておく
サービスに登録する
Unit
定義ファイルを作る
$ sudo vim /etc/systemd/system/ctfd.service
[Unit] Description=CTFd After=network.target [Service] User=nginx Group=nginx WorkingDirectory=/var/www/CTFd ExecStart=/usr/bin/uwsgi --ini ctfd.ini [Install] WantedBy=multi-user.target
Unit
がService
として認識されたか確認する
$ sudo systemctl list-unit-files --type=service | grep ctfd ctfd.service disabled
こうなればOK
自動起動を有効化する
$ sudo systemctl enable ctfd
起動する
$ sudo systemctl start ctfd
エラーが出ずに実行できれば成功
再起動してからhttps://ctf.jyoken.net
にアクセスしてCTFdが表示されたら完成!