28

I am trying to run a Flask REST service on CentOS Apache2 using WSGI. The REST service requires a very small storage. So i decided to use SQLite with sqlite3 python package. The whole application worked perfectly well on my local system and on the CentOS server when ran using app.run(). But when i used WSGI to host the application on Apache, i am getting

OperationalError: attempt to write a readonly database

I have checked the permissions of the file. The user and group of the file are set to apache (under which the server is running) using chown andchgrp. Also, the file has rwx permission. Still i am getting read-only database error. Following is what i get by running ls -al on the db file:

-rwxrwxrwx. 1 apache apache 8192 Nov 19 01:39 dbfile.db

My Apache Configuration:

<VirtualHost *>
ServerName wlc.host.com

WSGIDaemonProcess wlcd
WSGIScriptAlias / /var/www/html/wlcd.wsgi
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
<Directory /var/www/html/>
    WSGIProcessGroup wlcd
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
    Require all granted
</Directory>

Shivaprasad Bhat
  • 555
  • 1
  • 6
  • 13

3 Answers3

25

In addition to changing the database file permissions, you need also to change permissions for the directory that hosts the database file. You can try the following command:

chmod 664 /path/to/your/directory/

You can also change the directory's owner as follows:

chown apache:apache /path/to/your/directory/
ettanany
  • 19,038
  • 9
  • 47
  • 63
  • Did you change the directory's ower to `apache:apache`? – ettanany Nov 20 '16 at 11:15
  • The python package of my flask app has permissions `drw-rw-r--` (with apache:apache) and the wsgi script which imports this also has the same. – Shivaprasad Bhat Nov 20 '16 at 11:18
  • Try to add `-R` like `chown -R apache:apache /path/to/your/directory/` – ettanany Nov 20 '16 at 11:21
  • Yes, right. Can you post your Apache configuration? – ettanany Nov 20 '16 at 11:31
  • I read about disabling SELinux here http://stackoverflow.com/questions/24261716/operationalerror-at-attempt-to-write-a-readonly-database?rq=1 Thought i would give a try and so i did. It actually works. But not sure, what other things might go wrong – Shivaprasad Bhat Nov 20 '16 at 13:03
  • Use `WSGIDaemonProcess wlcd user=apache group=apache threads=5`, it might fix your issue. – ettanany Nov 20 '16 at 13:39
  • tried that as well., did not work.. some SELinux rule is interfering i guess. – Shivaprasad Bhat Nov 20 '16 at 15:59
  • Directory permissions should be ``0775`` not ``0664``. Directories would normally always have the execute bit set as well. Setting ``user`` and ``group`` to ``apache`` user on ``WSGIDaemonProcess`` is not required as the daemon process will inherit the Apache user anyway, no need to set it again. If things work without SELinux, then look at moving your application code and database to a different directory where default SELinux policy will allow you to have it and re-enable SELinux. – Graham Dumpleton Nov 20 '16 at 19:37
2

What worked for me (I don't have sudo) was removing the database file and all migrations and starting again, as described here: How do I delete DB (sqlite3) in Django 1.9 to start from scratch?

  • yes, for me i got the database from a git clone and the error was thrown. REmoving it solved the pb – agenis Sep 27 '20 at 12:58
-3

run

sudo chmod 774 path-to-your-djangoproject-directory 

The directory should be the one which contains manage.py and db.sqlite3 files. (the parent container).

Worked in my case.