Skip to content

Instantly share code, notes, and snippets.

@colinfrei
Created November 8, 2012 13:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinfrei/4038664 to your computer and use it in GitHub Desktop.
Save colinfrei/4038664 to your computer and use it in GitHub Desktop.
Capistrano recipe to add an htaccess file when deploying
# [...]
set :htaccess_username, "User"
set :htaccess_password, "Password"
#set :htaccess_password_hashed, "zJDC6IeGdG8QU"
before "deploy:create_symlink", "deploy:htaccess_protect"
# [...]
namespace :deploy do
desc "Add .htaccess protection"
task :htaccess_protect do
_cset(:htaccess_username) { abort "Please specify an htaccess username, set :htaccess_username, 'foo'" }
unless exists?(:htaccess_password_hashed)
_cset(:htaccess_password) { abort "Please specify htaccess_password or htaccess_password_hashed" }
set :htaccess_password_hashed, "#{htaccess_password}".crypt('httpauth')
end
# This appends to the end of the file, so don't run it multiple times!
run "echo '#{htaccess_username}:#{htaccess_password_hashed}' >> #{File.join(current_path, '.htpasswd')}"
run "echo 'AuthType Basic' >> #{File.join(current_path, '.htaccess')}"
run "echo 'AuthName \"Restricted\"' >> #{File.join(current_path, '.htaccess')}"
run "echo 'AuthUserFile #{File.join(current_path, '.htpasswd')}' >> #{File.join(current_path, '.htaccess')}"
run "echo 'Require valid-user' >> #{File.join(current_path, '.htaccess')}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment