1,准备工作
安装虚拟环境
-
$pip install virtualenv
-
$virtualenv uwsgi-test
-
$cd uwsgi-test
-
$source bin/activate
安装Django
-
pip install Django
-
django-admin.py startproject mysite
-
cd mysite
安装uwsgi
2,测试
-
# 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 --http :8000 --wsgi-file test.py
可以看到hello world
3,测试django项目
uwsgi --http :8000 --module mysite.wsgi
4,配置nginx和uwsgi
-
# 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配置文件软连接
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
# 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自启动,修改rc.local
/usr/local/bin/uwsgi --ini /path/to/your/project/mysite_uwsgi.ini --uid www-data --gid www-data
阅读(3028) | 评论(0) | 转发(0) |