Getting Flask to Run on Apache on Ubuntu
<p>Having trouble getting Flask to run on Apache. Followed the directions <a href="https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps" rel="nofollow">here</a> to setup mod_wsgi. I put my desired python code into the <code>/var/www/TDAA_reminder/TDAA_reminder/__init__.py</code>. If I manually run the code via <code>sudo python __init__.py</code> and use ngrok it will received a twilio post so I know that code is working. But will not work with the mod_wsgi. The system has auto-generated <code>__init__.pyc</code> so I feel like it is somewhat working maybe, but I am stuck on where to debug next. I have reloaded and restarted apache numerous times. Gone back to ngrok to ensure I hadn't broken anything. Thanks!</p>

<p>The directory structure from the tutorial is a bit off what I would have expected so:</p>

<pre><code>|--------TDAA_reminder
|----------------TDAA_reminder
|-----------------------static
|-----------------------templates
|-----------------------TDAAenv
|-----------------------__init__.py
|----------------flaskapp.wsgi
</code></pre>

<p>Error in twilio is <code>11200 HTTP retrieval failure</code>:</p>

<pre><code>&lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;500 Internal Server Error&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1&gt;Internal Server Error&lt;/h1&gt;
        &lt;p&gt;The server encountered an internal error or
misconfiguration and was unable to complete
your request.&lt;/p&gt;
        &lt;p&gt;Please contact the server administrator at 
 admin@andrewtclaus.com to inform them of the time this error occurred,
 and the actions you performed just before this error.&lt;/p&gt;
        &lt;p&gt;More information about this error may be available
in the server error log.&lt;/p&gt;
        &lt;hr&gt;
            &lt;address&gt;Apache/2.4.7 (Ubuntu) Server at andrewtclaus.com Port 80&lt;/address&gt;
        &lt;/body&gt;
    &lt;/html&gt;
</code></pre>

<p>My hook is directed to <code>http://andrewtclaus.com/TDAA_reminder</code>.</p>

<p>excerpt from <code>/var/www/TDAA_reminder/TDAA_reminder/__init__.py</code>:</p>

<pre><code>app = Flask(__name__)

@app.route('/TDAA_reminder', methods=['POST'])
def TDAA_reminder():
</code></pre>

<p><br>
<code>/etc/apache2/sites-available/TDAA_reminder.conf</code>:</p>

<pre><code>&lt;VirtualHost *:80&gt;
                ServerName AndrewTClaus.com
                ServerAdmin admin@andrewtclaus.com
                WSGIScriptAlias / /var/www/TDAA_reminder/TDAA_reminder.wsgi
                &lt;Directory /var/www/TDAA_reminder/TDAA_reminder/&gt;
                        Order allow,deny
                        Allow from all
                &lt;/Directory&gt;
                Alias /static /var/www/TDAA_reminder/TDAA_reminder/static
                &lt;Directory /var/www/TDAA_reminder/TDAA_reminder/static/&gt;
                        Order allow,deny
                        Allow from all
                &lt;/Directory&gt;
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
&lt;/VirtualHost&gt;
</code></pre>

<p><br>
<code>/var/www/TDAA_reminder/TDAA_reminder.wsgi</code>:</p>

<pre><code>#!/usr/bin/python

activate_this = '/var/www/TDAA_reminder/TDAA_reminder/TDAAenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/TDAA_reminder/")

from TDAA_reminder import app as application
</code></pre>

<p><br>
Apache error codes:</p>

