A small howto, to use Git to deploy your site, instead of FTP. It’s not that very difficult.
To set this up, you’ll need to have
git installed on both your webhost and your local machine, and SSH access to your webhost.
I’ve seen a few different setups doing simular things, but with this setup we’ll create a bare git repository on the webhost, outside the public folder, and then add a hook to make it update the site.
Below there are some names, like
yoursite.dev, etc… Change to match your own setup.
If you need a good webhost I can recommend the one I’m using: Oderland. The site is in Swedish, but should translate fine with Google translate. They accept customers outside Sweden, and if you have any questions - just send them an email and they’ll help you. All servers have
git installed, and they also have PHP7. Actually, you can switch/select between PHP7 down to 5.2 - if you want/need (in “cPanel”). Most accounts/plans have a 2 week trial period if you just want to test.
Turn you local site/folder into a repository:
git init /srv/www/yoursite.dev/public_html
.gitignore file… I have this for example:
.ht* _* blog/ cgi-bin/ *test*.php info.php index.html
The blog is Octopress and have its own deployment (using
Set up SSH access to your webhost…
Creating the keys:
ssh-keygen -t ed25519 -o -a 100 -f ~/.ssh/id_yoursite_ed25519 -C "$(whoami)@$(hostname)-$(date "+%F")"
ssh-add ~/.ssh/id_yoursite_ed25519 # On OS X you use -M to add it to the keychain. ssh-add -M ~/.ssh/id_yoursite_ed25519
And setup a shortcut in your config file (
Host Foo User webhost-username Hostname yoursite.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_yoursite_ed25519 Port 22
And copy the public key to the webhost server:
ssh-copy-id -i ~/.ssh/id_yoursite_ed25519.pub Foo
Log in to your webhost:
To not mess up the home folder - put the git repository in a “repos” directory. Since
git init will create the directories on the fly, just run:
git init --bare repos/yoursite.git
Now, we need to add the “hook”
# Using nano nano repos/yoursite.git/hooks/post-receive # or with cat cat > repos/yoursite.git/hooks/post-receive
The script to add is:
1 2 3 4 5 6 7
Save it and make it run:
chmod +x repos/yoursite.git/hooks/post-receive
Now you can logout…
Back on the local machine. First thing to do is to add everything.
cd /srv/www/yoursite.dev/public_html git add . git commit -m "Initial commit"
Then add the remote… (I call it “live”)
git remote add live ssh://email@example.com:/home/yoursite.com/repos/yoursite.git
Check your remotes:
git remote -v
And push it. First time you do that it might be good to use the
git push -u live master
…and later on:
git push live master # …or just git push
It’s a really nice setup to work with. And by using the git repo outside the public folder, there’s no need for xtra stuff in the
.htaccess file etc.