环境
- Ubuntu:14.04
- Python:2.7
部署代码
- 代码说明:样例代码参考Django官方例子 https://docs.djangoproject.com/en/1.10/intro/tutorial01/
- 代码位置:https://github.com/Eric-aihua/django_sample
安装软件
uwsgi
pip install uwsgi
Nginx
apt-get install nginx
Django
pip install django
验证uwsgi
初始化project
假设github上的代码已经下载到/root/django_sample 目录,执行以下命令
root@ubuntu:~/django_sample# python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying polls.0001_initial... OK
Applying polls.0002_user... OK
Applying polls.0003_user_zipcode... OK
Applying sessions.0001_initial... OK
## 修改project 配置文件
django_sample/settings.py 文件的下列配置,需要修改为本机的IP
ALLOWED_HOSTS = ['192.168.116.131','127.0.0.1','localhost']
使用uwsgi启动django的project
root@ubuntu:~/django_sample# uwsgi --http :8000 --module django_sample.wsgi
到目前为止,已经成功使用uwsgi启动了django工程,通过在浏览器中输入http://192.168.116.131:8000/polls/ 可以开始访问系统
# 配置Nginx
# 添加配置文件
在/root/django_sample 下创建两个文件
1. uwsgi_params:内容来自https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
- django_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 /root/django_sample/media; # your Django project's media files - amend as required
}
location /static {
alias /root/django_sample/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 include /root/django_sample/uwsgi_params; # the uwsgi_params file you installed
}
}
- 让Nginx加载project的nginx配置文件
root@ubuntu:~/django_sample# ln -s /root/django_sample/django_nginx.conf /etc/nginx/sites-enabled/
- 启动Nginx
/etc/init.d/nginx start
- 启动uwsgi
root@ubuntu:~/django_sample# uwsgi --socket :8001 --module django_sample.wsgi
此处的端口配置与mysite_nginx.conf 中upstream的配置相同
上述启动方式输入参数比较多,也可以将启动配置添加到ini文件中,创建django_sample_uwsgi.ini,内容如下:
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /root/django_sample
# Django's wsgi file
module = django_sample.wsgi
# the virtualenv (full path)
#home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 1
# the socket (use the full path to be safe
socket = :8001
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
启动方式为:root@ubuntu:~/django_sample# uwsgi –ini django_sample_uwsgi.ini
到目前为止,数据访问链路已经基本畅通,请求链路为:the web client <-> the Nginx <-> the socket <-> uWSGI <-> Django
访问路径:http://192.168.116.131:8000/polls/
参考
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html