Nginx服务器部署Django应用

Nginx服务器部署Django应用

之前做了一个关于Apache服务器上的部署Django的应用,最近发现Nginx是个很方便的服务器软件,于是就花了点时间来部署这个
http://write.blog.csdn.net/mdeditor[Setting up Django and your web server with uWSGI and nginx]这个链接是官方发布的文档,参考这个文档基本是稳稳地把这个Django应用部署上去。

注意!

官方的文档确实很详细了,但是这里要明白通过浏览器最终访问到网站的数据的流程:client <--> nginx <-->socket<--> wsgi <--> Django app 文档部署案例中因为相连接环节多,是选择了其中几个环节连通,最后将整个连接打通。

部署环境

  • ubuntu 16.04.3
  • django 2.0
  • nginx 1.10.3

Django App

安装Django

安装虚拟环境工具包:
pip install virtualenv

创建虚拟环境env35
virtualenv --no-site-packages --python=3.5 env35
virtualenv创建一个独立的Python运行环境,加上参数–no-site-packages,使得已经安装到系统Python环境中的所有第三方包都不会复制过来,这样,就得到了一个不带任何第三方包的“干净”的Python运行环境。

激活虚拟环境
source env35/bin/activate# 激活环境,进入env35虚拟环境

安装Django工具包
pip3 install django# 安装Django

安装mezzanine工具包
pip3 install mezzanine# 这个是开源 CMS Web 框架```

创建项目:
mezzanine-project blogsite

创建数据库:
python manage.py createdb

收集静态文件:
python manage.py collectstatic

uwsgi协议模块

安装uwsgi模块:
pip install uwsgi

运行Django应用:
uwsgi --http :8001 --module blogsite.wsgi#测试 遇到Invalid HTTP_HOST header: ‘192.168.0.100:8001’. You may need to add ‘192.168.0.100’ to ALLOWED_HOSTS.
A:这个需要在blogsite/setting.py 相应参数中添加这个ip地址。

通过浏览器访问到Django App下ip+端口访问成功,说明检查client <-->wsgi <--> Django app是连通的。这个在官方文档中看到这类似示图。

配置相应的文件

在项目文件下 创建 nginx.conf ,uwsgi.ini , uwsgi_params三个文件

Nginx

安装nginx
sudo apt install nginx

在blogsite项目文件下创建nginx.conf服务器配置文件
vim nginx.conf

编写内容如下

#mysite_nginx.conf
# the upstream component nginx needs to connect to
	upstream django {
	    server unix:///home/zack/blogsite/blogsite.sock; # for a file socket 即UNIX Socket通信机制
	   # server 127.0.0.1:8001; # for a web port socket (we'll use this first) 即TCP/IP Soket通信机制
	}
	
	# configuration of the server
	server {
	    # the port your site will be served on
	    listen      8002;
	    # the domain name it will serve for
	    server_name 192.168.0.100; # 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 /path/to/your/mysite/media;  # your Django project's media files - amend as required
	   # }
	
	    location /static {
	        alias /home/zack/blogsite/static; # your Django project's static files - amend as required
	    }
	
	    # Finally, send all non-media requests to the Django server.
	    location / {
	        uwsgi_pass  django;  # 找到upstream模块 django
	        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
	    }
	}

这个nginx.conf 需要软链接到 ln -s /home/zack/blogsite/nginx.conf /etc/nginx/sites-enabled/ 使nginx能够使用到这个项目文件下的nginx.conf配置文件。

本次案例中使用的是UNIX Socket通信机制。UNIX Socket是同一台服务器上不同进程间的通信机制;TCP/IP Socket是网络上不同服务器之间进程的通信机制,当然也可以让同一台服务器的不同进程通信。

重新启动nginx服务器
service nginx restart

uwsgi.ini

创建uwsgi.ini文件:
vim uwsgi.ini

配置内容如下:

[uwsgi]
	# Django-related settings
	# the base directory (full path)
	chdir           = /home/zack/blogsite
	# Django's wsgi file
	module          = blogsite.wsgi
	# the virtualenv (full path) 这里的值是虚拟环境env35的路径
	home            = /home/zack/env35   
	
	# process-related settings
	# master
	master          = true
	# maximum number of worker processes
	processes       = 10
	# the socket (use the full path to be safe
	socket          = /home/zack/blogsite/blogsite.sock
	# ... with appropriate permissions - may be needed
	chmod-socket    = 664
	# clear environment on exit
	vacuum          = true
	

这里socket 参数和nginx.conf文件中的
server unix:///home/zack/blogsite/blogsite.sock;相对应匹配这样可以实现nginx<-->socket<-->wsgi 连通。

uwsgi_params

将/etc/nginx/uwsgi_params路径下的这个文件拷贝到项目文件夹下
cp /etc/nginx/uwsgi_params#将uwsgi_params文件拷贝到当前项目文件下。

退出虚拟环境外安装uwsgi

敲入deactivate命令 退出虚拟环境env35
pip install uwsgi

设置开机启动uwsgi

在/etc/rc.local 添加/usr/local/bin/uwsgi --ini /home/zack/blogsite/uwsgi.ini
设置开机启动uwsgi.ini

这样可以通过浏览器访问了!

note!
设置开机启动uwsgi,服务器一直卡在启动uwsgi的界面,不能看到登录界面。有时候重新启动Linux会卡死,这个事情让我体会到生产环境中后台运行的服务器不能随便重启,否则会出现意想不到的问题。这也是作为程序员的苦逼之处……

发布了30 篇原创文章 · 获赞 5 · 访问量 7674

猜你喜欢

转载自blog.csdn.net/c0586/article/details/78905880
今日推荐