title: Custom flask application running fine on localhost but returning 500 response to external visitors
body: <p><strong><em>Latest update</strong>: The problem had indeed to do with permission and user groups, today I learned why we do not simply use root for everything. Thanks to <a href="https://stackoverflow.com/users/397331/jakub-p">Jakub P.</a> for reminding me to look into the apache error logs and thanks to <a href="https://stackoverflow.com/users/2712644/domoarrigato">domoarrigato</a> for providing helpful insight and solutions.</em></p>
  
  <hr>
  
  <p>What's up StackOverflow.</p>
  
  <p>I followed the <a href="https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps" rel="nofollow noreferrer">How To Deploy a Flask Application on an Ubuntu VPS</a> tutorial provided by DigitalOcean, and got my application to successfully print out <code>Hello, I love Digital Ocean!</code> when being reached externally by making a <code>GET</code> request to my public server IP.</p>
  
  <p>All good right? Not really.</p>
  
  <p>After that, I edit the tutorial script and write a custom flask application, I test the script in my personal development environment and it runs without issue, I also test it on the DigitalOcean server by having it deploy on localhost and making another <code>GET</code> request.</p>
  
  <p>All works as expected until I try to access it from my public DigitalOcean IP, now suddenly I am presented with a <code>500 Internal Server Error</code>.</p>
  
  <p>What is causing this issue, and what is the correct way to debug in this case?</p>
  
  <h2>What have I tried?</h2>
  
  <ul>
  <li><p>Setting <code>app.debug = True</code> gives the same <code>500 Internal Server Error</code> without a debug report.</p></li>
  <li><p>Running the application on the localhost of my desktop pc and DigitalOcean server gives no error, the script executes as expected.</p></li>
  <li><p>The tutorial code runs and executed fine, and making a <code>GET</code> request to the Digital Ocean public IP returns the expected response.</p></li>
  <li><p>I can switch between the tutorial application and my own application and clearly see that I am <em>only</em> getting the error wit my custom application. However the custom application still presents <em>no</em> issues running on localhost.</p></li>
  </ul>
  
  <h2>My code</h2>
  
  <pre><code>from flask import Flask, request, redirect
  from netaddr import IPNetwork
  import os
  import time
  
  app         = Flask(__name__)
  APP_ROOT    = os.path.dirname(os.path.abspath(__file__))
  
  # Custom directories
  MODULES     = os.path.join(APP_ROOT, 'modules')
  LOG         = os.path.join(APP_ROOT, 'log')
  
  
  def check_blacklist(ip_adress):
      ipv4 = [item.strip() for item in open(MODULES + '//ipv4.txt').readlines()]
      ipv6 = [item.strip() for item in open(MODULES + '//ipv6.txt').readlines()]
  
      for item in ipv4 + ipv6:
          if ip_adress in IPNetwork(item):
              return True
  
          else:
              pass
  
      return False
  
  
  @app.route('/')
  def hello():
      ip_adress   = request.environ['REMOTE_ADDR']
      log_file    = open(LOG + '//captains_log.txt', 'a')
  
      with log_file as f:
  
          if check_blacklist(ip_adress):
              f.write('[ {}: {} ][ FaceBook ] - {} .\n'
                      .format(time.strftime("%d/%m/%Y"), time.strftime("%H:%M:%S"), request.environ))
              return 'Facebook'
  
          else:
              f.write('[ {}: {} ][ Normal User ] - {} .\n'
                      .format(time.strftime("%d/%m/%Y"), time.strftime("%H:%M:%S"), request.environ))
              return 'Normal Users'
  
  
  if __name__ == '__main__':
      app.debug = True
      app.run()
  </code></pre>
  
  <h2>The tutorial code:</h2>
  
  <pre><code>from flask import Flask
  app = Flask(__name__)
  @app.route("/")
  def hello():
      return "Hello, I love Digital Ocean!"
  if __name__ == "__main__":
      app.run()
  </code></pre>