Django+Nginx+uwsgi+Ubuntu配置
2017-06-19 22:36
459 查看
参考官方文档
Nginx常用操作:
开启nginx:
重启nginx:
关闭nginx:
(2)进入manage.py同目录下运行uwsgi
(3)在浏览器输入127.0.0.1:8000,如果正常显示Hello World则说明[b]the web client <-> uWSGI <-> Python三者是通的[/b]
安装部分:
1.虚拟环境下安装Djang
pip install django
2.安装uwsgi
sudo pip install uwsgi
3.安装Nginx
sudo apt-get install nginx
Nginx常用操作:
开启nginx:
sudo serverice nginx start
重启nginx:
sudo serverice nginx restart
关闭nginx:
sudo serverice nginx stop
准备&测试部分:
1.虚拟环境下创建Django项目
python django-admin startproject mysite
2.测试运行uwsgi
(1)Django项目下manage.py同目录创建测试文件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
(2)进入manage.py同目录下运行uwsgi
uwsgi --http :8000 --wsgi-file test.py
(3)在浏览器输入127.0.0.1:8000,如果正常显示Hello World则说明[b]the web client <-> uWSGI <-> Python三者是通的[/b]
配置部分:
1.拷贝文件uwsgi_params到Django项目
cp /etc/nginx/uwsgi_params ~/mysite
2.项目manage.py同目录下创建文件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 8000; # 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 } }
3.创建软链接到nginx
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
4.配置Django项目的setting.py,末尾添加:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
5.Django项目同步静态文件
python manage.py collectstatic
6.项目manage.py同目录下创建media文件夹,并放入media.png
7.重启Nginx
sudo serverice nginx restart
8.在浏览器输入127.0.0.1:8000/media/media.png,如果正常显示media.png则说明Django项目在nginx上正常运行
9.打通nginx、uWSGI和test.py
uwsgi --socket :8001 --wsgi-file test.py
10.在浏览器输入127.0.0.1:8000,如果正常显示Hello World则说明the web client <-> the web server <-> the socket <-> uWSGI <-> Python三者是通的
11.使用Unix sockets代替端口,更改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)
12.连接nginx、uWSGI和test.py运行下面命令,在浏览器输入127.0.0.1:8000,观察是否成功
uwsgi --socket mysite.sock --wsgi-file test.py
13.如果出现502错误,则需解决权限问题
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (more sensible)
14.调通django、nginx和uwsgi
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
15.在浏览器输入127.0.0.1:8000,如果正常显示Django的”It works”则说明成功
优化部分:
1.项目manage.py同目录下创建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 = 664 # clear environment on exit vacuum = true
2.运行
uwsgi --ini mysite_uwsgi.ini
相关文章推荐
- Ubuntu12.4系统下配置Django+Nginx+uWSGI
- Ubuntu linux nginx + uwsgi + django 配置
- ubuntu下uwsgi + nginx +django 修改默认配置部署的注意点
- Django Ubuntu部署:nginx + uWSGI配置
- Ubuntu 上配置Python3 + django + Nginx + uwsgi
- ubuntu下配置nginx+uwsgi+django
- 基于Ubuntu16.04服务器的Nginx+Django+Uwsgi配置
- Django + nginx + uwsgi配置和环境搭建(ubuntu)
- 腾讯云Ubuntu 16.04 配置Django+Uwsgi+Nginx
- Ubuntu Server 14.04.2 LTS 配置 Nginx + uwsgi + Django
- 我的 Fedora 20 + django + nginx + uwsgi 配置
- Ubuntu 14.04+Django 1.7.1+Nginx+uwsgi部署最简记录(精简自uwsgi官网教程)
- Debian环境 Python + Django + Nginx + uWSGI + MySQL 配置
- Ubuntu 14.04---Nginx+uWsgi+Django+Python+MongoDB+m
- Nginx + uWSGI + Django 配置
- VPS上配置NGINX+UWSGI+DJANGO+MYSQL
- 在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程
- Ubuntu 14.04+Django 1.7.1+Nginx+uwsgi部署教程
- CentOS6.下Nginx+django+uwsgi配置多个虚拟机
- Nginx+uWSGI+django配置详解