Linux python+django+uwsgi+nginxのインストールと導入の概要


1.pythonとdjangoの環境構築
(1)anaconda 3をダウンロードしてインストールする
        wget https://3230d63b5fc54e62148e-c95ac804525aac4b6dba79b00b39d1d3.ssl.cf1.rackcdn.com/Anaconda3-2.3.0-Linux-x86_64.sh
        sh Anaconda3-2.3.0-Linux-x86_64.sh
Enterキーを押して、環境変数に追加するかどうかをプロンプトする場合はyesを入力します.
(2)djangoのインストール
直接pip install django
インストールに成功したら、プロジェクトを新規作成できます.
        django-admin startproject demosite
        cd demosite
        python manage.py startapp blog
        python manage.py migrate(このコマンドを実行するには、djangoに実行可能なappを生成させます.そうしないと、後でuwsgiを使用するとエラーが発生します)
(3)djangoの実行
        python manage.py runserver
curl 127.0.0.1:8000で正常にアクセスできれば、djangoのインストールに成功したことを示します.
2.uwsgiのインストール
        (1)centOS
        yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
        pip install uwsgi
uwsgi--version#uwsgiバージョンの表示
        (2)test.py
そして、Run uWSDI:
        uwsgi --http :8000 --wsgi-file test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return ["Hello World"] # python2
    #return [b"Hello World"] # python3

(3)ubuntuでエラーが発生する可能性があります.
エラーが発生したら、!!!no internal routing support, rebuild with pcre support !!!
            sudo apt-get install libpcre3 libpcre3-dev
            sudo pip uninstall uwsgi
            sudo apt-get remove uwsgi
            sudo pip install uwsgi
(4)テスト
1)次のurlを開くと、ブラウザにhello worldが表示されるはずです
                curl http://127.0.0.1:8000httpモジュールをインストールする場合はhttpを使用しますhttp://127.0.0.1:8000
Hello Worldが正しく表示されている場合は、上記のコーナーがスムーズであることを示します.
2)djangoのテスト
デフォルトでdjangoを使用した新規プロジェクトはappの下にwsgiを生成します.pyのファイル
                uwsgi --http :8000 --wsgi-file wsgi.pyそのままでもエラーが発生します
                uwsgi --http :8000 --wsgi-file appname/wsgi.py
ブラウザ入力を開くhttp://127.0.0.1:8000現実がweb client<-->uwsgi<--->djangoがスムーズであることを正しく説明すれば
             
3.インストール構成nginx
(1)取付
            wget http://nginx.org/download/nginx-1.9.5.tar.gz
            tar xf nginx-1.9.5.tar.gz
            cd nginx-1.9.5
            ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module
            make && make install
または参照
            http://cantgis.blog.51cto.com/5788192/1540004
(2)プロファイル
            vi /usr/local/nginx/conf/nginx.conf
一般的にはサーバーを入れておけばOKです
参照構成は次のとおりです.
user root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    use   epoll;
    worker_connections  65535;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

	server {

        listen   8099;
        server_name 10.117.52.157;              ##     IP    
        access_log /tmp/cms/access.log;
        error_log /tmp/cms/error.log;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
         include        uwsgi_params;
         uwsgi_pass     127.0.0.1:8088;
         uwsgi_read_timeout 300;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page   500 502 503 504  /registration/500.html;
        #location = /registration/500.html {
        #    root   html;
        #}

        location /static/ {
            alias  /root/cms/cms/static/;
            index  index.html index.htm;
        }

        location /media/ {
            alias  /root/cms/cms/media/;
        }
    }

}

(3)運転and再起動
            /usr/local/nginx/sbin/nginx
起動:nginx start
再起動:nginx-s reload
4.uwsgiのプロファイルを使用してdjangoを実行
nginxが実行されていることを確認したら、uwsgiでdjangoを実行できます.nginxは最外層で要求を受信し,静的な自己処理,動的にsocketポートをuwsgiに渡すことで処理する.
プロファイルの内容は次のとおりです.
    [uwsgi]
socket=:8088#nginx対応のIPとポート番号と一致する
chdir=/root/cms/cms#APPのディレクトリ
    module=cms.wsgi        #wsgi.pyファイルの場所
touch-reload=/root/cms/cms/reload#再起動コマンドtouch reloadファイルを入力すればよい
    processes=4
    threads=2
    daemonize=/tmp/cms/wsgi.log#ログファイルの場所
APPの上のレベルのディレクトリに置く
直接uwsgi--ini uwsgiを実行します.iniでいい