0%

ubantu下使用nginx、uwsgi搭建django环境(二)

安装 virtualenvwrapper

pip install virtualenvwrapper

在~/.bashrc中添加行:

1
2
3
4
5
6
7
8
9
10
export VIRTUALENV_USE_DISTRIBUTE=1        #  总是使用 pip/distribute                                                                                   
export WORKON_HOME=$HOME/.local/virtualenvs # 所有虚拟环境存储的目录
if [ -e $HOME/.local/bin/virtualenvwrapper.sh ];then
source $HOME/.local/bin/virtualenvwrapper.sh
else if [ -e /usr/local/bin/virtualenvwrapper.sh ];then
source /usr/local/bin/virtualenvwrapper.sh
fi
fi
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true

启动 virtualenvwrapper: source ~/.bashrc

安装uwsgi

pip install uwsgi

基本测试

创建一个名为 test.py文件:

1
2
3
4
5
# 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

访问8000端口显示 Hello World

测试 django 项目

首先确保 django 项目正确运行

uwsgi --http :8000 --module project_name.wsgi

此时访问 8000 端口显示项目正常运行

安装 django

首先进入虚拟环境 workon blogvir1

然后安装 pip install django==1.10.6

安装 ngnix

提示被占用,查询网上的方法,删除锁住的文件,sudo rm /var/lib/dpkg/lock

配置 ngnix

修改 /etc/nginx/sites-enabled/default

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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
}
}

部署静态文件

settings.py 添加:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

然后运行
python manage.py collectstatic

配置 uwsgi 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir = /home/str3am/django_blog
# Django's wsgi file
module = django_blog.wsgi
# the virtualenv (full path)
home = /home/str3am/.local/virtualenvs/blogvir1

# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /home/str3am/django_blog.django_blog.sock
# ... with appropriate permissions - may be needed
chmod-socket = 664
# clear environment on exit
vacuum = true

# plugins = python

uwcgi 运行

uwcgi --ini mysites.ini

搭建成功