經過各種坑之后centos+ uwsgi + nginx +django 終於配好了


https://pypi.python.org/pypi/setuptools#downloads

https://www.python.org/ftp/python/

 

開機 加入 uwsgi nginx 自啟動

echo "/etc/init.d/nginx restart" >>/etc/rc.local 

echo "/usr/bin/setsid /data/soft_ware/uwsgi-1.9.6/uwsgi /data/soft_ware/s10day11/demo.ini " >>/etc/rc.local 

setsid 是后台掛起不受hungup 信號的影響。 進程號為1 與init相同

 

一 centos python是2.6 版本所以我們需要 裝python2.7.9  請看之前的博客

查看python的版本

 

[plain]  view plain copy
 
  1. #python  -V    
  2. Python 2.6.6  


1.下載Python-2.7.3

 

 

[plain]  view plain copy
 
  1. #wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2  

 

附帶 3.5.1 https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tar.xz
2.解壓

 

 

[plain]  view plain copy
 
  1. #tar -jxvf Python-2.7.3.tar.bz2  


3.更改工作目錄

 

 

[plain]  view plain copy
 
  1. #cd Python-2.7.3  


4.安裝

 

 

 yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
[plain]  view plain copy
 
  1. #./configure  
  2. #make all             
  3. #make install  
  4. #make clean  
  5. #make distclean  


5.查看版本信息

 

 

[plain]  view plain copy
 
  1. #/usr/local/bin/python2.7 -V  


6.建立軟連接,使系統默認的 python指向 python2.7

[plain]  view plain copy
 
  1. #mv /usr/bin/python /usr/bin/python2.6.6  
  2. #ln -s /usr/local/bin/python2.7 /usr/bin/python  


7.重新檢驗Python 版本

 

 

[plain]  view plain copy
 
  1. #python -V  


8解決系統 Python 軟鏈接指向 Python2.7 版本后,因為yum是不兼容 Python 2.7的,所以yum不能正常工作,我們需要指定 yum 的Python版本

 

 

[plain]  view plain copy
 
  1. #vi /usr/bin/yum  

 

 

將文件頭部的
#!/usr/bin/python


改成
#!/usr/bin/python2.6.6

http://www.zhangchun.org/the-centos6-3-upgrade-python-to-2-7-3-

這篇教程就到這里了,但是不久就突然發現輸入法圖標不見了,然后打字沒有候選框!iBus 崩了!再次進行搜索,又是版本問題抓狂 iBus也是不支持Python2.7的啊!

於是。。。

 

 

以前在使用Python的時候,都是使用root用戶安裝好的全局python,現在,因為root用戶安裝的Python版本太低,同時自己沒有root權限去對全局Python升級,所以要在非root用戶下安裝自己指定的Python。因此,就重新整理了一份如何在Linux環境下使用非root用戶安裝python及其相關的庫,以備不時之需。

安裝python

python版本庫https://www.python.org/ftp/python/,此處我選擇2.7.5版本的,在安裝python的時候,使用--prefix指定安裝路徑即可,命令如下:

wget https://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz tar -xzf Python-2.7.5.tgz cd Python-2.7.5 mkdir -p /home/liudiwei/software/python27 ./configure --prefix="/home/liudiwei/software/python27" make make install

[liujianzuo@sy01-db3 pip-8.1.1]$ cat ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=/home/liujianzuo/bin/python/bin:$PATH:$HOME/bin

export PATH

[liujianzuo@sy01-db3 ~]$ . .bash_profile

[liujianzuo@sy01-db3 ~]$ which python
~/bin/python/bin/python

安裝setuptools

setuptools主要是為安裝pip做准備的,下面是從下載到安裝的全部命令,使用上面安裝的指定路徑的python/home/liudiwei/software/python27/bin/python進行安裝:

https://pypi.python.org/pypi/setuptools頁面最下面的有它的安裝鏈接

