最近使用django写了一些项目,不过部署到服务器上碰到一些问题,还有静态文件什么的一堆问题,这里总结一下碰到的问题和解决方案,整体思路是按照官方文档走的。html
原文地址:http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.htmlpython
讲的很清楚,不过仍是须要一些注意的地方nginx
对于uwsgi+nginx的部署方式,它的访问关系大概是:web
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
若是你须要使用virtualenv:django
virtualenv uwsgi-tutorial cd uwsgi-tutorial source bin/activate
安装django:ubuntu
pip install Django django-admin.py startproject mysite cd mysite
这里假设的你域名是:example.com,在后面的你能够换成你的域名或者IP地址。vim
原教程中使用的是8000端口号,咱们这直接使用80端口。bash
安装uwsgi服务器
pip install uwsgi
先作个测试uwsgi是否能正常使用,建立一个test.py文件(在哪创你本身决定啊,反正配置完要删的):微信
# test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2
下面咱们使用uwsgi:
uwsgi --http :8000 --wsgi-file test.py
参数解释:
http :8000:指示用的是8000端口
wsgi-file test.py:加载指定文件 test.py
而后你就能够尝试访问了:
http://example.com:8000
接下来咱们在django项目上尝试一下
新建的django项目须要先:
python manage.py migrate
python manage.py runserver
若是可以运行:
uwsgi --http :8000 --module mysite.wsgi
参数:
module mysite.wsgi :指定wsgi
尝试访问:
http://example.com:8000
如今的结构相似于:
the web client <-> uWSGI <-> Django
安装nginx
sudo apt-get install nginx sudo /etc/init.d/nginx start # start nginx
也能够用nginx服务命令好比
sudo service nginx start sudo service nginx stop sudo service nginx restart
如今访问http://127.0.0.1/就能看到默认的nginx主页
而后咱们来配置nginx文件,先对原始配置文件作个备份
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak
而后编辑配置文件
sudo vim /etc/nginx/sites-available/default
# default # 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:8001; # 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 default_server; listen [::]:80 default_server ipv6only=on; # the domain name it will serve for server_name .example.com; # 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 /path/to/your/mysite/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed }
一下是个人配置信息供参考
upstream django { server unix:///home/ubuntu/blogsite/mysite.sock; # for a file socket #server 127.0.0.1:8001; # for a web port socket (we'll use this first) } server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm; # Make site accessible from http://localhost/ server_name localhost; charset utf-8; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; client_max_body_size 75M; location /media { alias /home/ubuntu/blogsite/media; } location /static { alias /home/ubuntu/blogsite/static; } location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. #try_files $uri $uri/ =404; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules uwsgi_pass django; include /etc/nginx/uwsgi_params; }
下面咱们对django进行一下配置,编辑django配置文件mysite/settings.py 加上:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
这些配置信息具体用处可在个人另外一篇文章中看到。
而后执行命令
python manage.py collectstatic
而后重启nginx
sudo /etc/init.d/nginx restart
如今能够在django项目中放几个静态文件,看是否能访问:
好比将一个media.png的图片放在mysite/media 文件夹中(没有media文件夹能够本身建立一个)
而后访问
http://example.com/media/media.png
就能访问到这个图片。
还记得咱们建立的test.py文件,如今咱们再让它发挥一下做用
再test.py 的目录下执行命令:
uwsgi --socket :8001 --wsgi-file test.py
这里使用的8001端口号跟上面的配置文件中的
upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8001; # for a web port socket (we'll use this first) }
相对应,咱们如今是用的端口socket因此使用下面的配置,之后会使用到文件socket
而后咱们访问网站就能看到test.py 文件返回的内容了
此次咱们的访问顺序相似于:
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
注:若是上文中的8001端口不能使用可改用其余端口号
前面咱们使用了tcp端口,十分简单,可是咱们最好使用sockets文件,这样能减小资源消耗
咱们将default的配置稍做修改
server unix:///path/to/your/mysite/mysite.sock; # for a file socket # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
而后在django目录中使用命令
uwsgi --socket mysite.sock --wsgi-file test.py
而后访问网站,
注:若是不能访问(通常来讲访问不了!!),咱们check一下nginx的错误日志,
vim /var/log/nginx/error.log
若是在里面看到相似于
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission denied)
那是权限问题,咱们改用下面的命令
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666
若是能正常访问了,那咱们来试试使用wsgi来访问django项目
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=666
而后咱们使用.ini文件来配置uwsgi(差很少就能完成了),在项目目录下建立mysite_uwsgi.ini
# mysite_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) chdir = /path/to/your/project # Django's wsgi file module = project.wsgi # the virtualenv (full path) #home = /path/to/virtualenv # process-related settings # master master = true # maximum number of worker processes processes = 10 # the socket (use the full path to be safe socket = /path/to/your/project/mysite.sock # ... with appropriate permissions - may be needed chmod-socket = 666 # clear environment on exit vacuum = true
一下是个人配置,供参考
[uwsgi] # Django-related settings # the base directory (full path) chdir = /home/ubuntu/blogsite # Django's wsgi file module = blogsite.wsgi # the virtualenv (full path) # home = /path/to/virtualenv # process-related settings # master master = true # maximum number of worker processes processes = 2 # the socket (use the full path to be safe socket = /home/ubuntu/blogsite/mysite.sock # ... with appropriate permissions - may be needed chmod-socket = 666 # clear environment on exit vacuum = true
而后,跑起来
uwsgi --ini mysite_uwsgi.ini
以上,使用uWSGI+nginx部署Django项目就算是完成了,还有其它的配置可参考官方文档(好比怎样服务开机自启)
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
欢迎多来访问博客:http://liqiongyu.com/blog
微信公众号: