Jump to content

Using Git instead of FTP in Shared Hostings


clsource
 Share

Recommended Posts

Hello, 

So I'm working on a PW project in a shared hosting like Hostgator.

For this to work you need ssh access, and of course git.

I always have problems working with FTP, I feel my productivity decreases when using them, 

also you have no control over changes that are made. So a single file deletion could cause a big catastrophe.

The first thing you have to do is cloning the PW repo, and checkout to dev branch  :rolleyes:

Here we clone the repo, use the dev branch. Delete the entire PW git directory and initialize a new one

$ git clone https://github.com/ryancramerdesign/ProcessWire/
$ cd ProcessWire
$ git checkout dev
$ rm -rf .git
$ rm  .gitignore
$ git init
$ git add .
$ git commit -m "Initial Commit"

Note:
The file .gitignore from the dev branch have some files that you want to be commited, like /sites.
I recommend deleting that file and create a new one.

Ok, now we need to configure our remote repo inside the shared hosting.

You need to login using a terminal. Linux and Mac have ssh already installed, if you are in Windows

you can use Putty.

ssh username@domain.com -p 2222
...
Enter your password: *********
Now we need to authorize our computer in the hosting, so we can push the commits.

We need the contents of  ~/.ssh/id_rsa.pub located our computer. A more complete guide for generating such keys is available here.

Then we will copy the contents to ~/.ssh/authorized_keys.
create the authorized_keys in the hosting if not exists using touch ~/.ssh/authorized_keys


The next step is creating a new repo and configure some hooks in our server.
 
$ mkdir www/processwire
$ cd www/processwire
$ git init
$ git config receive.denyCurrentBranch ignore
$ cd .git
$ cd hooks
$ touch post-receive

The contents of post-receive hook should be. (this is a ruby script, but could be any language available on the server)

#!/usr/bin/env ruby
# post-receive

# 1. Read STDIN (Format: "from_commit to_commit branch_name")
from, to, branch = ARGF.read.split " "

# 2. Only deploy if master branch was pushed
if (branch =~ /master$/) == nil
    puts "Received branch #{branch}, not deploying."
    exit
end

# 3. Copy files to deploy directory
deploy_to_dir = File.expand_path('../')
`GIT_WORK_TREE="#{deploy_to_dir}" git checkout -f master`
puts "DEPLOY: master(#{to}) copied to '#{deploy_to_dir}'"

# 4.TODO: Deployment Tasks
# i.e.: Run Puppet Apply, Restart Daemons, etc

Now we make it executable using chmod

$ chmod +x post-receive

With all that done, we should enable a passwordless login so we can push without a password prompt on every push.

In our local machine look for this file
 

~/.ssh/config

And add this text (Change your-domain.com and port as needed)
 

Host your-domain.com
  Port 2222
  PreferredAuthentications publickey

Now we should go to our project directory and add the remote host with

$ git remote add hosting user@domain.com:www/processwire
$ git push --set-upstream hosting master

This should do the trick for commiting changes and upload files without ftp.

Now we must install Processwire and then pull the changes.

Go to your domain and start PW setup.
once you are ready installing processwire, you need to pull changes that PW installer made.

Login to your hosting via ssh and commit the changes that installer made.
 

$ ssh user@domain.com
$ cd www/processwire
$ git rm $(git ls-files --deleted)  
$ git add . -f
$ git commit -m "Initial PW Setup"

When that is ready go back to your local machine and pull the repo.
 

$ cd Processwire
$ git fetch
$ git reset --hard hosting/master

Now we are finally ready to start working with PW in shared hosts or other machines that have ssh enabled.

simply makes changes like

$ touch site/templates/welcome.php
$ git add .
$ git commit -m "Added welcome.php"
$ git push

Now if you have some problems you can zip the setup using

$ git tag -a v0.0.1 -m "Initial PW Setup"
$ git archive --format=zip --output=pw.zip v0.0.1

And download the file and work from there

Thats all  O0

References:

http://www.arlocarreon.com/blog/git/push-git-repo-into-shared-hosting-account-like-hostgator/

http://ahmadassaf.com/blog/miscellaneous/how-to-use-git-to-host-and-push-projects-on-shared-hosting-services/

http://motorscript.com/setting-up-a-remote-git-repo-on-shared-server-like-hostgator/

http://krisjordan.com/essays/setting-up-push-to-deploy-with-git

http://git-scm.com/book/en/Customizing-Git-Git-Hooks

  • Like 11
Link to comment
Share on other sites

Thanks for posting this. Work flows like this are looked for by many.

I think FTP is still ok when you map it as a drive in windows or mac.

Thanks,

Yes that could make easier working with FTP.

But I think that this approach its more flexible.  :lol:

Link to comment
Share on other sites

  • 2 weeks later...

So I have to use another computer for development

and have to do some additional steps when you are using an existing repo.

First You have to generate id_rsa.pub and add them to the authorized_keys 

in the hosting. (copy and paste the entire new rsa below the old one)

Example if you have 2 RSA

ssh-rsa A521312312123xg7/E6JK1Noo7t4Oqh5AQVSdCQqlXVO0IpMEHK7zefsNj1IBauAOtfK123312inqHV3avzIu2I/dzHWSYXI5pGkdnYkakkckasN1ovQ7FkQ1gUg6OCgKgEPzQyC5/nSmK+zBdOvbYNtY/g99234bt7+VDpoC9dqK9uOcfmdpVYqwertyqTll9JVdf4KESH1ssLYYCHa6WH0yQfDE2rSR/5Edu0YLLFN email@email.com
ssh-rsa ch123312AXx6/MFa7cDtoznUF6ViirJhsL6KOSNKmkt8jjdjjjjj12b5ND8DouaaMtwaQ8j/5554ffaasdfbkWlfv123UwbDLxwCwKGLi++++JGI7aotsOQcL/ea7CS2kov5yS4PsGoSwnRM0D5lK3psGn/pE0V8Pgmg5AvfxfvTHml email@email.com

In your local machine go to .ssh/config and add your hosting info 

HostName hosting.com
	Port 2222
	PreferredAuthentications publickey

now create a new directory, go inside and create a new git repo

$ mkdir my_project
$ cd my_project
$ git init

Then add the remote host
 

$ git remote add hosting user@hosting.com:www/my_project

Pull the repo

$ git pull hosting master

Make some changes
and push them to the hosting

$ git add .
$ git commit -m "Some changes"
$ git push --set-upstream hosting master

Now you can work using simple

$ git push

O0

  • Like 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...