cd /home/work/prober/src/lib/thirdparty
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-12.0.3.tar.gz#md5=f07e4b0f4c1c9368fcd980d888b29a65 
tar xvf setuptools-12.0.3.tar.gz
cd setuptools-12.0.3
python setup.py install
cd ..
wget --no-check-certificate https://pypi.python.org/packages/41/27/9a8d24e1b55bd8c85e4d022da2922cb206f183e2d18fee4e320c9547e751/pip-8.1.1.tar.gz#md5=6b86f11841e89c8241d689956ba99ed7
tar -xzf pip-8.1.1.tar.gz
cd pip-8.1.1
python setup.py install
cd ..
# 升級pip
python -m pip install --upgrade pip

 

 

安裝pip

使用pip來安裝python相關庫,方便簡單,此處將python setup.py install的python換成自己安裝的指定路徑下的python/home/liudiwei/software/python27/bin/python setup.py install.

wget --no-check-certificate https://pypi.python.org/packages/41/27/9a8d24e1b55bd8c85e4d022da2922cb206f183e2d18fee4e320c9547e751/pip-8.1.1.tar.gz#md5=6b86f11841e89c8241d689956ba99ed7 tar -xzf pip-8.1.1.tar.gz cd pip-8.1.1 python setup.py install

 

安裝相關庫

進入python安裝目錄的bin路徑下,安裝下面相關庫,經測試,下列庫均可安裝。

  • simplejson
  • redis
  • numpy
  • scipy
  • sklearn

安裝命令:

cd /home/liudiwei/software/python27/bin/
./pip install simplejson ./pip install redis ./pip install numpy ./pip install scipy ./pip install sklearn

關於matplotlib的安裝,因為系統有些依賴包沒有安裝而導致matplotlib安裝失敗,如libpng, freetype等,待后續安裝完成后,再來完善。

 
 

 

 

 

 

9.配置iBus

分別用 vi 打開下面兩個文件,找到 exec python 那一行,把exec python 改為 exec python2.6 保存,退出。iBus在重啟后就恢復正常了!是不是很開心?

 

[plain]  view plain copy
 
  1. #vi /usr/bin/ibus-setup  
[plain]  view plain copy
 
    1. #vi/usr/libexec/ibus-ui-gtk  

二 、centos 安裝django  python支持的mysql模塊

yum -y install mysql-devel

[root@ayibang-server ~]# yum install -y MySQL-python

安裝uwsgi  盡量別裝 2版本 會報錯。 當然用如下方法裝2 版本 指定python版本安裝uwsgi也行。 待測。

重裝 uwsgi1.9版本 指定python版本 python2.7

[root@ayibang-server soft_ware]# tar zxf uwsgi-1.9.6.tar.gz 
[root@ayibang-server soft_ware]# cd uwsgi-1.9.6
[root@ayibang-server uwsgi-1.9.6]# python
python python-config python2 python2-config python2.7 python2.7-config
[root@ayibang-server uwsgi-1.9.6]# python2.7 uwsgiconfig.py --build

裝django

[root@ayibang-server soft_ware]# tar zxvf Django-1.8.4.tar.gz 

                  cd Django-1.8.4 之后 如果執行 安裝 就是python版本問題

[root@ayibang-server Django-1.8.4]# python setup.py install
Traceback (most recent call last):
File "setup.py", line 32, in <module>
version = __import__('django').get_version()
File "/data/soft_ware/Django-1.8.4/django/__init__.py", line 1, in <module>
from django.utils.version import get_version
File "/data/soft_ware/Django-1.8.4/django/utils/version.py", line 7, in <module>
from django.utils.lru_cache import lru_cache
File "/data/soft_ware/Django-1.8.4/django/utils/lru_cache.py", line 28
fasttypes = {int, str, frozenset, type(None)},
^
SyntaxError: invalid syntax

解決

[root@ayibang-server soft_ware]# wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz

接下來就是 第一步的操作; 安裝2.7版本python

 

四 報錯 沒裝settools

[root@ayibang-server Django-1.8.4]# python setup.py install
Traceback (most recent call last):
File "setup.py", line 5, in <module>
from setuptools import find_packages, setup
ImportError: No module named setuptools

