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

Ohi*_*rge 3 python django mod-wsgi

我最近完成了django 2应用程序的开发,部分要求是将其部署到centos计算机上的apache 2服务器上。django应用程序是使用python3编写的,并且centos计算机已预先安装了python2。

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

任何帮助,将不胜感激。

编辑 我已经解决了问题。请参阅下面的答案

Ohi*_*rge 5

1.将python 3.6.5全新安装到usr / local和usr / local / lib

我确保YUM是最新的:

$ yum update
Run Code Online (Sandbox Code Playgroud)

编译器和相关工具:

$ yum groupinstall -y "development tools"
Run Code Online (Sandbox Code Playgroud)

启用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
Run Code Online (Sandbox Code Playgroud)

安装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
Run Code Online (Sandbox Code Playgroud)

2.使用pip install安装了mod_wsgi

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

$python3.6 -m pip install mod_wsgi
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

4.重新启动Apache Server并确认已加载mod_wsgi

$apachectl restart
$apachectl -M
Run Code Online (Sandbox Code Playgroud)

查看列表,并确保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>
Run Code Online (Sandbox Code Playgroud)

修改virtualhost后重启apache

根据该网站上创建一个hello.py WSGI应用- http://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html

你好

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]
Run Code Online (Sandbox Code Playgroud)

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

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

我希望这可以帮助别人