'First deploy fails in Capistrano 3 with a /var/www/XXX permission error

I am running the first deploy to a machine via cap staging deploy. As of Cap3, deploy:setup is no longer needed. Yet, strangely, I get a mkdir permissions error:

 INFO[cb348f12] Running /usr/bin/env mkdir -pv /var/www/myapp/shared /var/www/myapp/releases on ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com
 DEBUG[cb348f12] Command: /usr/bin/env mkdir -pv /var/www/myapp/shared /var/www/myapp/releases
 DEBUG[cb348f12]    mkdir: 
 DEBUG[cb348f12]    cannot create directory ‘/var/www’
 DEBUG[cb348f12]    : Permission denied
 DEBUG[cb348f12]    
 DEBUG[cb348f12]    mkdir: 
 DEBUG[cb348f12]    cannot create directory ‘/var/www’
 DEBUG[cb348f12]    : Permission denied
 DEBUG[cb348f12]    
 cap aborted!
 SSHKit::Runner::ExecuteError: Exception while executing on host ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com: mkdir exit status: 1

Of course, I could go an create and chmod that directory myself, but that's not the point. Cap3 is supposed to take care of that itself. I'm confused why it doesn't do that. I should also note that I had this same setup succeed before with another machine (though that was "production" environment).

Here are some basic settings in my deploy.rb:

 set :application, "myapp"
 set :user, 'ec2-user'


Solution 1:[1]

Instead of creating the following directory structure: /var/www/myapp/shared/var/www/myapp/releases try: /var/www/myapp/releases

Solution 2:[2]

EDIT: Re-reading your question, I found that you already know how to create folder on server and set permission.

Cap3 is supposed to take care of that itself. I'm confused why it doesn't do that

Nope. Cap3 cannot create a folder on server automatically, if the user provided to Cap3 does not have previledges to create the folder. There is no magic here just unix permissions.

In your case the user your have provided ec2-user does not have permission to create folder in /var which causes the error.


Original answer

Check whether /var/www folder exists on your server. If it does not, then create that folder and set its ownership to ec2-user so that Capistrano can create any other folders as necessary during the deployment.

sudo mkdir -p /var/www sudo chown ec2-user:ec2-user /var/www

With the above commands run, try deploying again with capistrano. Permission error should now be gone.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user3770862
Solution 2