1. 下載setuptools及pip的源碼包
	setuptools與pip都是python的模塊
	setuptools源碼包: https://pypi.python.org/pypi/setuptools
	pip源碼包: https://pypi.python.org/pypi/pip#downloads
2. 安裝setuptools與pip
目前下載的版本是setuptools-12.0.5.tar.gz與pip-6.0.6.tar.gz

先安裝setuptools, 進行setuptools的源碼根目錄下, 執行以下命令進行setuptools模塊的安裝:
 
        
[plain] view plaincopy 在CODE上查看代碼片 派生到我的代碼片
 
  1. # python setup.py install  
安裝完setuptools后, 接着安裝pip, 進入pip的源碼包根目錄下, 執行以下命令進行安裝:
 
        
[html] view plaincopy 在CODE上查看代碼片 派生到我的代碼片
 
  1. # python setup.py build  
  2. # python setup.py install  
安裝完pip后. 看看pip都安裝在哪里. 執行以下命令:
 
        
[plain] view plaincopy 在CODE上查看代碼片 派生到我的代碼片
 
  1. # whereis pip  
然后再執行 # pip  命令並回車, 如果無法用pip命令, 則可通過創建pip軟鏈接, 執行以下命令:


重裝django 檢測

[root@ayibang-server Django-1.8.7]# python
Python 2.7.9 (default, Dec 3 2015, 01:04:49)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>>


五 5.關聯django到nginx

服務器已經在跑PHP站點,現在新生成一個虛擬服務器

[root@ayibang-server ~]# cat /etc/nginx/conf.d/django.conf
server {
listen 8000;
server_name 192.168.1.200;
location / {
include uwsgi_params;
uwsgi_pass 0.0.0.0:9000;
}
}

完成后重啟下nginx服務
$ sudo /etc/init.d/nginx restart

 

六 配置 uwsgi 啟動文件   這里有個錯 ,網上文章有錯誤 網上的文件寫的可能是早期的 版本。 

1 py文件啟動

[root@ayibang-server ~]# cd /data/soft_ware/mysite/  #進到項目目錄
[root@ayibang-server mysite]# ls
app01 db.sqlite3 demo.ini demo.py demo.pyc manage.py mysite

編輯一個py文件
[root@ayibang-server mysite]# cat demo.py
#!/usr/bin/env python
#coding:utf-8
import os
from django.core.wsgi import get_wsgi_application   #網上的大都導入的其他模塊 這個版本1.9 uwsgi 1.8 django 可能就不支持了
os.environ['DJANGO_SETTINGS_MODULE']='mysite.settings' #項目名 mysite 記得改 ; setting 
application = get_wsgi_application()

啟動 uwsgi  如下端口 對應 你的nginx的pass 端口   記住應用 你自己安裝的 uwsg 看前面的第2三4補

[root@ayibang-server mysite]# /data/soft_ware/uwsgi-1.9.6/uwsgi -s 0.0.0.0:9000 -w demo

 

2  ini 文件啟動

[root@ayibang-server ~]# cd /data/soft_ware/mysite/  #進到項目目錄
[root@ayibang-server mysite]# ls 

[uwsgi]
socket = 0.0.0.0:9000
processes = 3  #進程數 
chdir = /data/soft_ware/mysite/ #項目名 mysite 記得改
pythonpath = /usr/local/bin/python
env = DJANGO_SETTINGS_MODULE=mysite.settings   #項目名 mysite 記得改 ; setting 
module = django.core.wsgi:get_wsgi_application()

ini 文件 啟動方式

[root@ayibang-server s10day11]# /data/soft_ware/uwsgi-1.9.6/uwsgi demo.ini 

 

對比 我的 另一個項目 的不同

