【问题标题】:How to setup mod_wsgi on a centos 6 server如何在 centos 6 服务器上设置 mod_wsgi
【发布时间】:2018-06-09 08:42:54
【问题描述】:

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

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

任何帮助将不胜感激。

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

【问题讨论】:

  • 卸载系统mod_wsgi包,然后使用pip install方法安装mod_wsgi。见pypi.org/project/mod_wsgi。如果您还有其他问题,请务必详细说明问题的具体细节,否则难以解决。
  • @格雷厄姆。我关注了link,并找到了我的解决方案

标签: python django mod-wsgi


【解决方案1】:

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

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

我希望这对某人有所帮助

【讨论】:

  • 建议你看看使用守护模式。你通常不会使用 mod_wsgi 的嵌入模式。
  • 我使用的是守护模式。谢谢
  • 上面的配置没有显示守护模式。这是否意味着你已经改变了?顺便说一句,不要将 DocumentRoot 设置为所有代码和日志所在的位置。如果您要注释掉WSGIScriptAlias,人们可以下载您的代码。在下面创建一个空的htdocs 目录并将DocumentRoot 设置为它。
猜你喜欢
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 2014-03-14
  • 2011-01-31
  • 2014-03-02
相关资源
最近更新 更多