一旦你创建了一个 dJango 应用程序。只需按照以下步骤操作:
步骤 1.在你的 Django 项目目录中创建一个 uwsgi.ini 文件。即除了manage.py
[uwsgi]
# set the http port
http = :<port_no>
# change to django project directory
chdir = <project directory>
# add /var/www to the pythonpath, in this way we can use the project.app format
pythonpath = /var/www
# set the project settings name
env = DJANGO_SETTINGS_MODULE=<project_name>.settings
# load django
module = django.core.handlers.wsgi:WSGIHandler()
STEP 2.在/etc/nginx/sites-available添加 .conf 文件
server {
listen 84;
server_name example.com;
access_log /var/log/nginx/sample_project.access.log;
error_log /var/log/nginx/sample_project.error.log;
# https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-production
location /static/ { # STATIC_URL
alias /home/www/myhostname.com/static/; # STATIC_ROOT
expires 30d;
}
}
STEP 3.在 nginx.conf 中,将请求传递给您的 Django 应用程序
在 server { } 块下,
location /yourapp {
include uwsgi_params;
uwsgi_pass <your app address, eg.localhost>:<portno>;
}
步骤 4.运行 uwsgi.ini
> uwsgi --ini uwsgi.ini
现在对您的 nGINX 的任何请求都会通过 uwsgi 将请求传递给您的 Django 应用程序。享受 :)