3

I have looked everywhere and tried many suggested solutions, still without the required result: to run a python file from my lamp server. I can not seem to integrate all the pieces of the puzzle ... Complicating the story is that many solutions either use old apache version (<2.4), which changed the config files significantly. No more httpd.conf! so this executing-a-python-script-in-apache2 does not help; But also the python version being > 3 complicates matters.

specs:

  • linux Kubuntu, apache 2.4, python 3.5
  • apache is running
  • website files are in root/var/www/html/, I have sudo access to this folder.
  • apache2 cgi module enabled: a2enmod cgi
  • the python 3.5 path is usr/bin/env python3
  • the python script, simplest of scripts, has been made executable

    #!/usr/bin/env python3
    
    print ("Content-type: text/html\n")
    print ("Hello world!")
    

lets boil it down to the simplest case: I would like to have apache interpret the spark.py script and spit out the html: "Hello world!"

Questions:

  • is the script file correct as is?
  • which config files do I need to change and what do I need to add to these config files?

I know for security reasons, you should not have apache run script in your root dir.

Community
  • 1
  • 1
hewi
  • 1,274
  • 3
  • 17
  • 32
  • Look into `mod_wsgi`, it's much better than plain old CGI. The main Ubuntu archives have a `libapache2-mod-wsgi-py3` package, I'm sure there's one available for Kubuntu as well. – MattDMo Jan 08 '17 at 17:25
  • I am looking into the wsgi modules, but that seems to be even harder to get implemented. Maybe you have a very basic simple example of this? – hewi Jan 20 '17 at 16:57
  • I have google'd it into oblivion, believe me, hence the delay in my response :) don't forget youtube, also quite helpfull, but none of the examples are really explaining a-to-z, where does the wsgi script lives, what are the apache server settings, where does your actual application live? Google "basic python wsgi example" reveals 155,000 results, I just check and I have actually visited aprox 60 links, still not working :( anyway, thanks for your energy, I will post a new question ... – hewi Jan 21 '17 at 19:06

2 Answers2

6

The python documentation for modwsgi seems to fit what you are asking for. The following webpage has a really simple example and the necessary configuration for a python3-apache2 setup.

http://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html

You will need to install the mod_wsgi for the configuration to work. Take note of the different "_" underscore and "-" dash character used in apt and pip3.

$ sudo apt install apache2-dev libapache2-mod-wsgi-py3
$ sudo pip3 install mod_wsgi

libapache2-mod-wsgi-py3 and mod_wsgi seems to be the same thing. However, my test deployment only works after installing mod_wsgi. Could be configuration issue. The following are the details of the configuration I have tested on Ubuntu 16.04.2.

Application file /home/user/wsgi_sample/hello.wsgi:

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]

Apache2 configuration /etc/apache2/sites-available/000-test.conf

<VirtualHost *:80>
  ServerName testmachine
  <Directory /home/user/wsgi_sample>
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>
  WSGIScriptAlias /hello /home/user/wsgi_sample/hello.wsgi
</VirtualHost>

Enable the site in apache2.

sudo a2ensite 000-test.conf

Open your browser to testmachine/hello.

wsgi may also be deployed on Apache2 using passenger. It demands a slighter longer configuration. Ask a new question if passenger/python3 is desired.

Devakhim
  • 111
  • 1
  • 4
  • Welcome to Stack Overflow. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – Fabian Horlacher Apr 12 '17 at 03:16
0

Yes, your minimum code seem correct. The Apache config information is answered here https://stackoverflow.com/a/57531411/4084546

Stan S.
  • 237
  • 7
  • 22