[root@ayibang-server ~]# cd /data/soft_ware/s10day11/
[root@ayibang-server s10day11]# ls
app01 app03 app04 db.sqlite3 demo.ini demo.py demo.pyc manage.py s10day11 statics templates test
[root@ayibang-server s10day11]# cat demo.py
#!/usr/bin/env python
#coding:utf-8
import os
from django.core.wsgi import get_wsgi_application
os.environ['DJANGO_SETTINGS_MODULE']='s10day11.settings'
application = get_wsgi_application()
[root@ayibang-server s10day11]# cat demo.ini
[uwsgi]
socket = 0.0.0.0:9000
processes = 9
chdir = /data/soft_ware/s10day11/
pythonpath = /usr/local/bin/python
env = DJANGO_SETTINGS_MODULE=s10day11.settings
module = django.core.wsgi:get_wsgi_application()

 

 

[root@ayibang-server s10day11]# /data/soft_ware/uwsgi-1.9.6/uwsgi demo.ini
[uWSGI] getting INI configuration from demo.ini
*** Starting uWSGI 1.9.6 (64bit) on [Thu Dec 3 15:02:25 2015] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-4) on 03 December 2015 12:59:57
os: Linux-2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013
nodename: ayibang-server
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /data/soft_ware/s10day11
detected binary path: /data/soft_ware/uwsgi-1.9.6/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 30853
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
uwsgi socket 0 bound to TCP address 0.0.0.0:9000 fd 3
Python version: 2.7.9 (default, Dec 3 2015, 01:04:49) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xd129b0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 654264 bytes (638 KB) for 9 cores
*** Operational MODE: preforking ***
added /usr/local/bin/python to pythonpath.
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0xd129b0 pid: 23478 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (pid: 23478, cores: 1)
spawned uWSGI worker 2 (pid: 23485, cores: 1)
spawned uWSGI worker 3 (pid: 23486, cores: 1)
spawned uWSGI worker 4 (pid: 23487, cores: 1)
spawned uWSGI worker 5 (pid: 23488, cores: 1)
spawned uWSGI worker 6 (pid: 23489, cores: 1)
spawned uWSGI worker 7 (pid: 23490, cores: 1)
spawned uWSGI worker 8 (pid: 23491, cores: 1)
spawned uWSGI worker 9 (pid: 23492, cores: 1)

 

3 xml 文件啟動方式

如安裝了libxml2,也可用如下方法處理:
在app目錄創建個django.xml文件
[root@ayibang-server s10day11]# sudo vi django.xml
內容如下:

 127.0.0.1:9090
2
 /var/www/erp/erp
 ..
 DJANGO_SETTINGS_MODULE=mysite.settings
 django.core.wsgi:get_wsgi_application()


啟動方式:
[root@ayibang-server s10day11]# uwsgi -x django.xml 

 

 

FAQ 總結

報錯1  python版本不一致

[root@ayibang-server s10day11]# uwsgi -s 0.0.0.0:9999 -w demo
*** Starting uWSGI 2.0.6 (64bit) on [Thu Dec 3 12:29:00 2015] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-4) on 03 December 2015 00:55:54
os: Linux-2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013
nodename: ayibang-server
machine: x86_64
clock source: unix
detected number of CPU cores: 4
current working directory: /data/soft_ware/s10day11
detected binary path: /usr/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 30853
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 0.0.0.0:9999 fd 3
Python version: 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1a33780
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72768 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
File "./demo.py", line 2, in <module>
import django.core.handlers.wsgi
ImportError: No module named django.core.handlers.wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 26436, cores: 1)

 

開始 解決報錯1  python 版本不一致

先pip uninstall uwsgi,然后yum install -y pcre pcre-devel pcre-static

 

安裝pip

[root@ayibang-server soft_ware]# tar zxf pip-6.0.6.tar.gz
[root@ayibang-server soft_ware]# cd pip-
-bash: cd: pip-: No such file or directory
[root@ayibang-server soft_ware]# cd pip-6.0.6
[root@ayibang-server pip-6.0.6]# python setup.py build

[root@ayibang-server pip-6.0.6]# python setup.py install

 

創建項目 app

