Search code examples
ruby-on-railsgitnginxcapistranocarrierwave

Rails / Carrierwave / GIT / nginx / Capistrano - can't create a directory in git releases folder


I'm using carrierwave in a rails app to upload files. It works fine on my development environment, but on my production VM (Ubuntu), I'm getting this error:

An Errno::EACCES occurred in users#update:

  Permission denied - /home/yards/apps/yardsapp/releases/20130616143623/public/uploads/tmp/20130616-1438-14186-3184
  /usr/local/lib/ruby/1.9.1/fileutils.rb:244:in `mkdir'

I'm pretty sure I understand what is going on, but I can't seem to figure out a fix. My capistrano deploy.rb is set up with the user as root. So when it creates the new release folder on a deploy, the access rights are for root (I think).

Then when I try to upload a file, I get that error because nginx is trying to execute a mkdir as www-data.

I could chown the folder after the deploy and it works...but then another deploy creates another new directory with owner set to root as default.

At least I think this is what is going on. Does anyone have any ideas on how I should be doing this?


Solution

  • Run your deployment as www-data. You might need to adjust the authorized_keys file for the www-data user as well to be able to connect.

    To fastest way would be to copy over your authorized_keys file for whatever user you are using at the moment (assuming you are root):

    mkdir $WWW_DATA_HOME/.ssh
    cp ~/.ssh/authorized_keys $WWW_DATA_HOME/.ssh/authorized_keys
    chown www-data:www-data $WWW_DATA_HOME/.ssh/authorized_keys
    

    You might also need to change the shell for the www-data user to log in to it:

    chsh -s /bin/bash www-data
    

    Now you should be able to do

    ssh [email protected]
    

    and log in.