Self hosting hastebin with nginx & docker

0
Share
person using MacBook Pro

Have you ever wanted to post some code or error, but found that it reaches the message limit of the platform your using. You then go to the first pastebin you think of. Perhaps hastebin or pastebin its self. However have you ever wanted to use your own pastebin? Its probably not that hard to code your own, however in this tutorial we’ll be self-hosting hastebin.

Installing hastebin

To install hastebin, we first have to download the binaries.

git clone https://github.com/toptal/haste-server.git cd haste-server/
Code language: PHP (php)

Now you can configure it by editing the config.js file. Finally we need to build & run the docker file.

docker build --tag haste-server . docker run --name haste-server-container -p 7777:7777 --env STORAGE_TYPE=file haste-server

To test, if you run the following command, you should get a bunch of html in response:

curl http://localhost:7777
Code language: JavaScript (javascript)

Setting up a nginx reverse proxy

To setup nginx, you also need to setup SSL. I’m going to use cloudflares.

In our root folder we’re going to create a folder called certs. This is the folder where our certificates will be stored. Make sure you have your domain routed through Cloudflare. Read this guide to see how.

First of all, go to your domain and at the top, click on “SSL/TLS”

Picture showcasing where the SSL/TLS button is

Now click on the “Origin Server” menu and create a certificate. You can leave all your settings as it is (including domains). Now in the folder we created earlier, create a file called cert.pem. Paste the contents of the Origin certificate text box into this file and save it. Then create a file called key.pem and paste the contents of your private key into that file. Finally, make sure your domain that you want (e.g. nucker.me or blog.nucker.me) is pointing towards your IP (in an A record) and is proxied.

Step four: Installing and setting up nginx

We’re gonna first off install Nginx. We can do this by running the following:

sudo apt install nginx
Code language: Bash (bash)

Now navigate to /etc/nginx/sites-enabled/ and delete the file in there (rm ./*). Create a new file called default (nano default) and paste the following into it:

server { server_name paste.yourdomain.com; # Change this to your domain listen 443 ssl; location / { proxy_pass http://127.0.0.1:7777; # Check that your hastebin is running on this port proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } ssl_certificate /home/user/certs/cert.pem; # Change this to your certs folder path ssl_certificate_key /home/user/certs/key.pem; # And this ssl on; }
Code language: Nginx (nginx)

Make sure to change the server_name to the address it will be accessed from; change proxy_pass to the address of the hastebin installation and ssl_certificate and ssl_certificate_key to the correct paths.

Now run:

nginx -t

You might get a couple warnings, however you can ignore those. Finally, just run:

systemctl restart nginx.service
Code language: CSS (css)

And that’s it! It should all be working.

Conclusion

And there you are. you can now use your own, self-hosted hastebin in peace. If you need any help, just make a reply and I’ll help you out. If you are maybe interested in self hosting some other websites, have a look at this category.