[root@ayibang-server soft_ware]# python /usr/local/bin/django-admin.py startproject mysite
[root@ayibang-server soft_ware]# python manage.py startapp app01

 

原來是 uwsgi是識別的 python2.6 而2.6 對django1.8不支持太好

[root@ayibang-server uwsgi-2.0.6]# ls /usr/bin/python*
/usr/bin/python /usr/bin/python2 /usr/bin/python2.6-config
/usr/bin/python-config /usr/bin/python2.6 /usr/bin/python2.6.6
[root@ayibang-server uwsgi-2.0.6]# vim /usr/bin/python-config
[root@ayibang-server uwsgi-2.0.6]# ls /usr/local/bin/python*
/usr/local/bin/python /usr/local/bin/python2 /usr/local/bin/python2.7
/usr/local/bin/python-config /usr/local/bin/python2-config /usr/local/bin/python2.7-config

重裝 uwsgi1.9版本 指定python版本 python2.7

[root@ayibang-server soft_ware]# tar zxf uwsgi-1.9.6.tar.gz
[root@ayibang-server soft_ware]# cd uwsgi-1.9.6
[root@ayibang-server uwsgi-1.9.6]# python
python python-config python2 python2-config python2.7 python2.7-config
[root@ayibang-server uwsgi-1.9.6]# python2.7 uwsgiconfig.py --build

 

執行1.9版本報錯 缺少lib   解決yum libyaml

[root@ayibang-server uwsgi-1.9.6]# ./uwsgi --version
./uwsgi: error while loading shared libraries: libyaml-0.so.2: cannot open shared object file: No such file or directory

[root@ayibang-server uwsgi-1.9.6]# yum install libyaml

[root@ayibang-server uwsgi-1.9.6]# which uwsgi
/usr/sbin/uwsgi
[root@ayibang-server uwsgi-1.9.6]# uwsgi --version
2.0.6

重新啟動 

[root@ayibang-server mysite]# /data/soft_ware/uwsgi-1.9.6/uwsgi -s 127.0.0.1:9000 -w demo
*** Starting uWSGI 1.9.6 (64bit) on [Thu Dec 3 13:05:55 2015] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-4) on 03 December 2015 12:59:57
os: Linux-2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013
nodename: ayibang-server
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /data/soft_ware/mysite
detected binary path: /data/soft_ware/uwsgi-1.9.6/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 30853
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
uwsgi socket 0 bound to TCP address 127.0.0.1:9000 fd 3
Python version: 2.7.9 (default, Dec 3 2015, 01:04:49) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x2844750
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72696 bytes (70 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x2844750 pid: 1772 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 1772, cores: 1)

 

 

報錯2 解決

http://blog.163.com/rihui_7/blog/static/21228514320151353149688/ 

uwsgi+django報錯django.core.exceptions.AppRegistryNotReady

uwsgi+django報錯django.core.exceptions.AppRegistryNotReady

來源:http://www.aaini.com/

