How I Deploy Websites
I’ve gotten into the habit of using a Virtual Machine to develop my websites. The VM mimics the production server as much as possible so there are little-to-no changes that need made when the site is moved to production.
To move a site from the development server to the production one, I wanted a solution that was simple and not obtrusive to my normal workflow.
I tried Capistrano and was able to get it working with a few of my PHP projects. It worked great and I really liked how extendible it was, but I thought it took too much work integrate it into a new project.
My Solution
In the end, I settled on Git and a simple shell script:
#!/bin/sh
LOCALHOME=/local/path/to/project
REMOTEHOME=/remote/path/to/project
WEBROOT=public_html
SERVER=remote_server
USERNAME=jtopjian
GITREPO=/path/to/project.git
ssh $USERNAME@$SERVER "cd $GITREPO; git archive master | tar -x -C $REMOTEHOME"
This script assumes that your remote Git repo is on the same server as the website you’re creating. If it isn’t, you’ll need to make a few minor changes.
I use git archive because this ensures that only the latest version of the project is deployed to production and doesn’t copy any meta-data.
If you use Zend or any other framework where you specify DEVELOPMENT or PRODUCTION configuration settings, you can easily switch from DEV to PROD with sed. Append this to the last line:
ssh $USERNAME@$SERVER "cd $REMOTEHOME/$WEBROOT; sed -i -e 's/development/production/' index.php"
The Whole Workflow
So now, my whole workflow moves like this:
- Write code
Update git:
git add .git commit -m "Summary"git push
Deploy:
./deploy.sh
Conclusion
I’m curious to know other types of deployment methods. If you have one, please leave a comment!
(kind of new to git, svn) btw, I just finished your book Unix for the Beginning Mage (it’s your right?) In any case, you’re name is on there. Finally, I found a teacher/writer who speak my language. The analogy is like super glue in my brain. So, it left me hoping that you have written the same kind of stuff for OOP PHP and other stuff. I’m trying to forge myself into a full developer by the end of this year, but I need to feel like I’m some kind character while I do it, and a Mage is not bad at all.
How do I get started with git…this must be the same as svn right? Happily following your blog now
Hi,
Yes, I wrote that book and some of the stuff on unixmages. Unfortunately that’s all that was ever written in that style. I’m glad you found some use out of it, though!
git is pretty easy to work with. gitcasts.com has some free screencasts. The author of that site is also coming out with a book called Pro Git soon.
http://hoth.entp.com/output/git_for_designers.html is also an easy tutorial to start with.
github.com is fun to play with once you’re familiar with the basics.
You don’t need to be an expert to use git. Honestly I’ve never even done a merge with git yet…
Thanks for this tip.. but im having an issue with permissions. Is it just my server setup, or goes GIT alter permission from 644 to 664 for the files?
When I use your script, it works beautifully, but the files (php) are now 664 (group writeable) and my server kicks a 500 error.
Any advice would be wicked cool.
Hello,
I was unable to reproduce this. PHP files on my development server were 644 and after running the script, they were deployed as 644. Maybe it is a umask problem?