WebFaction
Community site: login faq

I have a flask application running on an mod_wsgi 3.4/Python 2.7 application, when trying to access the url I receive an 200 Error:

"The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, [no address given] and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log."

what I am doing wrong?

I have the following file structure and I have already installed all with virtualenv

 ~/webapps/app_case
    |-- requirements.txt
    |-- run.py
    |-- apache2
    |-- bin
    |-- include
    |-- lib
    `-- app                <== Flask App
        |-- __init__.py
        |-- forms.py
        |-- views.py
        |-- static
        |-- templates

run.py

#!flask/bin/python
from app import app
app.run(debug = True)

app/init.py

import os

from flask import Flask
from flask.ext.mail import Mail

app = Flask(__name__)
app.config.from_object('config')
mail = Mail(app)
from app import views

apache2/conf/httpd.conf

ServerRoot "/home/xpostudio4/webapps/app_case/apache2"

LoadModule dir_module        modules/mod_dir.so
LoadModule env_module        modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule setenvif_module   modules/mod_setenvif.so
LoadModule wsgi_module       modules/mod_wsgi.so
LoadModule alias_module      modules/mod_alias.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/username/logs/user/access_app_case.log combined
DirectoryIndex index.py
DocumentRoot /home/username/webapps/app_case/htdocs
ErrorLog /home/username/logs/user/error_app_case.log
KeepAlive Off
Listen 23999
MaxSpareThreads 3
MinSpareThreads 1
ServerLimit 1
SetEnvIf X-Forwarded-SSL on HTTPS=1
ThreadsPerChild 5
WSGIDaemonProcess app_case processes=5 python-path=/home/username/webapps/app_case/lib/python2.7 threads=1
WSGIProcessGroup app_case
WSGIRestrictEmbedded On
WSGILazyInitialization On

WSGIPythonPath /home/username/webapps/app_case/htdocs
WSGIScriptAlias / /home/username/webapps/app_case/run.py

<Directory /home/username/webapps/app_case/htdocs>
    AddHandler wsgi-script .py
    RewriteEngine on
    RewriteBase /
    WSGIScriptReloading on

</Directory>

/htdocs/index.py

import sys

activate_this = '/home/username/webapps/app_case/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

sys.path.append('/home/username/webapps/app_case/htdocs')
from app import app as application

The error received in the logs is the following

Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] mod_wsgi (pid=27906): Target WSGI script '/home/username/webapps/app_case/run.py' cannot be loaded as Python module.
[Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] mod_wsgi (pid=27906): Exception occurred processing WSGI script '/home/username/webapps/app_case/run.py'.
[Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] Traceback (most recent call last):
[Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1]   File "/home/username/webapps/app_case/run.py", line 2, in <module>
[Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1]     from app import app
[Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] ImportError: No module named app

asked 14 Dec '13, 13:46

xpostudio4
128
accept rate: 100%

edited 14 Dec '13, 18:51


I found the error the correct configuration would be. I eliminated run.py, directed the Apache server to the /htdocs/index.py as the SWGI file, on index.py I added the path of the whole app and the app is now instantiated on init.py

~/webapps/app_case
    |-- requirements.txt
    |-- apache2
    |-- bin
    |-- include
    |-- lib
    `-- app                <== Flask App
        |-- __init__.py
        |-- forms.py
        |-- views.py
        |-- static
        |-- templates

app/init.py

import os

from flask import Flask
from flask.ext.mail import Mail

app = Flask(__name__)
app.config.from_object('config')
mail = Mail(app)
from app import views

if __name__ == '__main__':
     #Bind to PORT if defined, otherwise default to 5000.
     port = int(os.environ.get('PORT', 5000))
     app.run(host='0.0.0.0', port=port)

apache2/conf/httpd.conf

ServerRoot "/home/username/webapps/app_case/apache2"

LoadModule dir_module        modules/mod_dir.so
LoadModule env_module        modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule setenvif_module   modules/mod_setenvif.so
LoadModule wsgi_module       modules/mod_wsgi.so
LoadModule alias_module      modules/mod_alias.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/username/logs/user/access_app_case.log combined
DirectoryIndex index.py
DocumentRoot /home/username/webapps/app_case/htdocs
ErrorLog /home/username/logs/user/error_app_case.log
KeepAlive Off
Listen 23999
MaxSpareThreads 3
MinSpareThreads 1
ServerLimit 1
SetEnvIf X-Forwarded-SSL on HTTPS=1
ThreadsPerChild 5
WSGIDaemonProcess app_case processes=5 python-path=/home/username/webapps/app_case/lib/python2.7 threads=1
WSGIProcessGroup app_case
WSGIRestrictEmbedded On
WSGILazyInitialization On

WSGIPythonPath /home/username/webapps/app_case/htdocs
WSGIScriptAlias / /home/username/webapps/app_case/htdocs/index.py

<Directory /home/username/webapps/app_case/htdocs>
    AddHandler wsgi-script .py
    RewriteEngine on
    RewriteBase /
    WSGIScriptReloading on

</Directory>

/htdocs/index.py

import sys

activate_this = '/home/username/webapps/app_case/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

sys.path.append('/home/username/webapps/app_case/') # I did not needed to put htdocs
from app import app as application
permanent link

answered 14 Dec '13, 22:49

xpostudio4
128
accept rate: 100%

What you'll need to do is find out what the actual error is about, which you can see by looking at the application's log at this location:

/home/username/logs/user/error_app_case.log

That should let you know what the real problem is so that you can take the next step

permanent link

answered 14 Dec '13, 18:14

waynek
4254
accept rate: 27%

The error I receive is the following:

Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] mod_wsgi (pid=27906): Target WSGI script '/home/xpostudio4/webapps/suggestions/run.py' cannot be loaded as Python module. [Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] mod_wsgi (pid=27906): Exception occurred processing WSGI script '/home/xpostudio4/webapps/suggestions/run.py'. [Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] Traceback (most recent call last): [Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] File "/home/xpostudio4/webapps/suggestions/run.py", line 2, in <module> [Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] from app import app [Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] ImportError: No module named app [xpostudio4@web425 user]$

(14 Dec '13, 18:48) xpostudio4

The error logs show the following.

Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] mod_wsgi (pid=27906): Target WSGI script '/home/username/webapps/app_case/run.py' cannot be loaded as Python module.

[Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] mod_wsgi (pid=27906): Exception occurred processing WSGI script '/home/username/webapps/app_case/run.py'.

[Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] Traceback (most recent call last): [Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] File "/home/username/webapps/app_case/run.py", line 2, in <module>

[Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] from app import app

[Sun Dec 15 00:47:24 2013] [error] [client 127.0.0.1] ImportError: No module named app

(14 Dec '13, 19:49) xpostudio4

According to the error, you are missing a module named "app". In your Apache httpd.conf file you can set up your Python Path to ensure that you have all the required modules in the Python Path. We have documentation which describes that here: http://docs.webfaction.com/software/python.html#adding-to-sys-path-within-python-and-wsgi-scripts

(14 Dec '13, 22:06) waynek
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×55
×55
×52
×44

question asked: 14 Dec '13, 13:46

question was seen: 6,961 times

last updated: 14 Dec '13, 22:49

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM