环境说明
Linux : rhel 5.3
nginx:nginx-1.6.2
python:2.7.9
django: 1.6.7
uwsgi: 2.0.10
注意,要预先装好编译环境必备的开发包及libxml2,python-devel,pcre-devel,mysql-devel
2. 软件下载地址
nginx http://nginx.org/download/nginx-1.6.2.tar.gz
Python https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
Virtualenv https://pypi.python.org/packages/source/v/virtualenv/virtualenv-12.0.7.tar.gz#md5=e08796f79d112f3bfa6653cc10840114
Django https://www.djangoproject.com/download/1.6.7/tarball
uwsgi http://projects.unbit.it/downloads/uwsgi-2.0.10.tar.gz
MySQL-python https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip#md5=654f75b302db6ed8dc5a898c625e030c
3. 软件安装
安装nginx
# yum install pcre-devel gcc openssl-devel –y
# cd /usr/local/src/django-env
# tar -zxvf nginx-1.6.2.tar.gz
# cd nginx-1.6.2
# sed -i -e 's/1.6.2//g' -e 's/nginx\//WS/g' -e 's/"NGINX"/"WS"/g' src/core/nginx.h
# useradd webadmin
# ./configure --user=webadmin --group=webadmin --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
# make && make install
安装python
# cd /usr/local/src/django-env
# tar -zxvf Python-2.7.9.tgz
# cd Python-2.7.9
# ./configure --prefix=/usr/local/python2.7
# make && make install
安装virtualenv
# cd /usr/local/src/django-env
# tar –zxvf virtualenv-12.0.7.tar.gz
# cd virtualenv-12.0.7
# /usr/local/python2.7/bin/python setup.py install
构建虚拟环境
# cd /usr/local/python2.7/bin
# ./virtualenv /home/webadmin/virtualenv/tools
New python executable in /home/webadmin/tools/bin/python
Installing setuptools, pip...done.
进入并查看虚拟环境
进入虚拟环境在虚拟环境(长前缀的都表示在虚拟环境中的操作),默认会安装好easy_install这类工具
# source /home/webadmin/virtualenv/tools/bin/activate (退出执行命令 deactivate)
(tools)[root@tools bin]#
(tools)[root@tools bin]# python -V
Python 2.7.9
安装Django-1.5.4
(tools)[root@tools ~]# cd /usr/local/src/django-env/
(tools)[root@tools django-env]# tar -zxvf Django-1.5.4.tar.gz
(tools)[root@tools django-env]# cd Django-1.6.7
(tools)[root@tools Django-1.6.7]# python setup.py install
安装uwsgi
(tools)[root@tools ~]# cd /usr/local/src/django-env/
(tools)[root@tools django-env]# tar -zxvf uwsgi-2.0.10.tar.gz
(tools)[root@tools django-env]# cd uwsgi-2.0.10
(tools)[root@tools uwsgi-2.0.10]# python uwsgiconfig.py --build
(tools)[root@tools uwsgi-2.0.10]# python setup.py install
安装mysql-python
(tools)[root@tools ~]# cd /usr/local/src/django-env/
(tools)[root@tools django-env]# unzip MySQL-python-1.2.5.zip
(tools)[root@tools django-env]# cd MySQL-python-1.2.5
(tools)[root@tools MySQL-python-1.2.5]# python setup.py install
注意事项:
如果系统没有安装mysql-devel包,在安装MySQL-python的时候会报如下错误:
Traceback (most recent call last):
File "setup.py", line 16, in <module>
metadata, options = get_config()
File "/root/lzh/MySQL-python-1.2.2/setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "/root/lzh/MySQL-python-1.2.2/setup_posix.py", line 24, in mysql_config
raise EnvironmentError, "%s not found" % mysql_config.path
EnvironmentError: mysql_config not found
4. 软件配置
配置django项目
部署代码
(tools)[root@tools ~]# mkdir /home/webadmin/djangoproj/tools/ -p
(tools)[root@tools tools]# unzip tools.zip
(tools)[root@tools tools]# ls
dmc logs manage.py plugins src static templates tools tools.zip
在tools的项目配置文件目录(即setting.py所在目录),检查文件wsgi.py
(tools)[root@tools tools]# cd tools
(tools)[root@tools tools]# cat wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tools.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
同级目录下,创建uwsgi的启动参数的xml文件(wsgi.xml)
<uwsgi>
<socket>127.0.0.1:9090</socket>
<listen>20</listen>
<master>true</master>
<pidfile>/home/webadmin/logs/tools/uwsgi/uwsgi.pid</pidfile>
<processes>4</processes>
<module>wsgi</module>#就是上面那个wsgi.py文件
<pythonpath>/home/webadmin/djangoproj/tools/tools</pythonpath>
#建立项目的路径(此项需要注意,在虚拟环境中需要配置到wsgi所在目录;在非虚拟环境中,只需要配置到项目目录即可)
<profiler>true</profiler>
<memory-report>true</memory-report>
<enable-threads>true</enable-threads>
<logdate>true</logdate>
<limit-as>6048</limit-as>
<daemonize>/home/webadmin/logs/tools/uwsgi/tools.log</daemonize>
</uwsgi>
配置nginx
location /{
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
}
#静态文件的处理(如果django中有处理静态文件的了,就可以不要这个了;一般建议将静态文件交给nginx处理好些)
location /static/ {
alias /home/webadmin/djangoproj/tools/static/;
expires 30d;
break;
}
#配置django admin需要的文件
location ~* ^/media/(css|img|js)/.*$ {
root /usr/local/lib/python2.6/site-packages/django/contrib/admin;
expires 30d;
break;
}
location ~* ^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)$ {
access_log off;
break;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|bmp|js)$ {
expires 30d;
break;
}
检查和修正目录权限
(tools)[root@tools webadmin]# chown webadmin:webadmin /home/webadmin/ -R
5. 启动服务
(tools)[webadmin@tools ~]$ uwsgi -x /home/webadmin/djangoproj/tools/tools/wsgi.xml
# /usr/local/nginx/sbin/ngin
6. 多站点部署
重复从构建虚拟环境开始的步骤~然后在虚拟环境中安装好要运行的实际环境,并启动服务即可
配置nginx的虚拟主机即可
7. 故障检查思路
查看的日志包括(nginx日志[Nginx配置文件中定义],uwsgi日志[xml文件中定义])
查看uwsgi日志的主要原因是看uwsgi是否正常接管了nginx下发的请求任务
uwsgi日志可查看django执行错误信息,也可在开发中通过print 打印调试
nginx错误日志/访问日志查看
两种日志区别在于,前者针对代码程序级的错误,后者针对网络访问级的错误
8. 一些建议
有时候使用killall uwsgi时不能将uwsgi干掉,你可以uwsgi -s reload的方式重启uwsgi或kill -HUP cat /pathto/uwsgi.pid
uWSG配置文件的进程数,可以根据实际情况分配。不要开得太大,否则机器可能会内存耗用太高。一般来说,对于一个小社区来说,4个进程已经足够了。
一般情况下,可以编写一下 stop.sh 脚本 来关闭uWSGI。
有了uWSGI为什么还需要nginx?nginx具备优秀的静态内容处理能力,然后将动态内容转发给uWSGI服务器,这样可以达到很好的客户端响应。
9. 注意事项
日志里面报类似于“Mon Mar 23 10:26:49 2015 – — no python application found, check your startup logs for errors —”这类错误时,需要好好检查一下xml文件
这个文件里面行尾不能有空格,参数左右也不能有空格,这个问题坑了我一晚上!!!
日志里面报类似于”ImportError: No module named wsgi”这类错误,需要检查一下你的xml文件的pythonpath选项
在虚拟环境下,需要配置到uwsgi所在目录(非虚拟环境下,只需要配置到项目所在路径即可)
10. 备注
启动时,正常的日志:
Mon Mar 23 10:59:20 2015 – *** Starting uWSGI 2.0.10 (64bit) on [Mon Mar 23 10:59:20 2015] ***
Mon Mar 23 10:59:20 2015 – compiled with version: 4.1.2 20080704 (Red Hat 4.1.2-44) on 22 March 2015 22:08:54
Mon Mar 23 10:59:20 2015 – os: Linux-2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010
Mon Mar 23 10:59:20 2015 – nodename: tools.cyou.com
Mon Mar 23 10:59:20 2015 – machine: x86_64
Mon Mar 23 10:59:20 2015 – clock source: unix
Mon Mar 23 10:59:20 2015 – pcre jit disabled
Mon Mar 23 10:59:20 2015 – detected number of CPU cores: 16
Mon Mar 23 10:59:20 2015 – current working directory: /home/webadmin/djangoproj/tools
Mon Mar 23 10:59:20 2015 – writing pidfile to /home/webadmin/logs/tools/uwsgi/uwsgi.pid
Mon Mar 23 10:59:20 2015 – detected binary path: /home/webadmin/virtualenv/tools/bin/uwsgi
Mon Mar 23 10:59:20 2015 – your processes number limit is 268288
Mon Mar 23 10:59:20 2015 – limiting address space of processes…
Mon Mar 23 10:59:20 2015 – your process address space limit is 6341787648 bytes (6048 MB)
Mon Mar 23 10:59:20 2015 – your memory page size is 4096 bytes
Mon Mar 23 10:59:20 2015 – detected max file descriptor number: 1024
Mon Mar 23 10:59:20 2015 – lock engine: pthread robust mutexes
Mon Mar 23 10:59:20 2015 – thunder lock: disabled (you can enable it with –thunder-lock)
Mon Mar 23 10:59:20 2015 – uwsgi socket 0 bound to TCP address 127.0.0.1:9090 fd 3
Mon Mar 23 10:59:20 2015 – Python version: 2.7.9 (default, Mar 23 2015, 10:40:10) [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)]
Mon Mar 23 10:59:20 2015 – Python main interpreter initialized at 0x161c5580
Mon Mar 23 10:59:20 2015 – python threads support enabled
Mon Mar 23 10:59:20 2015 – your server socket listen backlog is limited to 20 connections
Mon Mar 23 10:59:20 2015 – your mercy for graceful operations on workers is 60 seconds
Mon Mar 23 10:59:20 2015 – mapped 363840 bytes (355 KB) for 4 cores
Mon Mar 23 10:59:20 2015 – *** Operational MODE: preforking ***
Mon Mar 23 10:59:20 2015 – added /home/webadmin/djangoproj/tools/tools/ to pythonpath.
Mon Mar 23 10:59:20 2015 – WSGI app 0 (mountpoint=”) ready in 0 seconds on interpreter 0x161c5580 pid: 24588 (default app)
Mon Mar 23 10:59:20 2015 – *** uWSGI is running in multiple interpreter mode ***
Mon Mar 23 10:59:20 2015 – spawned uWSGI master process (pid: 24588)
Mon Mar 23 10:59:20 2015 – spawned uWSGI worker 1 (pid: 24599, cores: 1)
Mon Mar 23 10:59:20 2015 – spawned uWSGI worker 2 (pid: 24600, cores: 1)
Mon Mar 23 10:59:20 2015 – spawned uWSGI worker 3 (pid: 24601, cores: 1)
Mon Mar 23 10:59:20 2015 – spawned uWSGI worker 4 (pid: 24602, cores: 1)