WebFaction
Community site: login faq

I have been attempting to deploy a fairly simple Flask app on Webfaction for some time now. I have managed to get the app almost entirely running (web sockets and another dependency acting weird) if I remove any & all aliasing functions (either WSGIScriptAlias or Alias) from my httpd.conf file. This allows it to run but still shows index.py in the URL which I believe to be the root of some other issues, not to mention I simply would like to get the aliasing working i.e. hide this index.py file from the url.

In my deployment process I have been following both http://flask.pocoo.org/snippets/65/ and https://aaronpresley.com/deploying-a-flask-project-on-webfaction/

I am not running it on a virtual machine because it is going to be a short lived app and didn't see the need. My httpd.conf file has minimal changes from the original file generated by Webfaction – these changes correspond to the changes noted in the links included.

Below you can see my file:

ServerRoot "/home/< user >/webapps/< app_name >/apache2"

LoadModule authz_core_module modules/mod_authz_core.so
LoadModule ... (loads a bunch of other modules, unchanged from original)

*Does the log setting*

DirectoryIndex index.py
DocumentRoot /home/< user >/webapps/< app_name >/htdocs

Listen 19600
KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5

WSGIDaemonProcess insta processes=2 threads=12 python-path=/home/< user >/webapps/< app_name >/lib/python2.7
WSGIProcessGroup <app_name>
WSGIRestrictEmbedded On
WSGILazyInitialization On

< Directory /home/<user>/webapps/<app_name>/htdocs/ >
    Options +ExecCGI
    AddHandler wsgi-script .py
    RewriteEngine On
    RewriteBase /
    WSGIScriptReloading On
< /Directory >

I have replaced the above mentioned user and app_name with the proper info. I have tried many iterations of using either WSGIScriptAlias or Alias to get the aliasing working and it consistently breaks the app.

With WSGIScriptAlias I receive: "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."

With Alias I receive: "The requested URL /index.py/ was not found on this server."

I really have no idea whats going on at this point, I've been researching this issue for awhile and can't seem to find any solutions – it doesn't seem to be an issue for other people. Would love any advice that could be passed my way!

asked 21 Mar '16, 23:14

Matt Visco
25210
accept rate: 0%


It sounds like you're not using a WSGI file. This should be the most up-to-date and relevant documentation for deploying Flask under mod_wsgi, and it looks more correct than the other guides: http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/

Generally with mod_wsgi you do not want a Directory section at all, but rather a WSGI file which is pointed at with WSGIScriptAlias (flask's index.py file is not a WSGI file.)

If you are also using a virtualenv, then you would not use the execfile(activate_this,...) trick to activate the virtualenv, but rather python-home in the WSGIDaemonProcess directive. More information is available here: https://docs.webfaction.com/software/mod-wsgi.html#using-a-virtual-environment-with-mod-wsgi

Hope that helps!

permanent link

answered 22 Mar '16, 10:25

ryans ♦♦
5.0k93360
accept rate: 43%

edited 22 Mar '16, 10:30

So I fixed the problem mentioned below by adding back in the following:

Listen 19600
KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5

This makes sense, it configures the server, starts the server, etc.

Now when I restart apache and go to my webpage it's just 404ing and still going to < my_url >/index.py/. I really am unsure what I should be doing about this index.py that keeps getting pulled into the url, it seems to be the source of all my problems.

(22 Mar '16, 20:49) Matt Visco

I've gotten the alias to work if it is not simply / i.e. currently running:

WSGIScriptAlias /< my_app>  /home/< user >/webapps/< my_app >/htdocs/< my_app >.wsgi

This is still not ideal, if you have any insight into how to get

WSGIScriptAlias / /home/< user >/webapps/< my_app >/htdocs/< my_app >.wsgi

working I would love to know!

(22 Mar '16, 21:15) Matt Visco

This is still confusing me. I have tried to follow your instructions and the instruction from the link you attached. I have made a wsgi file which can be found at my < my_app >/htdocs. The file contains the following:

import sys
sys.path.append('/home/< user >/webapps/< my_app >/app')
from main import app as application

In my httpd.conf file I have the following:

ServerRoot "/home/< user >/webapps/< my_app >/apache2"

LoadModule authz_core_module modules/mod_authz_core.so
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 unixd_module      modules/mod_unixd.so
LoadModule access_compat_module modules/mod_access_compat.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/< user >/logs/user/access_< my_app >.log combined
ErrorLog /home/< user >/logs/user/error_< my_app >.log

< VirtualHost * >
    ServerName < server_name >.webfaction.com

    WSGIDaemonProcess < my_app > user=< user > group=< user > threads=5 python-path=/home/< user >/webapps/< my_app >/lib/python2.7
    WSGIScriptAlias / /home/< user >/webapps/< my_app >/htdocs/< my_app >.wsgi

    < Directory /home/< user >/webapps/< my_app >/htdocs >
        WSGIProcessGroup < my_app >
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    < / Directory  >
< / VirtualHost >

User has been replaced with the username associated with webfaction, my_app corresponds to the app name created in web faction, & server_name corresponds to the one generated by webfaction i.e. web< some_number >.

When I try to run this I get the following error:

httpd not running, trying to start
no listening sockets available, shutting down
AH00015: Unable to open logs

It may also be worth noting that index.py is still being appended to my URL – this could simply be because the conf file is working but thought I might note it.

Appreciate your help, hope to have this resolved soon :)

permanent link

answered 22 Mar '16, 18:34

Matt Visco
25210
accept rate: 0%

Most of the published instructions that are out there are pretty old. I would not trust them.

Here's how I do it.

I've got a mod_wsgi/Python app installed from the control panel, named myapp_server.

I've got a Flask project named myapp in a subdirectory of ~/webapps/myapp_server

~/webapps/myapp_server/myapp/__init__.py looks like this:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World from Flask!'

if __name__ == '__main__':
    app.run()

I've got a WSGI handler at ~/webapps/myapp_server/wsgi.py that has this:

from myapp import app as application

My apache2/conf/httpd.conf looks like this:

ServerRoot "/home/me/webapps/myapp_server/apache2"

LoadModule authz_core_module modules/mod_authz_core.so
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 unixd_module      modules/mod_unixd.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/me/logs/user/access_myapp_server.log combined
ErrorLog /home/me/logs/user/error_myapp_server.log

Listen 12345
KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5

WSGIDaemonProcess myapp_server processes=2 threads=12 python-path=/home/me/webapps/myapp_server:/home/me/webapps/myapp_server/lib/python2.7 
WSGIProcessGroup myapp_server
WSGIRestrictEmbedded On
WSGILazyInitialization On

WSGIScriptAlias / /home/me/webapps/myapp_server/wsgi.py

And that's all you need.

permanent link

answered 23 Mar '16, 00:14

seanf
12.2k41836
accept rate: 37%

Thanks for the clear instructions, that worked but I'm still running into this problem where index.py gets appended onto the end of the url if I just type in the base url.

I can go to the root if I append an extra / i.e. < my_domain >//. I can also go to other pages find such as < my_domain >/another_page but if I type in < my_domain >/ it will re-route me to < my_domain >/index.py and 404.

Let me know if you have any insights. Thanks again.

(23 Mar '16, 00:43) Matt Visco

Please open a support ticket so that we may inspect your actual configuration.

(23 Mar '16, 00:52) seanf
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:

×225
×113
×55
×44
×27

question asked: 21 Mar '16, 23:14

question was seen: 2,582 times

last updated: 23 Mar '16, 00:52

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