nginx+uwsgi+django是我们常用的django部署方式。nginx作为最前端的服务器,他负责接收所有的客户端请求,对于请求的静态文件,由nginx服务器自己完成,因为它具有很好处理静态文件的能力,性能进行过优化,支持高并发量;uWSGI服务器作为支持服务器,是用来服务nginx的,nginx将请求的动态文件交给uWSGI进行处理。uWSGI实现了uwsgi、wsgi和http协议,uwsgi协议是uWSGI自定义的协议,定义的是框架(django)和服务器对接的接口。
1、安装uwsgi
pip install uwsgi
2、创建配置文件
安装完成后,将我们windows中的项目文件blog通过winscp传递到linux中,我们这里直接放在root目录下。
然后在/root/myblog/目录下新建一个文件夹conf
mkdir conf
然后创建uwsgi的配置文件:
vim uwsgi.ini
写入
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /root/blog
# Django's wsgi file
module = blog.wsgi
# the virtualenv (full path)
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 5
# the socket (use the full path to be safe
socket = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
virtualenv = /root/.virtualenvs/blog_env_py3
3、对blog项目进行更改
我们在windows中是在开发环境下进行的,现在需要将其改成生产环境
setting.py
DEBUG = False
ALLOWED_HOSTS = ['*']
# STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
urls.py
from blog.settings import STATIC_ROOT
#添加静态文件的访问处理函数
url(r'^static/(?P<path>.*)/$', serve, {'document_root': STATIC_ROOT}),
更改haystack中的源码
进入Virtualenv\blog_env_py3\lib\python3.6\site-packages\haystack\utils\highlighting.py
添加上
if len(self.text_block) < self.max_length:
return self.text_block[:start_offset] + highlighted_chunk
4、配置nginx
在conf文件夹下新建nginx设置文件
vim uc_ngnix.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:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name 20.20.20.20 www.geerniya.cn ; # 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/blog/media/; # 指向django的media目录
}
location /static {
alias /root/blog/static/; # 指向django的static目录
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include uwsgi_params; # the uwsgi_params file you installed
}
}
server_name为你的公网ip地址和备案后的域名
将该配置文件加入到nginx的启动配置文件中
ln -s /root/myblog/conf/uc_nginx.conf /etc/nginx/conf.d/
运行命令, 搜集所有静态文件到static下,包括admin中的
python manage.py collectstatic
最后通过navicat将数据库导入到服务器中,配置即可完成。
——————————————————————————————————————————
项目的完整代码:django_blog
觉得有用的欢迎给个star。