<pre><code>[Sat Sep 03 01:53:23.598479 2016] [:error] [pid 17691] [client 192.0.101.226:16488] mod_wsgi (pid=17691): Target WSGI script '/var/www/TDAA_reminder/TDAA_reminder.wsgi' cannot be loaded as Py$
[Sat Sep 03 01:53:23.598508 2016] [:error] [pid 17691] [client 192.0.101.226:16488] mod_wsgi (pid=17691): Exception occurred processing WSGI script '/var/www/TDAA_reminder/TDAA_reminder.wsgi'.
[Sat Sep 03 01:53:23.598526 2016] [:error] [pid 17691] [client 192.0.101.226:16488] Traceback (most recent call last):
[Sat Sep 03 01:53:23.598542 2016] [:error] [pid 17691] [client 192.0.101.226:16488]   File "/var/www/TDAA_reminder/TDAA_reminder.wsgi", line 11, in &lt;module&gt;
[Sat Sep 03 01:53:23.598565 2016] [:error] [pid 17691] [client 192.0.101.226:16488]     from TDAA_reminder import app as application
[Sat Sep 03 01:53:23.598574 2016] [:error] [pid 17691] [client 192.0.101.226:16488]   File "/var/www/TDAA_reminder/TDAA_reminder/__init__.py", line 37, in &lt;module&gt;
[Sat Sep 03 01:53:23.598639 2016] [:error] [pid 17691] [client 192.0.101.226:16488]     flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
[Sat Sep 03 01:53:23.598658 2016] [:error] [pid 17691] [client 192.0.101.226:16488] AttributeError: 'TwilioRestClient' object has no attribute 'flow_from_clientsecrets'
[Sat Sep 03 01:57:16.917248 2016] [:error] [pid 17688] [client 54.165.223.88:40658] mod_wsgi (pid=17688): Target WSGI script '/var/www/TDAA_reminder/TDAA_reminder.wsgi' cannot be loaded as Py$
[Sat Sep 03 01:57:16.917277 2016] [:error] [pid 17688] [client 54.165.223.88:40658] mod_wsgi (pid=17688): Exception occurred processing WSGI script '/var/www/TDAA_reminder/TDAA_reminder.wsgi'.
[Sat Sep 03 01:57:16.917308 2016] [:error] [pid 17688] [client 54.165.223.88:40658] Traceback (most recent call last):
[Sat Sep 03 01:57:16.917324 2016] [:error] [pid 17688] [client 54.165.223.88:40658]   File "/var/www/TDAA_reminder/TDAA_reminder.wsgi", line 11, in &lt;module&gt;
[Sat Sep 03 01:57:16.917346 2016] [:error] [pid 17688] [client 54.165.223.88:40658]     from TDAA_reminder import app as application
[Sat Sep 03 01:57:16.917370 2016] [:error] [pid 17688] [client 54.165.223.88:40658]   File "/var/www/TDAA_reminder/TDAA_reminder/__init__.py", line 37, in &lt;module&gt;
[Sat Sep 03 01:57:16.917384 2016] [:error] [pid 17688] [client 54.165.223.88:40658]     flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
[Sat Sep 03 01:57:16.917400 2016] [:error] [pid 17688] [client 54.165.223.88:40658] AttributeError: 'TwilioRestClient' object has no attribute 'flow_from_clientsecrets'
</code></pre>

<p>Excerpt related to debugging:</p>

<pre><code>SCOPES = ('https://www.googleapis.com/auth/calendar')
store = file.Storage('cal_auth.json')
creds = store.get()

if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store, flags) \
            if flags else tools.run(flow, store)
CAL = build('calendar', 'v3', http=creds.authorize(Http()))
</code></pre>

<p>Answered in comments via following discussion: </p>

<blockquote>
  <p>what was the error from the Apache error log file? If the guide says
  to have it twice, the guide is technically wrong. It has to be
  /var/www/TDAA_reminder as that is the directory the WSGI script file
  is in. Without it, Apache shouldn't be serving up your WSGI
  application as it would be forbidden. If it works regardless of that,
  you have lax security elsewhere in your Apache configuration which is
  putting your server at a bit of risk as allowing access to anything in
  the file system.</p>
</blockquote>

<p>if I read it right, <code>'TwilioRestClient' object has no attribute 'flow_from_clientsecrets'</code> but when I do <code>python __init__.py</code> it works and and I get no errors when using ngrok to tunnel in so I am confused why that is the error?</p>

<blockquote>
  <p>changed the paths in my file to absolute and it worked!</p>
</blockquote>

python apache ubuntu flask mod-wsgi