Linux - 搭建Web项目(Django + nginx + uwsgi)

 

 


  工作中碰到需要使用Django + nginx + uwsgi 搭建项目环境

1. 搭建基本环境

    • 需要有python环境,不多做说明
    • 需要安装nginx,不多做说明
    • 需要安装uwsgi:

      yum install libxml2 gcc python-devel  # 安装uswgi之前需要安装基本环境

      pip install uwsgi

 

2. 准备一个可以“ python manage.py runserver ” 运行的django项目

  

  

 

  3. 用uwsgi 测试项目

    a. 利用命令执行

      

      命令测试启动:  uwsgi --http 192.168.31.123:8080 --file teacher/wsgi.py --static-map=/static=static

参数说明:

--http 这个就和runserver一样指定IP 端口
--file 这个文件就里有一个反射,如果你在调用他的时候没有指定Web Server就使用默认的
-- static 做一个映射,指定静态文件

 

     b. 利用uwsgi配置文件启动django项目

     第一步:在django项目同级目录创建uwsgi目录,用于存放配置脚本等等

       

    第二步:进入/uwsgi目录,创建一个uwsgi.ini文件

        

      编辑uwsgi.ini文件内容如下:

 

=========================================================

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

============================================================

       启动项目:

        

 4. 配置nginx.conf 文件

===================================

 

server { # 这个server标识我要配置了
listen 80; # 我要监听那个端口
server_name 192.168.2.108 ; # 你访问的路径前面的url名称 
access_log /var/log/nginx/access.log main; # 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:/opt/proj/script/uwsgi.sock; # 指定uwsgi的sock文件所有动态请求就会直接丢给他
}

 

# 指定静态文件路径
location /static/ {
alias /opt/proj/teacher/static/;
index index.html index.htm;
}

 

}

===============================================================

     重启nginx,完成

 

猜你喜欢

转载自www.cnblogs.com/blitheG/p/9829731.html
今日推荐