title: Internal Server Error Apache and WSGI (With Flask)
body: <p>(sorry fist question and not yet answered anything, I'll get to it!)</p>
  
  <p>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.</p>
  
  <p>I am following <a href="https://beagle.whoi.edu/redmine/projects/ibt/wiki/Deploying_Flask_Apps_with_Apache_and_Mod_WSGI" rel="nofollow">this tutorial</a> 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:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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('/&lt;page&gt;/',
                   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)
  </code></pre>
  
  <p>And here is the error.log file that is returned. I have read it b</p>
  
  <pre><code>[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 &lt;module&gt;
  [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 &lt;module&gt;
  [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')
  </code></pre>
  
  <p>If you need any more information then I would be happy to provide it, I would appreciate all the help I can get.</p>
  
  <p>EDIT:</p>
  
  <p>Here is my learningflask.wsgi file:</p>
  
  <pre><code>import sys
  sys.path.insert(0, '/home/carwyn/public_html/apps/learningflask')
  from app import app as application
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>&lt;VirtualHost *:8081&gt;
  
          # ---- Configure VirtualHost Defaults ----
  
      ServerAdmin carwynjohnnelson@aol.com 
  
          DocumentRoot /home/carwyn/public_html/http/learningflask/
  
          &lt;Directory /&gt;
                  Options FollowSymLinks
                  AllowOverride None
          &lt;/Directory&gt;
  
          &lt;Directory /home/carwyn/public_html/http/learningflask/&gt;
                  Options Indexes FollowSymLinks MultiViews
                  AllowOverride None
                  Order allow,deny
                  allow from all
          &lt;/Directory&gt;
  
          # ---- Configure WSGI Listener(s) ----
  
          WSGIDaemonProcess learningflask user=www-data group=www-data threads=5
          WSGIScriptAlias /learningflask /home/carwyn/public_html/wsgi/learningflask.wsgi 
  
          &lt;Directory /home/carwyn/public_html/http/learningflask&gt;
                  WSGIProcessGroup learningflask
                  WSGIApplicationGroup %{GLOBAL}
                  Order deny,allow
                  Allow from all
          &lt;/Directory&gt;
  
          # ---- Configure Logging ----
  
      ErrorLog /home/carwyn/public_html/logs/error.log
      LogLevel warn
      CustomLog /home/carwyn/public_html/logs/access.log combined
  
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>SECOND EDIT:</p>
  
  <p>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:</p>
  
  <pre><code>[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 &lt;module&gt;
  [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 &lt;module&gt;
  [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')
  </code></pre>