LoginSignup
0

More than 3 years have passed since last update.

NanoPIでDjangoをdeployするまで

Last updated at Posted at 2018-06-11

摘要

Django是一个用于创建Web应用程序的强大框架。 由于它具有内部http引擎,因此可以在此引擎上运行而不依赖于其他软件,直到调试终止,但是在发布工件时,您可以通过WSGI服务器使用Nginx或Apache 2作为前端 如果你调用Python,吞吐量和健壮性是不均衡的。 以下是在NanoPI Neo2上安装Python3.5,Django2.0,Nginx,uwsgi以及将Django工件部署到Armian(Debian stretch)的步骤。

はじめに

DjangoはWeb appをつくる強力なフレームワークです。内部にhttp engineを持っているのでdebug終了まで他のソフトに頼らず、このエンジンで実行することが可能ですが、成果物を公開するときは、NginxやApache2をフロントエンドにしWSGIサーバーを介してPythonを呼び出す方が、スループットも、堅牢性も段違いです。ここではNanoPI Neo2にArmbian (Debian stretch)に、Python3.5, Django2.0, Nginx, uwsgiをinstallしてDjangoの成果物をdeployする手順を示します。

Install

Django, Python, pipは普通に使えているものとして、話を省略します。
nginxは、

apt install nginx uwsgi uwsgi-plugin-python

でInstallしてください。

Django projectの制作

cd /home/www/
django-admin startproject dj
cd dj
vi dj/setting.py

ALLOWED_HOSTS = ['192.168.254.30']
のようにNanoPIのIP Addressを入れます。一般にはlocalhostからブラウザを接続しますが、displayを持たないNanoPIではほかのマシンから接続しますのでこの変更は必須となります。
念のため内蔵サーバーで確認します。

python manage.py runserver 0:8000

deployする

cd /etc/nginx/sites-available
vi django.conf

# the upstream component nginx needs to connect to
upstream django {
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name ['192.168.254.30']; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location /static {
        alias /home/www/dj/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/www/dj/uwsgi_params; # the uwsgi_params file you installed
    }
}

リンクします。

ln -s django.conf ../sites-enable

/home/www/dj/uwsgi_params を用意します。

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

uwsgiの起動スクリプトを書きます。
本当は /etc/uwsgi/apps-arivaleにパラメーターを設定するだけでよいのですが、うまくいかなかったので、/etc/init.d/djangoを書いてしまいました。

#!/bin/sh
### BEGIN INIT INFO
# Provides:          uwsgi
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: uwsgi
### END INIT INFO

case $1 in
        start)
                (cd /home/www/dj;
                uwsgi --master --socket :8001 --module dj.wsgi \
                  --uid www-data --gid www-data \
                  --processes 2 \
                  --pidfile /var/run/uwsgi.pid \
                  --daemonize /var/log/nginx/uwsgi.log
                )
                ;;
        stop)
                kill -9 `cat /var/run/uwsgi.pid`;
                ;;
        restart|reload)
                $0 stop;
                $0 start;
                ;;
esac
exit 0;

自動起動するようセットします。

update-rc.d django defaults

Serverを再起動します。

systemctl restart django nginx

http://192.168.254.30:8000
にアクセスできれば成功です。

終わりに

DjangoをDeplyする情報は意外なほど少ないようです。あってもapache2だったり、nginxでもuwsgiを立ち上げていながら、daemon化してなかったりで、組込に使えないものが多く苦労しました。という私も実はLighttpdでやりたかったのですが…

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