Internal Server Error Apache and WSGI (With Flask)

11,064

You should not be calling:

app.run()

This is starting Flask's own HTTP server within the Apache process.

There was a reason it was inside of an if() statement checking to see whether __name__ was '__main__'. That was to ensure it was only called when the script was run from command line Python interpreter. You do not want it to be run under Apache.

Share:
11,064
Carwyn Nelson
Author by

Carwyn Nelson

I am a hobbyist programmer, with a main focus on PHP in the back end. I am currently studying a BTEC in Software Development in college as well as the MTA qualification (developer track). I hope to soon find a job in the software industry where I can gain practical experience. I mainly use Linux based operating systems, however I have experience using Mac OSX and Windows. I am currently in love with the Laravel PHP framework, and hope to contribute to the code base in the future.

Updated on June 04, 2022

Comments

  • Carwyn Nelson
    Carwyn Nelson almost 2 years

    (sorry fist question and not yet answered anything, I'll get to it!)

    I am trying to setup Flask with Apache2 and mod_wsgi in an ubuntu 12.04 virtual machine, mainly so that I know how to do it in the future when I come to deploying a Flask application. Just so that you guys know, I am new to Python and Flask, but I am familiar with PHP, and as a consequence general programming practices.

    I am following this tutorial to setup Flask. I have successfully setup the test application defined in the tutorial, however when I try to setup an existing application using the tutorial I get the following error:

    Internal Server Error
        The server encountered an internal error and was unable to complete your request.     
        Either the server is overloaded or there is an error in the application.
    

    Here is my app.py file, I suspect there is an error here but I can't find it, I added in the app run code from the tutorial In hopes it would work but it didn't, I got the same error and I could never get an error log, so I switched back to "app.run()" and when I restart apache I get an error log:

    import flask
    import settings
    
    # Views
    from main import Main
    from login import Login
    from remote import Remote
    from music import Music    
    
    app = flask.Flask(__name__)
    app.secret_key = settings.secret_key
    
    # Routes
    app.add_url_rule('/',
                    view_func=Main.as_view('main'),
                    methods=["GET"])
    app.add_url_rule('/<page>/',
                     view_func=Main.as_view('page'),
                     methods=["GET"])
    app.add_url_rule('/login/',
                     view_func=Login.as_view('login'),
                     methods=["GET", "POST"])
    app.add_url_rule('/remote/',
                     view_func=Remote.as_view('remote'),
                     methods=['GET', 'POST'])
    app.add_url_rule('/music/',
                     view_func=Music.as_view('music'),
                     methods=['GET'])
    
    @app.errorhandler(404)
    def page_not_found(error):
      return flask.render_template('404.html'), 404
    
    #app.debug = True
    app.run()
    
    #if __name__ == '__main__':
        #"Are we in the __main__ scope? Start test server."
        #app.run(host='0.0.0.0',port=5000,debug=True)
    

    And here is the error.log file that is returned. I have read it b

    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1] mod_wsgi (pid=5739): Target WSGI script '/home/carwyn/public_html/wsgi/learningflask.wsgi' cannot be loaded as Python module.
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1] mod_wsgi (pid=5739): Exception occurred processing WSGI script '/home/carwyn/public_html/wsgi/learningflask.wsgi'.
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1] Traceback (most recent call last):
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/home/carwyn/public_html/wsgi/learningflask.wsgi", line 3, in <module>
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     from app import app as application
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/home/carwyn/public_html/apps/learningflask/app.py", line 35, in <module>
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     app.run()
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     run_simple(host, port, self, **options)
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 710, in run_simple
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     inner()
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 692, in inner
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     passthrough_errors, ssl_context).serve_forever()
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 436, in serve_forever
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     HTTPServer.serve_forever(self)
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]   File "/usr/lib/python2.7/SocketServer.py", line 225, in serve_forever
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1]     r, w, e = select.select([self], [], [], poll_interval)
    [Sat Jun 29 11:59:23 2013] [error] [client 127.0.0.1] error: (4, 'Interrupted system call')
    

    If you need any more information then I would be happy to provide it, I would appreciate all the help I can get.

    EDIT:

    Here is my learningflask.wsgi file:

    import sys
    sys.path.insert(0, '/home/carwyn/public_html/apps/learningflask')
    from app import app as application
    

    And here is my available-sites apache file-thing (called learningflask), it pretty much just follows the tutorial one but is set up for the tutorial application:

    <VirtualHost *:8081>
    
            # ---- Configure VirtualHost Defaults ----
    
        ServerAdmin [email protected] 
    
            DocumentRoot /home/carwyn/public_html/http/learningflask/
    
            <Directory />
                    Options FollowSymLinks
                    AllowOverride None
            </Directory>
    
            <Directory /home/carwyn/public_html/http/learningflask/>
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride None
                    Order allow,deny
                    allow from all
            </Directory>
    
            # ---- Configure WSGI Listener(s) ----
    
            WSGIDaemonProcess learningflask user=www-data group=www-data threads=5
            WSGIScriptAlias /learningflask /home/carwyn/public_html/wsgi/learningflask.wsgi 
    
            <Directory /home/carwyn/public_html/http/learningflask>
                    WSGIProcessGroup learningflask
                    WSGIApplicationGroup %{GLOBAL}
                    Order deny,allow
                    Allow from all
            </Directory>
    
            # ---- Configure Logging ----
    
        ErrorLog /home/carwyn/public_html/logs/error.log
        LogLevel warn
        CustomLog /home/carwyn/public_html/logs/access.log combined
    
    </VirtualHost>
    

    SECOND EDIT:

    I hope this might help, I removed the app.run and reloaded the page and I get the error. Then I looked in my previously cleared error log and get nothing. So i restarted apache and looked in my error log and I get this:

    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1] mod_wsgi (pid=5908): Target WSGI script '/home/carwyn/public_html/wsgi/learningflask.wsgi' cannot be loaded as Python module.
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1] mod_wsgi (pid=5908): Exception occurred processing WSGI script '/home/carwyn/public_html/wsgi/learningflask.wsgi'.
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1] Traceback (most recent call last):
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/home/carwyn/public_html/wsgi/learningflask.wsgi", line 3, in <module>
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     from app import app as application
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/home/carwyn/public_html/apps/learningflask/app.py", line 35, in <module>
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     #app.run()
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     run_simple(host, port, self, **options)
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 710, in run_simple
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     inner()
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 692, in inner
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     passthrough_errors, ssl_context).serve_forever()
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 436, in serve_forever
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     HTTPServer.serve_forever(self)
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]   File "/usr/lib/python2.7/SocketServer.py", line 225, in serve_forever
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1]     r, w, e = select.select([self], [], [], poll_interval)
    [Sat Jun 29 21:33:19 2013] [error] [client 127.0.0.1] error: (4, 'Interrupted system call')