Attempt to write a readonly database - Django w/ SELinux error

后端 未结 8 1846
粉色の甜心
粉色の甜心 2020-12-07 18:32

I have a CentOS server on which I have Apache, Django, Django CMS and mod_wsgi. My Django project files are stored in the /srv directory and I have SELinux tur

相关标签:
8条回答
  • 2020-12-07 19:03

    You have to add writing rights to the directory in which your sqlite database is stored. So running chmod 664 /srv/mysite should help.

    This is a security risk, so better solution is to change the owner of your database to www-data:

    chown www-data:www-data /srv/mysite
    chown www-data:www-data /srv/mysite/DATABASE.sqlite
    
    0 讨论(0)
  • 2020-12-07 19:15

    Here my solution:

    root@fiq:/home/django/django_project# chmod 777 db.sqlite3
    root@fiq:/home/django/django_project# cd ..
    root@fiq:/home/django# chmod 777 *
    

    Go to <'your_website/admin'> put username and password.. That's it.

    0 讨论(0)
  • 2020-12-07 19:16

    This issue is caused by SELinux. After setting file ownership just as you did, I hit this issue. The audit2why(1) tool can be used to diagnose SELinux denials from the log:

    (django)[f22-4:www/django/demo] ftweedal% sudo audit2why -a
    type=AVC msg=audit(1437490152.208:407): avc:  denied  { write }
          for  pid=20330 comm="httpd" name="db.sqlite3" dev="dm-1" ino=52036
          scontext=system_u:system_r:httpd_t:s0
          tcontext=unconfined_u:object_r:httpd_sys_content_t:s0
          tclass=file permissive=0
        Was caused by:
        The boolean httpd_unified was set incorrectly. 
        Description:
        Allow httpd to unified
    
        Allow access by executing:
        # setsebool -P httpd_unified 1
    

    Sure enough, running sudo setsebool -P httpd_unified 1 resolved the issue.

    Looking into what httpd_unified is for, I came across a fedora-selinux-list post which explains:

    This Boolean is off by default, turning it on will allow all httpd executables to have full access to all content labeled with a http file context. Leaving it off makes sure that one httpd service can not interfere with another.

    So turning on httpd_unified lets you circumvent the default behaviour that prevents multiple httpd instances on the same server - all running as user apache - messing with each others' stuff.

    In my case, I am only running one httpd, so it was fine for me to turn on httpd_unified. If you cannot do this, I suppose some more fine-grained labelling is needed.

    0 讨论(0)
  • 2020-12-07 19:20

    I faced the same problem but on Ubuntu Server. So all I did is changed to superuser before I activate virtual environment for django and then I ran the django server. It worked fine for me.

    First copy paste

    sudo su

    Then activate the virtual environment if you have one.

    source myvenv/bin/activate

    At last run your django server.

    python3 manage.py runserver

    Hope, this will help you.

    0 讨论(0)
  • 2020-12-07 19:22

    In short, it happens when the application which writes to the sqlite database does not have write permission.

    This can be solved in three ways:

    1. Granting ownership of db.sqlite3 file and its parent directory (thereby write access also) to the user using chown (Eg: chown username db.sqlite3 )
    2. Running the webserver (often gunicorn) as root user (run the command sudo -i before you run gunicorn or django runserver)
    3. Allowing read and write access to all users by running command chmod 777 db.sqlite3 (Dangerous option)

    Never go for the third option unless you are running the webserver in a local machine or the data in the database is not at all important for you.

    Second option is also not recommended. But you can go for it, if you are sure that your application is not vulnerable for code injection attack.

    0 讨论(0)
  • 2020-12-07 19:23

    You can change acls without touching the ownership and permissions of file/directory.

    Use the following commands:

    setfacl -m u:www-data:rwx /home/user/website
    setfacl -m u:www-data:rw /home/user/website/db.sqlite3
    
    0 讨论(0)
提交回复
热议问题