hewi hewi - 4 years ago 190
Apache Configuration Question

apache - wsgi - python - basic example

So I was advised to use wsgi instead of cgi, so I tried to set it up with following settings for a basic example, without Django:

specs:


  • linux Kubuntu, apache 2.4, python 3.5

  • apache is running, mod_wsgi installed and enabled

  • website files are in root/var/www/html/, I have sudo access to this folder

  • the python 3.5 path is usr/bin/env python3

  • the python script: "index.py, simplest of scripts, has been made executable

  • the python executable lives in root/var/www/scripts



Questions:


  • how do I make this function spit out it's result

  • what's the deal with this wsgi script? I have no need for that, I also do not want any wsgi extensions

  • what apache directive do I need to introduce to run a script

  • So where does the 'application' function live?



Really lost in this wsgi idea, some clarification might help

Answer Source

so let's start with what I know and want, using the minimalistic approach:

most useful info derived from shellhacks.com and modwsgi.readthedocs.io

  • download latest mod-wsgi module package from here and extract
  • $ ./configure --with-python=/usr/local/bin/python3.5
  • $ make
  • $ sudo make install
  • $ cd etc/apache2/mods-available/
  • $ sudo kate 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
    
    </VirtualHost>
    
  • $ sudo service restart apache

  • browse to localhost (if you are set up on the local apache server) and you should see most famous words in all programming history and man does it feel good to see those :)

Things to do: create app, point the script to that, ...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download