Apache - WSGI - Python 基础示例

5

我被建议使用wsgi而不是cgi,所以我尝试使用以下设置来进行基本示例的设置,没有使用Django:

规格:

  • linux Kubuntu,apache 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指令来运行脚本?

  • 'application'函数在哪里?

对于这个wsgi的概念感到非常迷茫,希望能得到一些澄清。


使用FlaskBottle等Web框架构建网页。它们内置服务器,因此您在开始时不需要Apache。 - furas
你有尝试从浏览器访问 index.py 吗?如果有,你得到了什么结果? - John Gordon
@John Gordon:index.py 工作正常。 - hewi
1
@furas: 我喜欢在开车之前了解汽车的工作原理 :) - hewi
https://modwsgi.readthedocs.io/en/master/user-guides/quick-configuration-guide.html - Calamar
显示剩余2条评论
1个回答

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
  • $ make
  • $ sudo make install
  • $ cd etc/apache2/mods-available/
  • $ SUDO_EDITOR=kate sudoedit wsgi.load

LoadModule wsgi_module modules/mod_wsgi.so

  • $ sudo a2enmod wsgi

  • $ sudo service apache2 restart

  • place following 'spark.py' script in the document root folder of apache (which is root/var/www/html for me) using your favorite text editor (which in this case is Kate for me)

      $ kate /var/www/html/spark.py
    
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]
  • add the WSGI script alias directive to the etc/apache2/sites-available/000-default.conf

    $ sudo kate etc/apache2/sites-available/000-default.conf
    
<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>
  • $ sudo service restart apache
  • 浏览到 localhost(如果您在本地apache服务器上设置了) ,您应该会看到所有编程历史上最著名的单词,并且可以看到它们真的感觉很好 :)

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


当我运行make时,出现了Python.h文件未找到的错误,因此我不得不运行sudo apt-get install python3-dev - scrollout

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接