使用uwsgi+nginx运行django程序

本文档详细介绍了如何在Ubuntu 14.04环境下,使用uwsgi和nginx部署Django应用程序。首先,介绍了环境配置,包括Ubuntu、Python 2.7和Django的版本。接着,讲解了安装uwsgi、Nginx和Django的过程。然后,通过修改Django项目的配置文件,并使用uwsgi启动项目,验证了uwsgi的功能。最后,配置了Nginx,创建了uwsgi_params和django_nginx.conf文件,并启动Nginx和uwsgi,确保了通过http://192.168.116.131:8000/polls/可以访问到系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境

  1. Ubuntu:14.04
  2. Python:2.7

部署代码

  1. 代码说明:样例代码参考Django官方例子 https://docs.djangoproject.com/en/1.10/intro/tutorial01/
  2. 代码位置:https://github.com/Eric-aihua/django_sample

安装软件

uwsgi

pip install uwsgi

Nginx

apt-get install nginx

Django

pip install django

验证uwsgi

初始化project

假设github上的代码已经下载到/root/django_sample 目录,执行以下命令

root@ubuntu:~/django_sample# python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying polls.0001_initial... OK
  Applying polls.0002_user... OK
  Applying polls.0003_user_zipcode... OK
  Applying sessions.0001_initial... OK

## 修改project 配置文件
django_sample/settings.py 文件的下列配置,需要修改为本机的IP

ALLOWED_HOSTS = ['192.168.116.131','127.0.0.1','localhost']

使用uwsgi启动django的project

 root@ubuntu:~/django_sample# uwsgi --http :8000 --module django_sample.wsgi

到目前为止,已经成功使用uwsgi启动了django工程,通过在浏览器中输入http://192.168.116.131:8000/polls/ 可以开始访问系统

# 配置Nginx
# 添加配置文件
在/root/django_sample 下创建两个文件
1. uwsgi_params:内容来自https://github.com/nginx/nginx/blob/master/conf/uwsgi_params

  1. django_nginx.conf 内容如下
 # mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /root/django_sample/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /root/django_sample/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     include     /root/django_sample/uwsgi_params; # the uwsgi_params file you installed
    }
}
  1. 让Nginx加载project的nginx配置文件
root@ubuntu:~/django_sample# ln -s /root/django_sample/django_nginx.conf /etc/nginx/sites-enabled/
  1. 启动Nginx
/etc/init.d/nginx start
  1. 启动uwsgi
root@ubuntu:~/django_sample# uwsgi --socket :8001 --module django_sample.wsgi

此处的端口配置与mysite_nginx.conf 中upstream的配置相同

上述启动方式输入参数比较多,也可以将启动配置添加到ini文件中,创建django_sample_uwsgi.ini,内容如下:

[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /root/django_sample
# Django's wsgi file
module          = django_sample.wsgi
# the virtualenv (full path)
#home            = /path/to/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 1
# the socket (use the full path to be safe
socket          = :8001
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

启动方式为:root@ubuntu:~/django_sample# uwsgi –ini django_sample_uwsgi.ini

到目前为止,数据访问链路已经基本畅通,请求链路为:the web client <-> the Nginx <-> the socket <-> uWSGI <-> Django

访问路径:http://192.168.116.131:8000/polls/

参考

http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值