Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git / github and web server deployment configuration

I'm running an Apache web server and was wondering what's the best way to deploy changes (from github) to the web server?

/var/www/ right now is only writable by root.

Should I have my git project directly in /var/www/? (so it creates /var/www/.git/?)

However, when I need to run commands (i.e. sudo git push) wouldn't work (since my ssh keys are not under sudo).

Would I be better off making /var/www/ writable by myself (and not just root)? Or should I add ssh keys to the root user? Or should I do something else entirely?

Thanks.

like image 588
FrankMN Avatar asked Nov 21 '10 02:11

FrankMN


1 Answers

I use rsync to sync the contents of my local machine with the server, and if you're just deploying to one server, then it's pretty simple (and Capistrano is overkill.). I put the following aliases in ~/.bash_profile:

alias eget='rsync -avie ssh [email protected]:sites/example.com/www/ ~/Projects/example/example.com/www/ --exclude .DS_Store --exclude ".git*" --delete-after'
alias edep='rsync -avuie ssh ~/Projects/example/example.com/www/ [email protected]:sites/example.com/www/ --exclude .DS_Store --exclude ".git*" --delay-updates --delete-after'

Then, from the git repo on my local machine. I do:

git commit -am 'commit some changes'
git pull --rebase # pull any new changes from remote (--rebase prevents an unnecessary merge commit.)
eget -n # confirm which files I've changed

If it looks fishy, I could do eget without the -n and then just do a git diff -w. Then, I could do git checkout -- path/to/file for the files I want to keep my changes for. Then, I commit the changes that were on the server that I didn't get yet. This would only happen if the files on the server are changing in a different way than from deployments. Otherwise, you know that your local version is always more up to date than the files on the server and so don't have to worry about overwriting things on the server that you don't yet have on your local. Continue...

edep -n # just see what files will be deployed/updated/etc.
edep # looks good. Deploy for real.

Done!

Check out the rsync(1) Mac OS X Manual Page for more info.

Another option is to use the Git post-receive hook. But, you'll have to install Git on the server to do that. Also, I recommend putting the .git directory outside of your public www directory for security & cleanliness reasons. You can do this with the Git core.worktree configuration option. For example, from ~/git/example.com.git, do git init --bare; git config core.worktree ~/sites/example.com/. That makes ~/git/example.com.git like the .git dir for ~/sites/example.com/.

like image 71
ma11hew28 Avatar answered Sep 21 '22 07:09

ma11hew28