Zach Dobbs
Zach Dobbs

Reputation: 63

Flask: OperationalError: unable to open database file

I am having immense difficulties attempting to get my Flask application's database working. I am following the sample tutorial from Flask and deploying my site on Apache server through an Amazon EC2 instance. I have no difficulties accessing the site, but whenever I try to post to the database I get a 500 Internal Server Error. Checking error.log shows:

[Wed Sep 13 19:37:47.713249 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492] [2017-09-13 19:37:47,712] ERROR in app: Exception on /add [POST], referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713291 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492] Traceback (most recent call last):, referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713294 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app, referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713296 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492]     response = self.full_dispatch_request(), referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713299 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request, referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713301 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492]     rv = self.handle_user_exception(e), referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713304 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1517, in handle_user_exception, referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713306 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492]     reraise(exc_type, exc_value, tb), referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713308 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1612, in full_dispatch_request, referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713310 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492]     rv = self.dispatch_request(), referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713312 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1598, in dispatch_request, referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713314 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492]     return self.view_functions[rule.endpoint](**req.view_args), referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713330 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492]   File "/var/www/FlaskApp/FlaskApp/FlaskApp.py", line 76, in add_entry, referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713333 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492]     [request.form['title'], request.form['text']]), referer: http://www.zachflask.tk/
[Wed Sep 13 19:37:47.713334 2017] [wsgi:error] [pid 27587] [client 209.54.86.83:50492] OperationalError: unable to open database file, referer: http://www.zachflask.tk/

I thought this may have something to do with my file permissions, but I chmod 777 FlaskApp.db without any outcomes.

zld6fd@vanadium:/var/www/FlaskApp/FlaskApp$ ls -la
total 44
drwxr-xr-x 5 zld6fd zld6fd 4096 Sep 13 19:45 .
drwxr-xr-x 6 zld6fd zld6fd 4096 Sep 13 19:37 ..
-rwxrwxrwx 1 zld6fd zld6fd 3072 Sep 13 19:18 FlaskApp.db
-rw-rw-r-- 1 zld6fd zld6fd 3194 Sep 13 19:16 FlaskApp.py
-rw-rw-r-- 1 zld6fd zld6fd 3943 Sep 13 19:16 FlaskApp.pyc
-rw-r--r-- 1 zld6fd zld6fd   26 Sep 12 20:39 __init__.py
-rw-r--r-- 1 zld6fd zld6fd  177 Sep 12 21:25 __init__.pyc
-rwxrwxrwx 1 zld6fd zld6fd  145 Sep 12 21:33 schema.sql
drwxr-xr-x 5 zld6fd zld6fd 4096 Sep 12 22:14 static
drwxr-xr-x 2 zld6fd zld6fd 4096 Sep 13 00:34 templates
drwxr-xr-x 7 zld6fd zld6fd 4096 Sep 11 23:32 venv

My FlaskApp.py file contains the following:

# Import statements
import os
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, \
        render_template, flash

# Application Instance
app = Flask(__name__) # Create the instance
app.config.from_object(__name__) # Load config from this file

# Load default config and override config from an environment variable
app.config.update(dict(
        DATABASE='/var/www/FlaskApp/FlaskApp/FlaskApp.db',
        SECRET_KEY='development key',
        USERNAME='admin',
        PASSWORD='password'
))

And my Apache conf file is:

<VirtualHost *:80>
            ServerName zachflask.tk
            ServerAdmin [email protected]

            WSGIDaemonProcess FlaskApp user=zld6fd group=zld6fd threads=5
            WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
            <Directory /var/www/FlaskApp/FlaskApp>
                    WSGIProcessGroup FlaskApp
                    WSGIApplicationGroup %{GLOBAL}
                    Require all granted
            </Directory>
            Alias /static /var/www/FlaskApp/FlaskApp/static
            <Directory /var/www/FlaskApp/FlaskApp/static/>
                    Require all granted
            </Directory>
            ErrorLog /var/www/FlaskApp/logs/error.log
            LogLevel warn
            CustomLog /var/www/FlaskApp/logs/access.log combined
</VirtualHost>

Any help would be greatly appreciated.

Upvotes: 0

Views: 2210

Answers (1)

Zach Dobbs
Zach Dobbs

Reputation: 63

The solution was found just as @pvg suggested in the comments. My FlaskApp.db had always been set to the correct permissions, but I failed to update the permissions of the directory it was within. I moved the FlaskApp.db file into a new tmp/ folder and ran sudo chmod 777 tmp

Upvotes: 1

Related Questions