首先让我说我是系统工程师,但我正在学习WebDev / DevOps材料,重点是Python。因此,我正在将我的网站从WordPress(基于PHP)转移到Mezzanine(基于Python)。要从一个VPS运行多个站点,我正在使用Gunicorn设置VirtualEnv,并在前面设置Nginx。由于我的日常工作是作为SysEngineer并且我使用RHEL服务器,因此CentOS 6是我当前的主机托管平台。以下是我目前在CentOS 6.5服务器上的内容:
***INSTALL PYTHON 3.3.4, PIP, VIRTUALENV***
yum groupinstall -y development
yum install -y zlib-dev openssl-devel sqlite-devel bzip2-devel xz-libs
wget http://www.python.org/ftp/python/3.3.4/Python-3.3.4.tar.xz
xz -d Python-3.3.4.tar.xz
tar -xvf Python-3.3.4.tar && cd Python-3.3.4
./configure
make && make altinstall
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-2.2.tar.gz
tar -xvf setuptools-2.2.tar.gz && cd setuptools-2.2
python3.3 setup.py install
curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python3.3 -
pip install virtualenv
***SETUP SITES & GUNICORN***
rm -rf /root/Python*
rm -rf /root/setuptools*
mkdir /sites && mkdir /sites/sitename && mkdir /sites/sitename/app && cd /sites/sitename
virtualenv sitename_venv
source sitename_venv/bin/activate
pip install gunicorn
deactivate
***WSGI***
cd /sites/sitename && vi wsgi.py
source sitename_venv/bin/activate
source sitename_venv/bin/activate
gunicorn -b 0.0.0.0:8081 --workers=5 wsgi:app
最后一节(WSGI)是测试Gunicorn,而wsgi.py看起来像这样:
def app(environ, start_response):
"""Simplest possible application object"""
data = 'Hello, World!\n'
status = '200 OK'
response_headers = [
('Content-type','text/plain'),
('Content-Length', str(len(data)))
]
start_response(status, response_headers)
return iter([data])
所以,当我启动网站时,我看到了这一点:
(sitename_venv)[root@SERVER sitename]# gunicorn -b 0.0.0.0:8081 --workers=5 wsgi:app
2014-02-22 13:13:07 [36417] [INFO] Starting gunicorn 18.0
2014-02-22 13:13:07 [36417] [INFO] Listening at: http://0.0.0.0:8081 (36417)
2014-02-22 13:13:07 [36417] [INFO] Using worker: sync
2014-02-22 13:13:07 [36420] [INFO] Booting worker with pid: 36420
2014-02-22 13:13:07 [36421] [INFO] Booting worker with pid: 36421
2014-02-22 13:13:07 [36422] [INFO] Booting worker with pid: 36422
2014-02-22 13:13:07 [36423] [INFO] Booting worker with pid: 36423
2014-02-22 13:13:07 [36424] [INFO] Booting worker with pid: 36424
所以该网站应该正在运行,但当我浏览它时(IP:8081 - 来自同一网络,因为这是一个本地虚拟机而不是我的VPS)我只是得到“网页不可用”。如果我停止Gunicorn,我会得到“Chrome无法连接......等等,等等”所以我知道Gunicorn正在响应我的http请求。我不知道的是,为什么我看不出wsgi.py应该显示什么?有什么想法吗?
Nginx尚未进入混音状态,因此该配置无关紧要。我对virtualenv和python仍然相当新,所以如果我的wsgi.py文件有问题我也不会感到惊讶。
谢谢!
EDIT1:
[root@SERVER ~]# curl -v http://0:8081
* About to connect() to 0 port 8081 (#0)
* Trying 0.0.0.0... connected
* Connected to 0 (0.0.0.0) port 8081 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 0:8081
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: gunicorn/18.0
< Date: Sat, 22 Feb 2014 18:26:24 GMT
< Connection: close
< Content-type: text/plain
< Content-Length: 14
<
* transfer closed with 14 bytes remaining to read
* Closing connection #0
curl: (18) transfer closed with 14 bytes remaining to read
答案 0 :(得分:0)
数据在python 3中必须是二进制的,因此在返回
之前对其进行编码return iter([data.encode()])