一、环境
操作系统:Ubuntu 14.04 64位
python:2.7.6
Django:1.8.2
项目源码:https://github.com/youchuancong/django.git
部署方式:Django+uWsgi+nginx
二、部署
1、Django安装
官网:https://www.djangoproject.com/
安装
wget https://www.djangoproject.com/m/releases/1.8/Django-1.8.2.tar.gz
tar -zxvf Django-1.8.2.tar.gz
cd Django-1.8.2
python setup.py install
验证:
python -c "import django; print(django.get_version())"
屏幕显示1.8.2,则安装成功
注:需要预先安装setuptools
2、uWsg安装
官网:https://uwsgi.readthedocs.org/
安装:
pip install uwsgi
验证:
在浏览器中访问:http://127.0.0.1:9090/ ,可查看到hello world字符串
3、Beautiful Soup 4.2.0安装
官网:http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
说明:Beautiful Soup为python的html解析库
安装:
apt-get install Python-bs4
验证:
在浏览器中访问 http://127.0.0.1:9090,即可看到“hello world”
4、nginx安装
安装:
apt-get update apt-get install nginx
5、配置
1、下载项目源码
假设项目源码存放路径为/ycc/weixin
2、配置setting.py
STATIC_URL = 'http://192.168.17.125/statics/' BOOTSTRAP_BASE_URL='//192.168.17.125/statics/bootstrap_toolkit/'
注意:ip和端口请注意修改成实际的ip和端口
STATIC_URL为静态资源路径(静态资源通过nginx访问)
3、收集静态资源
cd /ycc/weixin python manage.py collectstatic cp statics_dev/* ../statics_dev/statics/
说明:执行collectstatic命令收集的静态资源存放位置由setting.py中的STATIC_ROOT定义,也可直接将该值定义为/ycc/statics_dev/statics/,无需手动复制
4、配置uwsgi
在/ycc目录创建wsgi_conf.ini文件
[uwsgi] socket = 0.0.0.0:3031 chdir = /ycc/weixin/ wsgi-file = weixin/wsgi.py processes = 4 threads = 2
5、配置nginx
nginx配置文件路径:/etc/nginx/nginx.conf
#user nobody; ## Default: nobody worker_processes 5; ## Default: 1 error_log error.log; pid nginx.pid; worker_rlimit_nofile 8192; events { worker_connections 4096; ## Default: 1024 } http { include mime.types;#必不可少,否则css文件响应头中的content-type存在问题,导致浏览器无法解析 #include /etc/nginx/proxy.conf; #include /etc/nginx/fastcgi.conf; index index.html index.htm index.php; gzip on;#开启gzip压缩 gzip_min_length 5k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 3; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary on; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] $status ' '"$request" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log access.log main; sendfile on; tcp_nopush on; server_names_hash_bucket_size 128; # this seems to be required for some vhosts server { # listen 80;#nginx监听端口 server_name domain1.com www.domain1.com; charset utf-8; access_log domain1.access.log main; root /ycc/statics_dev;#静态资源存储路径 index index.html; location / {#跳转到uwsgi uwsgi_pass 127.0.0.1:3031;#uwsgi的端口为3031,且为socket方式 include uwsgi_params; } location ^~ /statics/ {#路径以statics开头的直接访问nginx的静态资源 } } }
6、运行验证
cd /ycc uwsgi wsgi_conf.ini nginx
浏览器访问http://ip/,,出现登录界面