首页 > 解决方案 > 如何在 centos 6 服务器上设置 mod_wsgi

问题描述

我最近完成了一个 django 2 应用程序的开发,部分要求是将其部署到 centos 机器上的 apache 2 服务器上。django 应用是用 python3 编写的,centos 机器预装了 python2。

到目前为止,我一直很难在 apache2 服务器上安装 mod_wsgi 并部署应用程序。

任何帮助,将不胜感激。

编辑 我已经解决了这个问题。请看下面我的回答

标签: pythondjangomod-wsgi

解决方案


1.将python 3.6.5干净安装到usr/local和usr/local/lib

我确保 YUM 是最新的:

$ yum update

编译器和相关工具:

$ yum groupinstall -y "development tools"

编译期间启用 Python 的所有功能所需的库:

    $ yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite- 
devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel 
expat-devel

安装 Python 3.6.5:

$ wget http://python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
$ tar xf Python-3.6.5.tar.xz
$ cd Python-3.6.5
$ ./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath 
/usr/local/lib"
$ make && make altinstall

2. 使用 pip install 安装 mod_wsgi

使用刚刚安装的 python3.6 我运行了以下命令

$python3.6 -m pip install mod_wsgi

3.加载Mod_wsgi到apache模块

找到 apache 配置文件 /etc/apache2/conf/http.conf 并添加以下命令以通过指定安装路径来加载 mod_wsgi。就我而言,将 loadModule 命令添加到 /etc/apache2/conf.d/includes/pre_main_global.conf

$ LoadModule wsgi_module /usr/local/lib/python3.6/site- 
packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so

4.重启Apache Server,确认mod_wsgi已加载

$apachectl restart
$apachectl -M

查看列表并确保 mod_wsgi 是 apache 加载的模块的一部分。

5.配置虚拟主机并进行简单的hello.py测试

我有一台服务器,它已经托管了 7 个不同的站点。我只是创建了一个新的子域并将子域虚拟主机修改为看起来像

<VirtualHost mysite.com_ip:80>
ServerName wsgitest.mysite.com
ServerAlias www.wsgitest.mysite.com
DocumentRoot /home/account_username/public_html/wsgitest
ServerAdmin admin@mysite.com

<Directory /home/account_username/public_html/wsgitest>
Order allow,deny
Allow from all
</Directory>

WSGIScriptAlias / /home/account_username/public_html/wsgitest/hello.py

ErrorLog /home/account_username/public_html/wsgitest/wsgitest_error.log
</VirtualHost>

修改虚拟主机后重启apache

根据这个站点创建一个 hello.py wsgi 应用程序 - http://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html

你好.py

def application(environ, start_response):
status = '200 OK'
output = b'Hello World!'

response_headers = [('Content-type', 'text/plain'),
                    ('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]

所以我的 wsgitest 应用程序在服务器 /home/account_username/public_html/wsgitest/hello.py 上看起来像这样

最后,像这样在浏览器上测试站点 - wsgitest.mysite.com 你应该会看到“Hello World”

我希望这可以帮助别人


推荐阅读