在Mac上使用pip安装Django和Gunicorn,并执行WSGI应用程序

使用Gunicorn将Django作为WSGI应用程序在Mac本地环境上运行。
(运行日期:2017/03/18)

Gunicorn 是指 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 –> Django

最新版的pip

命令:
升级 pip:
pip install –upgrade pip
(省略)

安装django

命令:
使用sudo命令安装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.
image

在网页浏览器显示后的终端显示内容

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

独角兽

安装gunicorn。

以下是自然中文的句子:
命令:
sudo pip 安装 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
image

在Mac上安装Django的参考网站:
使用Gunicorn运行Django的方法

广告
将在 10 秒后关闭
bannerAds