Search code examples
pythonflaskserverxamppcgi

Deploying a Flask application using cgi script in Xampp


I am attempting to deploy a simple Hello World script on XAMMP.

In case the link is removed here is application.py:

from flask import Flask 
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello World!"

My applciation.cgi script is as follows:

#!C:/Users/Simon/Documents/Python/Scripts/python.exe
from wsgiref.handlers import CGIHandler
from application import hello
CGIHandler().run(app)

C:/Users/Simon/Documents/Python/ is the location of my virtual python environment.

I have configured XAMMP (httpd.conf) in the following way:

AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict

This configuration I kept in all cases.

I tried the following as suggested in the documentation:

ScriptAlias /Network C:/Users/Simon/Documents/Network/application.cgi

This failed and I tried this as suggested in here:

<Directory "C:/Users/Simon/Documents/Network">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
</Directory>

Alias /Network "C:/Users/Simon/Documents/Network"

In all cases an Error 500 Server error! was returned.

I'm not sure if it's needed but here is the relevant error showing in access.log:

192.168.0.8 - - [17/Feb/2018:13:21:12 +0000] "GET / HTTP/1.1" 500 1100 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36"

If anything is missing please tell me and I will add it.

How can I correctly configure my server so that I can run this script?


There was mistake in application.cgi:

#!C:/Users/Simon/Documents/Python/Scripts/python.exe
from wsgiref.handlers import CGIHandler
from application import hello
CGIHandler().run(hello)

It is however still returning an error:

A server error occurred. Please contact the administrator.


Solution

  • You've imported the wrong thing in your CGI script; as a result app is not defined. You should be importing that rather than hello.