File “/usr/lib/python2.7/site-packages/django/core/handlers/wsgi.py”, line 187, in __call__
response = self.get_response(request)
File “/usr/lib/python2.7/site-packages/django/core/handlers/base.py”, line 199, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File “/usr/lib/python2.7/site-packages/django/core/handlers/base.py”, line 236, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File “/usr/lib/python2.7/site-packages/django/views/debug.py”, line 91, in technical_500_response
html = reporter.get_traceback_html()
File “/usr/lib/python2.7/site-packages/django/views/debug.py”, line 350, in get_traceback_html
return t.render(c)
File “/usr/lib/python2.7/site-packages/django/template/base.py”, line 148, in render
return self._render(context)
File “/usr/lib/python2.7/site-packages/django/template/base.py”, line 142, in _render
return self.nodelist.render(context)
File “/usr/lib/python2.7/site-packages/django/template/base.py”, line 844, in render
bit = self.render_node(node, context)
File “/usr/lib/python2.7/site-packages/django/template/debug.py”, line 80, in render_node
return node.render(context)
File “/usr/lib/python2.7/site-packages/django/template/debug.py”, line 90, in render
output = self.filter_expression.resolve(context)
File “/usr/lib/python2.7/site-packages/django/template/base.py”, line 624, in resolve
new_obj = func(obj, *arg_vals)
File “/usr/lib/python2.7/site-packages/django/template/defaultfilters.py”, line 769, in date
return format(value, arg)
File “/usr/lib/python2.7/site-packages/django/utils/dateformat.py”, line 343, in format
return df.format(format_string)
File “/usr/lib/python2.7/site-packages/django/utils/dateformat.py”, line 35, in format
pieces.append(force_text(getattr(self, piece)()))
File “/usr/lib/python2.7/site-packages/django/utils/dateformat.py”, line 268, in r
return self.format(‘D, j M Y H:i:s O’)
File “/usr/lib/python2.7/site-packages/django/utils/dateformat.py”, line 35, in format
pieces.append(force_text(getattr(self, piece)()))
File “/usr/lib/python2.7/site-packages/django/utils/encoding.py”, line 85, in force_text
s = six.text_type(s)
File “/usr/lib/python2.7/site-packages/django/utils/functional.py”, line 144, in __text_cast
return func(*self.__args, **self.__kw)
File “/usr/lib/python2.7/site-packages/django/utils/translation/__init__.py”, line 83, in ugettext
return _trans.ugettext(message)
File “/usr/lib/python2.7/site-packages/django/utils/translation/trans_real.py”, line 325, in ugettext
return do_translate(message, ‘ugettext’)
File “/usr/lib/python2.7/site-packages/django/utils/translation/trans_real.py”, line 306, in do_translate
_default = translation(settings.LANGUAGE_CODE)
File “/usr/lib/python2.7/site-packages/django/utils/translation/trans_real.py”, line 209, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File “/usr/lib/python2.7/site-packages/django/utils/translation/trans_real.py”, line 189, in _fetch
“The translation infrastructure cannot be initialized before the ”
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don’t make non-lazy gettext calls at import time.

參照網上用uwsgi+nginx配置python環境時,報了這么個錯,google發現是WSGI application的問題,好象是因為django升級,配置有所變化。

原來:

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

改成:

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

重啟一下uwsgi。

網上找來的解決辦法,希望有幫助。

后補:也可能是python版本和django版本不兼容問題,重新安裝django並選定安裝的版本就可以了

 

 

原來的 是如下 方式寫的 不過我的改成上面 黃色部分  如下是老版本的 調用方法

配置並運行一個簡單的python頁面

$ cd /var/www/mysite
/var/www/mysite$ sudo vi demo.py
輸入如下代碼:
import  os
import  django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE']='mysite.settings'
application=django.core.handlers.wsgi.WSGIHandler()

/var/www/mysite$ uwsgi -s 127.0.0.1:9090 -w demo 

如安裝了libxml2,也可用如下方法處理:
在app目錄創建個django.xml文件
/var/www/mysite$ sudo vi django.xml
內容如下:

 127.0.0.1:9090
2
 /var/www/erp/erp
 ..
 DJANGO_SETTINGS_MODULE=mysite.settings
 django.core.handlers.wsgi:WSGIHandler()


啟動方式:
/var/www/mysite$ uwsgi -x django.xml 

另一種配置方式,用於沒有安裝libxml2:
ini配置文件
/var/www/mysite$ sudo vi django.ini
[uwsgi]
socket = 127.0.0.1:9090
processes = 2
chdir = /var/www/mysite/mysite/
pythonpath = ..
env = DJANGO_SETTINGS_MODULE=mysite.settings
module = django.core.handlers.wsgi:WSGIHandler()

啟動方式:
/var/www/mysite$ uwsgi django.ini 

服務成功啟動后訪問 http://127.0.0.1:8000,如能看到歡迎信息,就說明運行環境配置好了。更多的設置可以看下The_Django_Book
千里之行,始於足下。慢慢設計你的website吧...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM