折腾了两天的django+nginx+uWSGI+ubuntu环境搭建,终于把它解决了,由于网上这方面的资料很少,出了问题在google和stackoverflow也没找到答案,所以希望我的搭建过程对你有所帮助!少遇坑!
介绍
- Nginx是一个网页服务器,它能反向代理HTTP, HTTPS, SMTP, POP3, IMAP的协议链接,以及一个负载均衡器和一个HTTP缓存
- uWSGI是一个web服务器,它同时实现了uwsgi协议和WSGI协议
以下图片很好的说明它们的关系:
准备
- http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html (官方文档 PS:不要觉得自己英文烂就不去看,其实官方的文档才是最好的)
- 相关环境的版本:django-1.9.8, nginx 1.4.6, ubuntu14.04.1 LTS
配置(尽量按照官网的顺序)
- 安装环境
Django安装
pip install Django
django-admin.py startproject mysite
cd mysite
uwsgi安装
pip install 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
**访问你的网站或者在浏览器输入127.0.0.1:8000,如果看到Hello World则说明下面这三者是通的 **
the web client <-> uWSGI <-> Python
运行你的Django程序
python manage.py runserver 0.0.0.0:8000
访问你的网站或者在浏览器输入127.0.0.1:8000,如果看到It works,说明你的Django也是通的
在项目目录下输入以下命令,并且运行(其中mysite.wsgi是mysite/mysite/wsgi.py文件):
uwsgi --http :8000 --module mysite.wsgi
说明你的uwsgi到django也是通的:
the web client <-> uWSGI <-> Django
安装nginx
sudo apt-get install nginx
sudo /etc/init.d/nginx start
访问127.0.0.1或者你的网站,如果出现nginx则说明你的nginx可用(默认是是80端口)
the web client <-> the web server
下面步骤用来告诉nginx你的网站在哪里
首先从copy下面文件到你的项目
cp /etc/nginx/uwsgi_params ~/mysite
创建文件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
}
}
创建一个软链接到nginx,意思是告诉nginx你的网站配置信息
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
配置你的Django的setting.py
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
同步静态文件
python manage.py collectstatic
重启nginx
sudo /etc/init.d/nginx restart
在mysite/media目录下放入一张图片,media.png,并访问 http://ip:8000/media/media.png或者http://127.0.0.1:8000/media/media.png则可以看到你的图片
接下来打通nginx、uWSGI和test.py
uwsgi --socket :8001 --wsgi-file test.py
访问http://ip:8000或者127.0.0.1:8000,如果正常,则说明以下是通的
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
使用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)
运行测试,在浏览器输入 http://ip:8000/或http://127.0.0.1:8000/是否成功
uwsgi --socket mysite.sock --wsgi-file test.py
如果出现502错误或者在/var/log/nginx/error.log发现
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permissiondenied)
则使用以下方法解决权限问题,本人使用第一种成功了~
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (very permissive)
or:
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (more sensible)```
##调通你的django、nginx和uwsgi
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
>在浏览器输入 http://ip:8000/或http://127.0.0.1:8000/在网页出现It works这说明成功
##创建一个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
##运行
uwsgi --ini mysite_uwsgi.ini
##将uwsgi配置为Emperor模式(可不做)
create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
###(PS:在实际过程中,最后一句 我使用的是uwsgi --emperor /etc/uwsgi/vassals)
##将uwsgi配置为开机启动,编辑/etc/rc.local
/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log