• Ubuntuに関連したハード、ソフトの備忘録サイトです

GunicornでDjangoアプリケーションをデプロイ(暫定投稿)

技術評論社から出ている「Nginx ポケットリファレンス」が要領よくまとまっています。PHPがはやるのは、環境構築が極めて簡単という麺もあるかと思います。言語仕様としては、Pythonの方が面白いのですが、Djangoなどフレームワークで作成したアプリケーションのデプロイの仕方がよく分からない。本書は各アプリケーションのデプロイの仕方をそれなりに詳しく記述していて、参考になりました。

Nginxの/etc/sites-enabled/defaultの内容は下記です。

# You may add here your
# server {
#	...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
	listen 80;
	# listen [::]:80 default_server ipv6only=on;

	# Make site accessible from http://localhost/
    server_name nginxsvr.com;

	# root /usr/share/nginx/html;
	root /var/www/html;
	index index.html index.htm index.php;
	
	access_log  /var/log/nginx/access.log;
    error_log   /var/log/nginx/error.log;
    

	location / {
        # try_files $uri $uri/ /index.php?$uri&$args;
        if (!-e $request_filename) {
            rewrite ^(.+)$  /index.php?url=$1 last;
            break;
        }
	}

	#location / {
        #if ($request_filename = false) {
        #    rewrite ^$  /index.php last;
        #}
        #if (!-e $request_filename) {
        #    rewrite ^(.+)$  /index.php?url=$1 last;
        #    break;
        #}
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		# try_files $uri $uri/ =404;
		# Uncomment to enable naxsi on this location
		# include /etc/nginx/naxsi.rules
	#}

	# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
	#location /RequestDenied {
	#	proxy_pass http://127.0.0.1:8080;    
	#}

	#error_page 404 /404.html;

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

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	location ~ \.php$ {
	#	 # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
	#
	#	 # With php5-cgi alone:
		 # fastcgi_split_path_info ^(.+\.php)(/.+)$;
		 # fastcgi_pass   127.0.0.1:9000;
         # fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
         # fastcgi_param  PATH_INFO $fastcgi_script_name;
         # include fastcgi_params;

	#	# With php5-fpm:
		 fastcgi_split_path_info ^(.+\.php)(/.+)$;
		 fastcgi_pass unix:/var/run/php5-fpm.sock;
         fastcgi_index index.php;
         include fastcgi_params;
	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.ht {
		deny all;
	}
}

