django+nginx+ubuntu
2016-09-01 15:02
447 查看
django的配置也是啥都有,本文是楼主亲测好用,都是楼主的亲身配置的过程以及一些注意事项,希望帮到大家!
1.nginx
安装
sudo apt-get install nginx
启动和关闭nginx
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
一般不推荐重启nginx,使用reload就好
sudo /etc/init.d/nginx reload
2. uwsgi
安装
pip install uwsgi
3. 测试uwsgi+django
原理
the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django
测试uwsgi是否工作,在自己的项目目录下创建test.py文件:
uwsgi --http :8000 --wsgi-file test.py
以上代码的含义是使用http协议8000端口,加载制定文件test.py,更多uwsgi的命令说明可自行google
打开url http://localhost:8000
# 如果服务器不能再浏览器查看网页,可以在命令行使用wget命令
# wget localhost:8000
# 如果能够访问,则会在当前目录下生成一个index文件
如果访问正确,则下面的三个环节是通的
the web client <-> uWSGI <-> Python
测试django运行正常
python manage.py runserver 0.0.0.0:8000
如果没问题,使用uwsgi将项目拉起来
uwsgi --http :8000 --module mysite.wsgi
如果project能够正常被拉起,说明以下环节是通的:
the web client <-> uWSGI <-> Django
4.部署nginx
安装nginx完成后,如果能正常打开http://hostname#这里是你的服务器域名,说明下面环节是通畅的:
the web client <-> the web server
增加nginx配置
将uwsgi_params文件拷贝到
4000
项目文件夹下。uwsgi_params文件在/etc/nginx/目录下
在项目文件夹下创建文件mysite_nginx.conf,填入并修改下面内容:
这里需要几点注意
默认使用的80端口,如果nginx不能启动,需要看看后台占用80端口的程序,当然你也可以更改其他端口
location的语法需要注意,如果有问题可以自行google相关配置。
nginx的日志文件在/var/log/nginx/下,可能涉及权限和链接问题,直接查看日志。
这个configuration文件告诉nginx从文件系统中拉起media和static文件作为服务,同时相应django的request
每次更改配置文件,记得执行
sudo /etc/init.d/nginx reload
在/etc/nginx/sites-enabled目录下创建本文件的连接,使nginx能够使用它:
sudo ln -s /path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/mysite_nginx.conf
5. 部署静态文件
首先在项目目录下执行
python manage.py collectstatic
重新加载nginx
sudo /etc/init.d/nginx reload
然后检查media文件是否已经正常拉起:
在目录/path/to/your/project/project/media directory下添加文件meida.png,然后访问http://example.com:8000/media/media.png ,成功后进行下一步测试。
6. 使用Unix socket代替TCP port
对mysite_nginx.conf做如下修改:
sudo /etc/init.d/nginx reload
uwsgi --socket mysite.sock --wsgi-file test.py
打开 http://example.com:8000/ ,看看是否成功
如果没有成功:
检查 nginx error log(/var/log/nginx/error.log)。如果错误如下:
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission denied)
添加socket权限再次运行:
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (very permissive)
7.运行django的程序
如果上面一切都显示正常,则下面命令可以拉起django application:
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=666
下面使用.ini文件开启服务器工作,同时,配置文件中会指定virtualenv路径,如果django运行在虚拟环境中,就不需要每次开启python虚拟环境了。方法如下:
在application目录下创建文件mysite_uwsgi.ini,填入并修改下面内容:
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file
1.nginx
安装
sudo apt-get install nginx
启动和关闭nginx
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
一般不推荐重启nginx,使用reload就好
sudo /etc/init.d/nginx reload
2. uwsgi
安装
pip install uwsgi
3. 测试uwsgi+django
原理
the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django
测试uwsgi是否工作,在自己的项目目录下创建test.py文件:
# test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return ["Hello World"] # python2然后运行uwsgi:
uwsgi --http :8000 --wsgi-file test.py
以上代码的含义是使用http协议8000端口,加载制定文件test.py,更多uwsgi的命令说明可自行google
打开url http://localhost:8000
# 如果服务器不能再浏览器查看网页,可以在命令行使用wget命令
# wget localhost:8000
# 如果能够访问,则会在当前目录下生成一个index文件
如果访问正确,则下面的三个环节是通的
the web client <-> uWSGI <-> Python
测试django运行正常
python manage.py runserver 0.0.0.0:8000
如果没问题,使用uwsgi将项目拉起来
uwsgi --http :8000 --module mysite.wsgi
如果project能够正常被拉起,说明以下环节是通的:
the web client <-> uWSGI <-> Django
4.部署nginx
安装nginx完成后,如果能正常打开http://hostname#这里是你的服务器域名,说明下面环节是通畅的:
the web client <-> the web server
增加nginx配置
将uwsgi_params文件拷贝到
4000
项目文件夹下。uwsgi_params文件在/etc/nginx/目录下
在项目文件夹下创建文件mysite_nginx.conf,填入并修改下面内容:
# mysite_nginx.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: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; # 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 /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed } }
这里需要几点注意
默认使用的80端口,如果nginx不能启动,需要看看后台占用80端口的程序,当然你也可以更改其他端口
location的语法需要注意,如果有问题可以自行google相关配置。
nginx的日志文件在/var/log/nginx/下,可能涉及权限和链接问题,直接查看日志。
这个configuration文件告诉nginx从文件系统中拉起media和static文件作为服务,同时相应django的request
每次更改配置文件,记得执行
sudo /etc/init.d/nginx reload
在/etc/nginx/sites-enabled目录下创建本文件的连接,使nginx能够使用它:
sudo ln -s /path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/mysite_nginx.conf
5. 部署静态文件
首先在项目目录下执行
python manage.py collectstatic
重新加载nginx
sudo /etc/init.d/nginx reload
然后检查media文件是否已经正常拉起:
在目录/path/to/your/project/project/media directory下添加文件meida.png,然后访问http://example.com:8000/media/media.png ,成功后进行下一步测试。
6. 使用Unix socket代替TCP port
对mysite_nginx.conf做如下修改:
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)重新加载nginx,并在此运行uWSGI
sudo /etc/init.d/nginx reload
uwsgi --socket mysite.sock --wsgi-file test.py
打开 http://example.com:8000/ ,看看是否成功
如果没有成功:
检查 nginx error log(/var/log/nginx/error.log)。如果错误如下:
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission denied)
添加socket权限再次运行:
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (very permissive)
7.运行django的程序
如果上面一切都显示正常,则下面命令可以拉起django application:
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=666
下面使用.ini文件开启服务器工作,同时,配置文件中会指定virtualenv路径,如果django运行在虚拟环境中,就不需要每次开启python虚拟环境了。方法如下:
在application目录下创建文件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现在,只要执行以下命令,就能够拉起django application:
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file
相关文章推荐
- ubuntu 10.04 server 下安装nginx1.2.0 + uwsgi-1.2 + django-1.4
- Python+Django+Ubuntu+Apache+Nginx架设服务器成功记
- Ubuntu 14.04---Nginx+uWsgi+Django+Python+MongoDB
- 通过Nginx部署Django(基于ubuntu)
- python Django + uwsgi + nginx 在阿里云Ubuntu环境下的部署
- Ubuntu搭建Django+Flup+Nginx环境
- [django] Deploy Django Applications Using uWSGI and Nginx on Ubuntu 14.04
- Ubuntu + Django + Nginx + uwsgi
- Ubuntu如何搭建Django与Flup和Nginx环境?
- nginx+uwsgi+django在Ubuntu下的部署
- Ubuntu10.10 Server+Nginx+Django+Postgresql安装步骤
- Ubuntu 14.04+Django 1.7.1+Nginx+uwsgi部署教程
- Ubuntu 14.04---Nginx+uWsgi+Django+Python+MongoDB+m
- 基于nginx和uWSGI在Ubuntu上部署Django
- ubuntu下配置nginx+uwsgi+django
- ubuntu下nginx+uWSGI+Django搭建
- Ubuntu搭建Django+Flup+Nginx环境
- ubuntu14.04+Django1.7.1+nginx1.6+uwsgi2.0环境搭建
- ubuntu14.04下nginx+gunicorn部署django1.8
- 基于nginx和uWSGI在Ubuntu上部署Django