nginx+Uwsgi+django搭建实践

  1. 部署django

    写一个最简单的django应用

    url.py
    from django.conf.urls import include, url
    from django.contrib import admin
    from shouye.views import *
    urlpatterns = [
    url(r’^admin/’, include(admin.site.urls)),
    url(r’^’, include(‘shouye.urls’, namespace=‘shouye’))
    ]

    shouye/urls.py:
    from django.conf.urls import url
    from . import views
    urlpatterns = [url(r’^$’, views.index), ]

    shouye/views.py:
    from django.http import *
    import json
    from django.shortcuts import render
    def index(request):
    #return render(request, ‘shouye/index.html’, context)
    return render(request,‘shouye/index.html’)

    确认manage.py能启动runserver即可
    nginx+Uwsgi+django搭建实践

2. 部署uwsgi服务器

首先,uwsgi部署安装相当easy,直接pip install uwsgi即可。
然后,测试通过uwsgi命令启动django框架:
uwsgi --http :8080 --module feekee.wsgi,显示如下即为成功
nginx+Uwsgi+django搭建实践 最后,配置uwsgi.ini文件,通过ini文件拉起django服务。
在项目同级目录(manage.py上一层目录),新建uwsgi目录,并创建uwsgi.ini文件:

# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/data/feekee
# 指定项目的application
module=feekee.wsgi:application
# 指定sock的文件路径
socket=/data/uwsgi/uwsgi.sock
# 进程个数
workers=5
pidfile=/data/uwsgi/uwsgi.pid
# 指定IP端口
http=:9999
# 指定静态文件
static-map=/static=/data/feekee/static
# 启动uwsgi的用户名和用户组
uid=ubuntu
gid=ubuntu
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/data/uwsgi/uwsgi.log

配置完成后启动即可
nginx+Uwsgi+django搭建实践

3. 部署nginx服务器
nginx的安装也相当简单,ubuntu直接apt-get安装。apt-get install nginx.
nginx配置:
cd /etc/nginx/conf.d/
新建项目conf文件:vi feekee.conf

server{
listen 80;
server_name 193.112.214.218 ; # 你访问的路径前面的url名称
access_log /var/log/nginx/access.log; # Nginx日志配置
charset utf-8; # Nginx编码
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; # 支持压>缩的类型

error_page 404 /404.html; # 错误页面
error_page 500 502 503 504 /50x.html; # 错误页面

# 指定项目路径uwsgi
location / { # 这个location就和咱们Django的url(r'^admin/', admin.site.urls),
include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
uwsgi_pass unix:/data/uwsgi/uwsgi.sock; # 指定uwsgi的sock文件所有动态请求就会直接丢给他
}

# 指定静态文件路径
location /static/ {
alias /data/feekee/template/;
index index.html index.htm;
}
}

配置完成,启动即可
nginx+Uwsgi+django搭建实践

nginx+Uwsgi+django搭建就大功告成啦!

相关文章:

  • 2021-11-27
  • 2018-05-25
  • 2018-09-29
  • 2021-10-27
  • 2021-07-23
  • 2021-12-23
  • 2021-11-27
  • 2021-05-26
猜你喜欢
  • 2021-07-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案