Autor: wangxianlonghtml
2016/7/10 16:17:55python
環境:linux
因爲centos
自帶的python2.6.6
已經驅動不起來django1.9
了. 大概會報這樣的錯誤nginx
Traceback (most recent call last):File "<stdin>", line 1, in <module> File "/usr/lib/python2.6/site-packages/django/__init__.py", line 1, in <module> from django.utils.version import get_versionFile "/usr/lib/python2.6/site-packages/django/utils/version.py", line 7, in <module> from django.utils.lru_cache import lru_cacheFile "/usr/lib/python2.6/site-packages/django/utils/lru_cache.py", line 28 fasttypes = {int, str, frozenset, type(None)}, SyntaxError: invalid syntax
因此咱們升級爲python2.7.5
. 咱們用來pyenv
來管理環境,固然也能夠從新編譯python
git
# 下載pyenv腳本 wget https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer chmod +x pyenv-installer ./pyenv-installer # 配置環境 cat /etc/profile.d/pyenv.sh export PATH="/root/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" source /etc/profile # 安裝python 2.7.5 pyenv install 2.7.5 # 使用 python2.7.5 pyenv local 2.7.5 pyenv rehash
yum install -y python-pip pip install django==1.9 pip install Mysql-python # 鏈接數據庫
wget http://nginx.org/download/nginx-1.8.1.tar.gz ./configure --prefix=/usr/local/nginx --with-http_stub_status_module--with-http_gzip_static_module && make && make install ln -s /usr/local/nginx/sbin/nginx /usr/bin/
pip install uwsgi 測試一下: uwsgi --http :8000 --wsgi-file test.py # test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2 在瀏覽器中看到helloworld
首先看下流程,知道流程思路清晰,作的纔不會亂,問題也好解決:github
the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django
web
配置文件sql
cat /usr/local/nginx/conf/nginx.conf #user nobody; worker_processes 2; #主進程數 events { use epoll; # 使用epoll I/O模型 worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server_tokens off; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; keepalive_timeout 45; server { listen 80; server_name x.x.x.x; charset utf-8; client_max_body_size 32M; location /static { alias /data/task/xuptlib/staticfile; #靜態文件位置,本身須要使用django來collect靜態文件 } location / { # 注意使用scoket,免去TCP的鏈接消耗 uwsgi_pass unix:///data/task/xuptlib/bookhelper.sock; include uwsgi_params; # 在配置文件conf中 } } }
配置完成:數據庫
nginx -t nginx
DEBUG = False ALLOWED_HOSTS = ['*'] # 注意容許的主機不要忘寫了, 不然HTTP400會來 # 順帶提一下能夠關閉url中admin後臺管理
# 剛纔有在nginx配置文件中提到哦!! STATIC_ROOT = os.path.join(BASE_DIR, "staticfile/") # 收集 python manage.py collectstatic
能夠理解uwsgi
在django
和nginx
搭了橋. 身份有點像Tomcat
django
配置文件/或者直接用命令
cat /etc/uwsgi.ini [uwsgi] chdir = /data/task/xuptlib #項目父目錄 module = bookhelper.wsgi #項目下的wsgi文件位置 socket = /data/task/xuptlib/bookhelper.sock # socket 文件位置,和nginx配置文件中的同樣哦!! chmod = 666 # socket 權限, 不夠的話,會permission denied master = true processes = 3 # 起的三個子進程數 vacuum = true # 退出清理環境 pidfile = /var/run/uwsgi.pid daemonize = /var/log/uwsgi.log # 日誌位置
uwsgi啓動腳本
[root@rikewang xuptlib]# cat /etc/init.d/uwsgi #! /bin/sh PATH="/root/.pyenv/plugins/pyenv-virtualenv/shims:/root/.pyenv/shims:/root/.pyenv/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin" NAME=uwsgi EXEC=uwsgi CONFIGFILE=/etc/$NAME.ini PIDFILE=/var/run/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME do_start() { $EXEC $CONFIGFILE &> /dev/null || echo -n "uwsgi already running" } do_stop() { $EXEC --stop $PIDFILE &> /dev/null || echo -n "uwsgi not running" rm -f $PIDFILE } do_reload() { $EXEC --reload $PIDFILE &> /dev/null || echo -n "uwsgi can't reload" } do_status() { ps aux|grep $EXEC } case "$1" in status) echo -en "Status $NAME: \n" do_status ;; start) echo -en "Starting $NAME: \n" do_start ;; stop) echo -en "Stopping $NAME: \n" do_stop ;; reload|graceful) echo -en "Reloading $NAME: \n" do_reload ;; *) echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2 exit 3 ;; esac exit 0
在回顧一下流程:
the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django