Sometimes a cheap shared host is all you need, and there is no point spending time and/or money on a VPS. I like Dreamhost because they let you use git and give shell access even with a cheap hosting account. Here’s how.
Enable SSH for your user
Under Users -> Manage Users change the user type to “Shell User”.
Connect to Dreamhost via SSH
Use the SFTP credentials Dreamhost emailed to you. On Windows you can use a client like Putty or Bitvise. On Mac you can use your terminal.
Set up your Keys (optional)
If you want you can skip all of this step and just enter your password when you push.
On your local computer generate a new ssh key (or you can use an existing one). On windows I like to use Git Bash.
Now copy your key and put it on Dreamhost. If you just used the default file location it would look like this:
On windows you can highlight and right-click to copy. Make sure you don’t copy any linebreaks.
Back on your Dreamhost server check if a directory named .ssh exists. If not, create it. Then create a file named authorized_keys and paste the key. I like to use vim. You could also use nano, or even create the file with a text editor and upload it via SFTP.
cd ~ ls -la ## if .ssh does not exist mkdir .ssh ## Create authorized_keys using the editor of your choice. I like vim vim .ssh/authorized_keys ## now in vim - a to insert a ## right-click to paste ## esc, :wq to save and quit esc :wq
Initialize your Dreamhost git repository
On Dreamhost create your git repository. This will be outside of your web directory. Later you can create a hook so that when this repository is updated it is copied to your webroot, making deployment automatic (yay!) You can call the repository your webroot.git. Make sure it ends in .git
From here on out replace “mysite” with your site name.
cd ~ mkdir mysite.com.git cd mysite.com.git git init --bare
Initialize your local git repository (if necessary)
If you don’t already have a local git repository use Git Bash (Windows) or terminal (mac/linux) and go to your project directory. Using git is beyond the scope of this article but here are some basics. Make sure you have a .gitignore in place.
cd mysite git init git add * git commit -m 'initial commit'
Add Dreamhost as a remote to your local git repository
In your local shell (Git Bash or Terminal) still within your project directory.
git remote add dreamhost ssh://[email protected]/~/mysite.com.git
I also like to keep a copy of my repo on Bitbucket or Github.
git remote add origin [email protected]:myname/mysite.git
Push you local repository to Dreamhost
git push -u dreamhost master
Optional, you can also push you work to Github or Bitbucket for easy collaboration.
git push -u origin master
Add a hook to Dreamhost so your code is automatically deployed to your web root
On your Dreamhost Server:
cd ~/mysite.com.git vim hooks/post-receive
Now paste this code into the file you’ve just created. (Remember in vim type a for insert mode, then paste, then esc, type :wq enter). If you’re uncomfortable with vim or nano you can create a file called post-receive and upload it via SFTP.
#!/bin/sh GIT_WORK_TREE=/home/myname/mysite.com git checkout -f
Give the file execute permissions.
chmod +x hooks/post-receive
Now when you push to Dreamhost your website is automatically updated. Woohoo!