Hosting your own Ghostbin

Acknowledgments

First of all, I'd like to thank Dustin Howett, Ghostbin's creator, his help while I was trying to install Ghostbin on my server. That has helped me to write this post.


Disclaimer

This post is translated from its original version in Spanish. As I'm not a native English speaker, I apologize in advance for any mistake I can make.


Across the internet you can find a lot of paste services available, being Pastebin one of the most expanded ones. But more expanded does not always mean best.
In example, I love Ghostbin, because of its friendly interface, being able to edit a paste even after being shared, not requiring Captchas after a long paste...

And as everyone knows, if there's anything you can do in order to make a service even cooler, is hosting it yourself.

For some time I've been looking for an open-source paste service which allowed me to host it myself, and after having a look to some projects, such as Haste or ZeroBin, I realized by reading the Ghostbin's "About" page that... It was already open-source.

So, in this post I'll try to exaplin how to host your own Ghostbin in your own server, as a little help for everyone like mi, that has no precious experience in Go.

Requirements

In order to enjoy your own Ghostbin, you will need:

  • A server to host it
  • git, bazaar and Mercurial installed
  • Go installed
  • Some patience

Installing bazaar

For me it was not an easy way, because by default Bazaar does not come at the default apt packages, and I didn't find a release marked working for Ubuntu 14.04 trusty. Finally, I solved it by adding the following lines at the end of my /etc/apt/sources.list:

deb http://ppa.launchpad.net/bzr/ppa/ubuntu trusty main
deb-src http://ppa.launchpad.net/bzr/ppa/ubuntu trusty main

One added, just update the sources and install bazaar:

apt-get update
apt-get install bzr

Installing Go

I you are like me, that never before had worked with Go, this step might be a little complex, so I'm going to illustrate as slow as I can how I got Go working on a Ubuntu 14.04 server.

Installing the language

sudo apt-get install golang

Everything fine until here, right?
This command will install nearly all the Go environment on our system.

Environment variables

While installing, Go does not set automatically the environment variable that will set our workspace location, so we will have to set it manually.

Go works with a workspace, if you have ever worked with Eclipse, you can think about it in the same way: Every Go project will be under the same directory:

	GOPATH
        |-- bin
        |-- pkg
        |-- src <-

Inside the src/ folder we will clone our projects.

It's common to set the workspace at $HOME/go, so let's go.
For setting the variable, we'll open our ~/.bashrc with our favorite editor, and append the next lines at the end of the file:

# GO Environment
export GOPATH=$HOME/go

Run source ~/.bashrc so our active session loads the file changes, and go through to the next step.

Installing and configuring Ghostbin

Cloning the project

Ghostbin's GitHub repo is hosted at the following link:
https://github.com/DHowett/ghostbin

Place your session at $HOME/go/src and create a folder called github.com (its a common practice having the packages classified into namespaces.
So, the commands would be the following:

cd $HOME/go
cd src
mkdir github.com
cd github.com
git clone https://github.com/DHowett/ghostbin.git
cd ghostbin

One inside the project's folder, as a personal recommendation from the author, we will work with the development branch, whose development is ahead from the master one (its last commit was about 6 months ago).

For changing the current branch we only need to run:

git checkout development

And a message will prompt, showing us that we've successfully changed the current branch.

Installing dependencies

Once the project is cloned and we are placed at the development branch, we only need to install the required dependencies.

For doing that, we run the following commands:

go get
go install

In my case, when I did it I got an error complaining about a dependence was missing. In order to solve it, I needed to run the following:

go get gopkg.in/russross/blackfriday.v1
go install

Compiling the project

Once we have the dependencies correctly installed, there's only one more step: compiling the project.
The command we need to run for starting the compilation is:

go build

Now sit and watch how all the packages compile and give an executable file called ghostbin as a result.

Also, you will find a copy of that executable at $HOME/go/bin.

Configuring nginx

After having our Ghostbin copy compiled, we'll need to configure nginx for redirecting all incoming connections to the desired port.
For doing that, we'll need to choose an address and a port (by default is 0.0.0.0:8080) which we will configure at our nginx server script.

It's recommendable the address is localhost or 127.0.0.1. The port is totally up to you.

Create a file at /etc/nginx/sites-available/ called ghostbin, and paste the following content:

# Upstream configuration
upstream ghostbin_upstream {
     server ADDRESS:PORT;
     keepalive 64;
 }


# Public
server {
    listen 80;
    server_name ghostbin.YOURDOMAIN.com; # domain of my site

   location / {
        proxy_http_version 1.1;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   X-NginX-Proxy    true;
        proxy_set_header   Host             $http_host;
        proxy_set_header   Upgrade          $http_upgrade;
        proxy_redirect     off;
        proxy_pass         http://ghostbin_upstream;
    }
}

Once created, we create the symbolic link and restart nginx:

ln -s /etc/nginx/sites-available/ghostbin /etc/nginx/sites-enabled/ghostbin
service nginx restart

Also, make sure you create the A record on your DNS manager.

Running Ghostbin

Place your terminal at $HOME/go/bin, and if you make a ls you should see an executable file called ghostbin.
If you run ./ghostbin -h you can see all the arguments you can pass to the executable for configuring the server options (and their default configuration):

For running it according the values set at our nginx script, just run

./ghostbin -addr="ADDRESS:PORT"

Despite not seeing any confirmation message telling us that the process has been started, we should be able to access ghostbin.YOURDOMAIN.com and see how our fresh Ghostbin copy loads in our browser.

NOTA

The original Ghostbin author, Dustin Howett, told me the parameters he sets when running Ghostbin on his server:
-addr="127.0.0.1:8619" -root="/srv/ghostbin" -log_dir="/srv/ghostbin/log"

Extra: Ghostbin as a background service

If you are planning to run your own Ghostbin running in background, you can do it easily by creating a script at /etc/init

You only need to create /etc/init/ghostbin.conf and paste the following content:

# Force the service to start at the server startup
start on startup

# Script begining
script
cd $HOME/go/bin
./ghostbin -addr="ADDRESS:PORT" # Set the rest of your parameters
end script

Now we should be able to run

sudo service ghostbin start

And voila! Enjoy your own Ghostbin hosted at your own server