11

I'm trying to use delayed_job to schedule tasks using Sqlite3, and it looks like apache isn't able to read my production.sqlite3 file.

Here's my database.yml:

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

Here's the error I am getting (in log/production.log):

ActiveRecord::StatementInvalid (SQLite3::CantOpenException: unable to open database file:) 

I have run RAILS_ENV=production rake db:create and RAILS_ENV=production rake db:migrate. The db/production.sqlite3 file exists, and the db directory and all its subfolders are owned by apache:apache, which is who apache runs as. I'm using Phusion Passenger on Amazon EC2.

4
  • I switched to using PostgreSQL and it seems to run fine. I still don't know why SQLite 3 didn't work. Commented Oct 18, 2012 at 11:41
  • Nope. I gave up and switched to PostgreSQL. Commented Oct 26, 2012 at 4:01
  • Why should user apache:apache read the database file when you are using Phusion Passenger? Did you ever check if the file is physically in production - > db/production.sqlite3? Commented Nov 3, 2012 at 20:45
  • @awenkhh the file existed physically. Commented Nov 4, 2012 at 1:17

3 Answers 3

9

SQLLite works by having the Rails process write to a system file within the Rails directory tree. The Rails process is owned by Apache, which sets a user "apache" and a group "apache" by default. To make it work you would need to give write permissions to the apache user or group on the /db directory.

OR

Configure apache to run with a group already having write permissions to the directory. A good strategy is to create a group of the various processes that may need access to various locations -- for example I have a "deployer" group that the user doing releases would be part of, along with the apache instance. I typically find that having a group that the various process and login users are part of makes life easier (e.g. for looking at server logs), writing uploads or cached files, etc.

AND/OR

Use a real database server like PostgreSQL or MySQL -- they work because they are their own processes that manage their own files. The Rails process (apache, in your case) connects to the database server process on a Unix port. Each server process securely manages only files it knows about.

SQLLite is fine to get started -- super easy and low overhead, but very soon you'll need to run a regular database server on production. And then you'll soon find that things aren't exactly the same between SQLLite and the others, at which point you should just install the same database server on your dev machine.

Sign up to request clarification or add additional context in comments.

1 Comment

I went to do this (on a dummy app in a gem's test folder) and found that db/ was missing entirely. mkdir db fixed it for me.
1

It's because nginx create www-data user, and this user don't have a previlegues to read sqlite3 file and your app...

You need to run commands:

1.sudo chown -R www-data:www-data rails_project/

2.sudo chmod -R 777 rails_project/

And check that you kick off your app in production mode.

2 Comments

This is a complete security fail, please NEVER chmod 777 your files in a production server. Read @Tom Harrison Jr answer.
@jperelli 644 ?
-1

Ran into this issue in an app where everything is owned by root.

Here is how i got solved.

root@se785fall16:/var/www/auth_whateveryousay# chmod -R 0777 db/


root@se785fall16:/var/www/auth_whateveryousay# ls -lad *
drwxr-xr-x 11 root root  4096 Nov 27 17:23 app
drwxr-xr-x  2 root root  4096 Nov 27 17:23 bin
drwxr-xr-x  5 root root  4096 Nov 27 17:23 config
-rw-r--r--  1 root root   130 Nov 27 17:23 config.ru
drwxrwxrwx  3 root root  4096 Nov 27 17:33 db
-rw-r--r--  1 root root   879 Nov 27 17:23 Gemfile
-rw-r--r--  1 root root  6367 Nov 27 17:24 Gemfile.lock
drwxr-xr-x  4 root root  4096 Nov 27 17:23 lib
drwxr-xr-x  2 root root  4096 Nov 27 17:25 log
drwxr-xr-x  2 root root  4096 Nov 27 17:23 public
-rw-r--r--  1 root root   227 Nov 27 17:23 Rakefile
-rw-r--r--  1 root root   898 Nov 27 17:23 README
-rw-r--r--  1 root root 26632 Nov 27 17:23 README.textile
drwxr-xr-x  6 root root  4096 Nov 27 17:23 spec
drwxrwxrwx  5 root root  4096 Nov 27 17:25 tmp
drwxr-xr-x  3 root root  4096 Nov 27 17:23 vendor

Bottom line is : this is premssions issue and you will need to make sure that whoever owns the app wether it be root or non-root, you will just need to give that user read and write access on the database being used chmod -R 0777 db/. Tweak this to fit your own security level.

4 Comments

This is a complete security fail, please NEVER chmod 777 your files in a production server. Read @Tom Harrison Jr answer.
@jperelli thanks for the feedback - The point here is that we only need to change permissions on the impacted folder "db" and not the whole app-folder"Rails project". At the end of the answer, I mentioned " Tweak this to fit your own security level" which implies what you said.
Ok, please put emphasis on security before in your answer, so people are aware to be cautious when applying your solution.
@jperelli. The only reason I wrote this answer is because 1) sudo chmod -R 777 rails_project/ was too broad and can be more catastrophic 2) sudo chown -R www-data:www-data can be misleading and I didn't need to do it in my case. You are absolutely right about the security fail thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.