CentOS7下Apache中WSGI配置

  1. 安装Apache服务器
  • yum install httpd (安装Apache服务器)
  • yum install httpd-devel (httpd-devel用于mod_wsgi编译,后文编译时指定–with-apxs的参数便来自这里)
  1. 安装Python
  • wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
  • tar -xzf Python-3.6.5.tgz
  • yum install openssl-devel -y
  • yum install gcc
  • ./configure –prefix=/usr/local/python3 –enable-shared (需要添加 –enable-shared参数,否则mod_wsgi编译时会出现错误 apxs:Error: Command failed with rc=65536)
  • make
  • make install
  1. 安装mod_wsgi
(1)方法一:从源码安装
  • https://github.com/GrahamDumpleton/mod_wsgi/releases/下载mod_wsgi源代码,此处下载了mod_wsgi 4.6.4
  • tar xvfz mod_wsgi-4.6.4.tar.gz
  • ./configure –with-apxs=/usr/bin/apxs –with-python=/usr/local/python3/bin/python3
  • make
  • make install
  • 在/etc/httpd/conf.modules.d目录下新建文件02-wsgi.conf,在该文件中加入 LoadModule wsgi_module modules/mod_wsgi.so
(2)方法二:使用pip安装
  • /usr/local/python3/bin/pip3 install mod_wsgi
  • mod_wsgi-express module-config 获取mod_wsgi配置信息,例如:
LoadModule wsgi_module “/usr/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so”
WSGIPythonHome “/usr”
  • 在/etc/httpd/conf.modules.d目录下新建文件02-wsgi.conf,在该文件中加入一行配置信息,即LoadModule wsgi_module “/usr/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so”
  1. Apahce中配置WSGI
  • 在/etc/httpd/conf.d目录下新建文件wsgi.conf,内容如下:
WSGIScriptAlias /test /var/www/wsgi/wsgi.py
WSGIPythonHome /usr
WSGIPythonPath /var/www/wsgi
<Directory /var/www/wsgi>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
  • 在/var/www/目录下新建文件夹wsgi,并在其下添加文件wsgi.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]
  • 启动Apache,并在浏览器中输入http://localhost:80/test访问

Add a Comment

电子邮件地址不会被公开。 必填项已用*标注