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

问题描述 投票:0回答:1

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

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

如有任何帮助,我们将不胜感激。

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

python django mod-wsgi
1个回答
11
投票

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 [email protected]

    <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

# hello.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”

我希望这对某人有帮助

© www.soinside.com 2019 - 2024. All rights reserved.