Capistrano Deployment Behind a Proxy
28 Feb 2016It’s best if we can deploy our app into an online VPS. However, sometimes we would still need to deploy our app to a server behind a VPN. In this article, I would like to share how we can setup to ease the process of installing gems, gem bundling and git clone / push in a server behind a VPN
Specify HTTP/HTTPS proxy
In order to use a proxy, we would need to indicate the http/https proxy by exporting all_proxy
, http_proxy
and https_proxy
. To do that, let’s add in the following lines into ~/.bashrc
# ~/.bashrc
export all_proxy=http://<username>:<password>@<ip_address>:<port>
export http_proxy=http://<username>:<password>@<ip_address>:<port>
export https_proxy=http://<username>:<password>@<ip_address>:<port>
Then reload ~/.bashrc
:
. ~/.bashrc
Configure wget to use HTTP/HTTPS proxy
You would very likely need wget to make use of the http/https proxy. To set that up, edit ~/.wgetrc
and add in the following lines
# ~/.wgetrc
use_proxy=yes
http_proxy=http://<ip_address>:<port>
https_proxy=http://<ip_address>:<port>
proxy_user=<proxy_username>
proxy_password=<proxy_user_password>
You can then try to run the following command to see if it works:
wget http://google.com
To temporarily disable a proxy, you can append --no-proxy
after the above command:
wget http://google.com --no-proxy
Configure Git to use HTTP/HTTPS Proxy
Next, we would need to setup our Git so that when we do git clone, or pull, it will go through a proxy. It’s difficult to channel the git protocol through a proxy. However, it’s easier to channel a git request via http/https request. Hence, what we could do is to replace the [email protected]
portion in git clone [email protected]
with git clone https://...
. In order to do that, open ~/.gitconfig
and add the following lines:
# ~/.gitconfig
[credential]
helper = cache --timeout=86400
[url "https://github.com/jameshuynh/SomeProject.git"]
insteadOf = [email protected]:jameshuynh/SomeProject.git
Note: You would need to replace releal project path.
With this, when we run:
git clone [email protected]:jameshuynh/SomeProject.git
Git will know and convert the command to
git clone https://github.com/jameshuynh/SomeProject.git
Hence this git clone
command will go through the http/https proxy. One thing to note is that the first time that you git clone / pull via http/https, git will prompt you for username & password, which you would need to key in. Subsequently, because the line helper = cache --timeout=86400
, git would cache your username & password. In this configuration, I am putting 86400
seconds, but it’s up to you to adjust how long git should remember your username & password.
Configure Gem Installation to use HTTP/HTTPS Proxy
Finally, we would need to setup proxy for our gem install
or bundle install
. In order to do that, edit ~/.gemrc
and add in the following lines:
# ~/.gemrc
http_proxy: http://<username>:<password>@<ip_address>:<port>
https_proxy: http://<username>:<password>@<ip_address>:<port>
That’s all. You are now ready to deploy from capistrano just like you do on a server outside VPN.