After spending a lot of my time making things harder for myself, I’ve finally started to look at things that might make development that little bit nicer. I’ve been working with Less for CSS work (which I’ll write about soon – It’s so good), various CSS/HTML frameworks / libs, and Git for deploying.
For those not familiar with Git, it is a fantastically solid and simple (but also incredibly powerful) version control system developed by Linus Torvalds. I’ll not go on about it anymore than that, but I’d suggest you look into it if you haven’t already.
Anyway, I thought I’d take us through how to really quickly deploy your site from one repository to another (this could be from localhost to server say).
First things first, this requires the remote server to have both SSH enabled and GIT installed. For me Dreamhost has both of these, so I’m good to start setting up!
1. Log in to your server with SSH – I use terminal on the Mac for this, so for me I type:
This then asks for your pass, go on and type that and hit return.
2. Navigate to your directory – if you need to see a list of files, just type ls -l. You can now travel between folders using the cd (change dir) command & if you want to go back up a folder use cd ../
3. Now create a new git repository
4. Set a few things up for git – Now, just need to set some config stuff and create your post-update hook git needs to deploy your site!
1
| git config receive.denyCurrentBranch ignore |
Now, here is where it looks tricky, but it’s ok – we’re just entering into the text editor VIM, using it to create a file, pasting in some code and saving.
1
| vim .git/hooks/post-update |
Ok, you’re in vim, now go here utsl.gen.nz/git/post-update - this is the post-update hook we need, now just copy and paste. You now need to save this, so press the escape key (this exits the direct editmode) and type
This tells VIM to save and quit, if you think you made a mistake just type :quit! and it wont save. You can try the step again.
Now, just to give it permissions!
1
| chmod +x .git/hooks/post-update |
Right, if all went well you should be all set on your remote server!
5. Add a new remote repository to your local branch – as a dreamhost user I need to type this to get to my folder ‘username@becrux.dreamhost.com/home/username/mydomain.com/folder where ‘username’ is my username. You might need to play around to find your direct path.
1
| git remote add production ssh://username@server.com/home/username/mydomain.com/folder |
6. Right, now to start pushing!
1
| git push production master |
That is it! Assuming you got the paths right, you should have deployed your site!
7. Not finish just yet – because your .git folder contains all revisions and source code, you’ll want to protect that! Fortunately it’s easy; Open up or create an .htaccess file in the main root folder (do this in your local directory, then commit and push after your finished), and type:
order allow,deny deny from all
If your directory doesn’t support <Directory> you can manually add
1
| order allow,deny deny from all |
in your .git folder, but bare in mind you’ll lose this if you ever reclone.
There you go – you’ve just pushed your site from development to live in no time at all – not an ftp client in sight! And remember any time you want to update your site, just git push! You can also create multiple remote repositories for staging etc, just follow the steps again but at 5 change the name, so:
1
| git remote add staging ssh://username@becrux.dreamhost.com/home/username/mydomain.com/folder |
Easy as pie!
Read more