# for FuelPHP 1.73
server {
	listen 80;
	# listen [::]:80 default_server ipv6only=on;

	# Make site accessible from http://localhost/
    server_name fuelphp6.it-ishin.com;

	# root /usr/share/nginx/html;
	root /var/www/fuelphp/public;
	index index.html index.htm index.php;
	
	access_log  /var/log/nginx/fuelphp-access.log;
    error_log   /var/log/nginx/fuelphp-error.log;
    

	location / {
        # try_files $uri $uri/ /index.php?$uri&$args;
        if (!-e $request_filename) {
            rewrite ^(.+)$  /index.php?url=$1 last;
            break;
        }
	}

	#location / {
        #if ($request_filename = false) {
        #    rewrite ^$  /index.php last;
        #}
        #if (!-e $request_filename) {
        #    rewrite ^(.+)$  /index.php?url=$1 last;
        #    break;
        #}
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		# try_files $uri $uri/ =404;
		# Uncomment to enable naxsi on this location
		# include /etc/nginx/naxsi.rules
	#}

	# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
	#location /RequestDenied {
	#	proxy_pass http://127.0.0.1:8080;    
	#}

	#error_page 404 /404.html;

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

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	location ~ \.php$ {
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
	#	# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
	#
	#	# With php5-cgi alone:
		# fastcgi_pass 127.0.0.1:9000;
	#	# With php5-fpm:
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		include /etc/nginx/fastcgi_params;
	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.ht {
		deny all;
	}
}

upstream backend {
    # server  127.0.0.1:8000;
    server  unix:/var/run/django01.sock;
}

server {
    listen 80;
    server_name bootcamp.com;
    
    proxy_set_header Host               $host;
    proxy_set_header X-Real-IP          $remote_addr;
    proxy_set_header X-Forwarded-Host   $host;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto  $scheme;
    
    access_log  /var/log/nginx/gunicorn-access.log;
    error_log   /var/log/nginx/gunicorn-error.log;

    location / {
        proxy_pass http://backend;
    }
    
    location /static/(.*)  {
        alias /home/michiaki/Django/bootcamp/staticfiles/$1;
	}

}

upstream backend2  {
    # server  127.0.0.1:8000;
    server  unix:/var/run/django02.sock;
}

server {
    listen 80;
    server_name mysite.com;
    
    proxy_set_header Host               $host;
    proxy_set_header X-Real-IP          $remote_addr;
    proxy_set_header X-Forwarded-Host   $host;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto  $scheme;
    
    access_log  /var/log/nginx/django-access.log;
    error_log   /var/log/nginx/django-error.log;

    location / {
        # proxy_pass http://localhost:8000;
        proxy_pass http://backend2;
    }

    #location /(.*) {
    #    proxy_pass http://localhost:8000/$1;
    #}
    
    location /static/  {
        alias /home/michiaki/Django/mysite/static/;
	}

}

server {
    listen 80;
    server_name mysite2.com;
    
    proxy_set_header Host               $host;
    proxy_set_header X-Real-IP          $remote_addr;
    proxy_set_header X-Forwarded-Host   $host;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto  $scheme;
    
    access_log  /var/log/nginx/django-access.log;
    error_log   /var/log/nginx/django-error.log;

    location / {
        # proxy_pass http://127.0.0.1:8000;
        proxy_pass http://backend2;
    }

    #location /(.*) {
    #    proxy_pass http://localhost:8000/$1;
    #}
    
    location /static/  {
        alias /home/michiaki/Django/mysite2/static/;
	}

}

これは、Unixドメインソケットを使ってNginxとDjangoアプリケーションを接続する設定例です。この設定のもとで、Nginxを動かせておいて、

sudo gunicorn --daemon --bind unix:/var/run/django01.sock bootcamp.wsgi:application

と打ち込むと、次のようにDjango製のSNSアプリケーションbootcampが立ち上がります。bootcampは、次のサイトに従ってインストールしておきます。https://github.com/vitorfs/bootcamp。このページにインストールガイドがあります。なお、Python2系列用のgunicorn, Python3系列用のgunicorn3はUbuntuでapt-get install gunicorn/gunicorn3でインストールできます。

[追記]ただし、python-dev, python-pipのインストールが必要です。また、pip install requirements.txtで必要なソフトがインストールできますが、デフォルトのデータベースサーバーとして、Postgresql Serverを使っているので、最後の行はコメントアウトする必要がありました。そうすると、デフォルトのデータベースとしてがsqlite3になるので、php5-sqliteもインストールする必要があります。

そのうえで、python manage.py syncdbで/tmp/db.sqlite3が作成されます。これをbootcampデイレクトリィにコピーして、settings.pyでデータベースとしてこのdb.sqlite3を登録する必要がありました。

対応または推奨のDjangoのバージョンは1.6.5なのでうまく行かなければ、インストール時にホームディレクトリの.localにダウンロードされるDjangoとgunicornを使ってください。sudo ~/.local/bin/gunicorn となります

gunicorn01

gunicorn02

これは、Ubuntu15.10にVirtuablBoxでUbuntu16.04LTSのalpha2をインストールして動かしたものです。URLで8000番ポートはありません。

最も簡単なお馴染みのPython3系列のDjangoインストール成功画面は次のようになります。

sudo gunicorn --daemon --bind unix:/var/run/django02.sock mysite2.wsgi:application

/var/run上にソケットを作成するので、sudoが必要になります。「ポケット・リファレンス」では一般ユーザーで起動していますが、これはCent OS 7の環境です。これで動くかどうかは分かりませんず、Ubuntuではそうは行きません。

gunicorn03

上図はpython3 manage.py runserverで立ち上げたものではありません。Nginx+Gunicorn2/3(アプリケーション・サーバー)です。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA