Python 如何在centos 6服务器上设置mod_wsgi

Python 如何在centos 6服务器上设置mod_wsgi,python,django,mod-wsgi,Python,Django,Mod Wsgi,我最近完成了django 2应用程序的开发,部分需求是将其部署到centos机器上的apache 2服务器上。 django应用程序是用python3编写的,centos机器预装了python2 到目前为止,我在apache2服务器上安装mod_wsgi并部署应用程序时遇到了困难 任何帮助都将不胜感激 编辑 我已经解决了这个问题。见下面我的答案1。将python 3.6.5干净地安装到usr/local和usr/local/lib 我确保百胜是最新的: $ yum update 编译器和相关工

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

到目前为止,我在apache2服务器上安装mod_wsgi并部署应用程序时遇到了困难

任何帮助都将不胜感激

编辑 我已经解决了这个问题。见下面我的答案

1。将python 3.6.5干净地安装到usr/local和usr/local/lib 我确保百胜是最新的:

$ 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安装安装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服务器并确认已加载mod_wsgi 查看列表并确保mod_wsgi是apache加载的模块的一部分

5.配置虚拟主机并执行简单的hello.py测试 我有一个服务器,已经托管了7个不同的网站。我只是创建了一个新的子域,并将子域virtualhost修改为

<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>
因此,我的wsgitest应用程序在服务器上看起来像这样 /home/account\u username/public\u html/wsgitest/hello.py

最后,在类似so-wsgitest.mysite.com的浏览器上测试该站点 你应该看看“你好,世界”


我希望这能帮助某人卸载系统mod_wsgi包,然后使用
pip install
方法安装mod_wsgi。看见如果您有进一步的问题,请确保您准确地解释了问题的其他难以帮助的细节。@Graham。我遵循了,并且能够找到我的解决方案,建议您使用守护程序模式查看。您通常不会使用mod_wsgi的嵌入式模式。我使用的是守护程序模式。感谢上面的配置没有显示守护程序模式。这是不是意味着从那以后你变了?顺便说一句,不要将
DocumentRoot
设置为所有代码和日志所在的位置。如果您要注释掉
WSGIScriptAlias
,人们可以下载您的代码。在那里创建一个空的
htdocs
目录,并将
DocumentRoot
设置为该目录。
<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>
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]