阿里云Ubuntu上通过nginx+uwsgi部署Django

来源:互联网 发布:中级会计职称题库软件 编辑:程序博客网 时间:2024/05/21 06:48

1.安装Nginx

sudo apt-get install nginx  #安装

在Nginx的安装目录下找到default配置文件进行设置:

这里写图片描述

server {        listen 9000 default_server;        listen [::]:9000 default_server;        root /var/www/html;        # Add index.php to the list if you are using PHP        index index.html index.htm index.nginx-debian.html;        server_name _;        location / {                include uwsgi_params;                uwsgi_pass 127.0.0.1:9006;        }        location /static {                root / ;        }}
参数 说明 listen 9000 监听的端口(未来访问的端口,可以自己修改) root 网页路径 server_name 服务名 location / 管理uwsgi的参数 location /static 静态文件的目录,例如自己网页的 js image html

其中 uwsgi_pass 127.0.0.1:9006; 参数是未来与uwsgi通信的地址与端口,必须与后面项目总uwsgi的配置相同


启动Nginx:

/etc/init.d/nginx start  #启动/etc/init.d/nginx stop  #关闭/etc/init.d/nginx restart  #重启

2.安装uwsgi

pip install uwsgi

安装之后新建一个test.py文件进行测试

def application(env, start_response):    start_response('200 OK', [('Content-Type','text/html')])    return [b"Hello World"]

运行test.py文件

uwsgi --http-socket :9000 --plugin python --wsgi-file test.py
控制台 ctrl+c可退出运行

运行成功,看到hello world表示uwsgi安装成功

这里写图片描述


3.接下来开始部署自己的django项目

首先

修改自己项目中的wsgi.py文件,添加两行代码

import sys
sys.path.append("/var/www/django_app_server/app_server/")

其中sys.path.append为你自己项目在当前系统中路进,请自行修改

这里写图片描述


然后

在自己项目manage.py同级目录上添加uwsgi配置文件,本例取名为app_server_uwsgi.ini,可以自行取名

可以使用linux创建文件的命令

touch app_server_uwsgi.ini

这里写图片描述


具体配置

# app_server_uwsgi.ini file[uwsgi]# Django-related settingssocket = 127.0.0.1:9006# the base directory (full path)chdir           = /var/www/django_app_server/app_server# Django s wsgi filemodule          = app_server.wsgi# the virtualenv (full path)home            = /root/SmartCommunity/SmartCommunity-ENV/vir1# process-related settings# mastermaster          = true# maximum number of worker processesprocesses       = 4# ... with appropriate permissions - may be needed# chmod-socket    = 664# clear environment on exitvacuum          = trueplugins=python
参数 说明 socket 与nginx通信的地址和端口 chdir 自己项目所在的系统路径 module wsgi文件所在的相对于项目的路径 home 运行django项目的虚拟环境路径 processes 进程数

其中 socket 要与nginx中配置的uwsgi_pass参数相同,nginx需要通过该地址将一些动态请求交给uwsgi去处理。

4.运行

在自己django项目的app_server_uwsgi.ini配置文件下运行uwsgi

uwsgi --ini app_server_uwsgi.ini

这里写图片描述

运行nginx

sudo service nginx start

现在就可以访问到我们部署上去的项目了

这里写图片描述

阅读全文
1 0