macでpipからdjango・gunicornをインストールしてWSGIアプリケーション実行する

macのローカル環境にdjangoを入れて実行する。
gunicornからdjangoをWSGIアプリケーションとして実行する。
(実行日付：2017/03/18)

Gunicornとは
[gunicorn.org](http://gunicorn.org/)より参照

```
Gunicorn 'Green Unicorn' is a Python WSGI HTTP Server for UNIX. 
It's a pre-fork worker model. 
The Gunicorn server is broadly compatible with 
various web frameworks, simply implemented, 
light on server resources, and fairly speedy.
```

## django
### pip最新化
コマンド：
pip install --upgrade pip
(省略)

### djangoインストール
コマンド：
sudo pip install django

```
USER-no-MacBook-Air:~ user$ sudo pip install django
Password:
The directory '/Users/user/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/user/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting django
  Downloading Django-1.10.6-py2.py3-none-any.whl (6.8MB)
    100% |████████████████████████████████| 6.8MB 61kB/s 
Installing collected packages: django
Successfully installed django-1.10.6
USER-no-MacBook-Air:~ user$ 
```

### プロジェクト作成
コマンド：
django-admin.py startproject [プロジェクト名称]

コマンド例：
django-admin.py startproject django_test

```
USER-no-MacBook-Air:django user$ django-admin.py startproject django_test
USER-no-MacBook-Air:django user$ ls -ltra
total 0
drwxr-xr-x  3 user  staff  102  3 18 21:18 ..
drwxr-xr-x  4 user  staff  136  3 18 21:19 django_test
drwxr-xr-x  3 user  staff  102  3 18 21:19 .
(プロジェクトディレクトリが作成される)
USER-no-MacBook-Air:django user$ cd django_test
USER-no-MacBook-Air:django_test user$ ls -ltra
total 8
-rwxr-xr-x  1 user  staff  809  3 18 21:19 manage.py
drwxr-xr-x  6 user  staff  204  3 18 21:19 django_test
drwxr-xr-x  3 user  staff  102  3 18 21:19 ..
drwxr-xr-x  4 user  staff  136  3 18 21:19 .
(プロジェクトディレクトリの中身)
USER-no-MacBook-Air:django_test user$
```

### 実行
コマンド：
python manage.py runserver

```
USER-no-MacBook-Air:django_test user$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

March 18, 2017 - 12:21:57
Django version 1.10.6, using settings 'django_test.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
```

webブラウザ表示結果
![image](https://qiita-image-store.s3.amazonaws.com/0/140346/f913e546-23ee-f87d-587d-2cc7a2a076a8.png)


webブラウザ表示後のターミナル表示内容

```
[18/Mar/2017 12:22:16] "GET / HTTP/1.1" 200 1767
```

## gunicorn
### gunicornインストール
コマンド：
sudo pip install gunicorn

```
USER-no-MacBook-Air:~ user$ sudo pip install gunicorn
Password:
The directory '/Users/user/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/user/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting gunicorn
  Downloading gunicorn-19.7.0-py2.py3-none-any.whl (112kB)
    100% |████████████████████████████████| 112kB 467kB/s 
Installing collected packages: gunicorn
Successfully installed gunicorn-19.7.0
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
USER-no-MacBook-Air:~ user$ 
```

### gunicorn実行
コマンド：
gunicorn [プロジェクト名称].wsgi:application
コマンド例：
gunicorn django_test.wsgi:application

```
USER-no-MacBook-Air:django_test user$ gunicorn django_test.wsgi:application
[2017-03-18 22:39:00 +0900] [2365] [INFO] Starting gunicorn 19.7.0
[2017-03-18 22:39:00 +0900] [2365] [INFO] Listening at: http://127.0.0.1:8000 (2365)
[2017-03-18 22:39:00 +0900] [2365] [INFO] Using worker: sync
[2017-03-18 22:39:00 +0900] [2368] [INFO] Booting worker with pid: 2368
```

webブラウザ表示結果
![image](https://qiita-image-store.s3.amazonaws.com/0/140346/69124a67-8214-03d7-9af4-843056888699.png)
(djangoから実行した時と変化は無いけど、コンソールを落とすと表示出来なくなるのでgunicornとして実行出来ているという事で、、、)

参照サイト：
[MacにDjangoをインストール](http://qiita.com/shisho/items/a01f68c2da87ec0b921d)
[Gunicorn で Django を使う方法](http://docs.djangoproject.jp/en/latest/howto/deployment/wsgi/gunicorn.html)