apache - wsgi - python - 基本示例

hew*_*ewi 5 python apache mod-wsgi

所以我被建议使用wsgi而不是 cgi,所以我尝试使用以下设置来设置它作为一个基本示例,而不使用 Django:

眼镜:

  • linux 库班图、阿帕奇 2.4、Python 3.5
  • apache 正在运行,mod_wsgi 安装并启用
  • 网站文件位于 root/var/www/html/ 中,我可以 sudo 访问该文件夹
  • python 3.5 路径是 usr/bin/env python3
  • python 脚本:“index.py,最简单的脚本,已可执行
  • python 可执行文件位于 root/var/www/scripts 中

问题:

  • 我如何让这个函数吐出它的结果

  • 这个 wsgi 脚本有什么用?我不需要这个,我也不想要任何 wsgi 扩展

  • 我需要引入什么 apache 指令来运行脚本
  • 那么“应用程序”功能在哪里呢?

真的迷失在这个 wsgi 想法中,一些澄清可能会有所帮助

hew*_*ewi 5

所以让我们从我所知道和想要的开始,使用简约的方法:

最有用的信息源自 shellhacks.commodwsgi.readthedocs.io

  • $ sudo apt-get install python3-distutils
  • $ sudo apt-get install apache2-dev
  • 从这里下载最新的 mod-wsgi 模块包并解压
  • $ ./configure --with-python=/usr/local/bin/python3.5
  • $ 制作
  • $ 须藤进行安装
  • $ cd etc/apache2/mods-available/
  • $ SUDO_EDITOR=凯特 sudoedit wsgi.load

LoadModule wsgi_module 模块/mod_wsgi.so

  • $ sudo a2enmod wsgi

  • $ sudo 服务 apache2 重新启动

  • 使用您最喜欢的文本编辑器(在本例中是 Kate)将以下“spark.py”脚本放入 apache 的文档根文件夹(对我来说是 root/var/www/html)

      $ kate /var/www/html/spark.py
    
    Run Code Online (Sandbox Code Playgroud)
def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!\n'
    response_headers = [('Content-type', 'text/plain'),
                  ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]
Run Code Online (Sandbox Code Playgroud)
<VirtualHost *:80>
    #lots and lots of comments
    some actual directives
    like DocumentRoot /var/www/html

    # more comments
    more directives

    # and all the way at the end
    # THE ACTUAL DIRECTIVE
    WSGIScriptAlias / /var/www/html/spark.py
    <Directory /usr/lib/python3.7>
        Require all granted
    </Directory>

</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
  • $ sudo 服务重新启动 apache
  • 浏览到 localhost(如果您在本地 apache 服务器上进行了设置),您应该会看到所有编程历史中最著名的单词,看到这些单词感觉很好吗:)

要做的事情:创建应用程序,将脚本指向该应用程序,...