IPFS - Interplanetary Filesystem - How to set up a writable IPFS Gateway on a VPS (and configure CORS for cross-domain requests)

in #technology7 years ago

IPFS is the Interplanetary Filesystem, it's a new layer of the internet, sort of like the World Wide Web.  But IPFS was built in a Peer To Peer model with the modern Internet in mind.  And it runs on nearly everything, even Raspberry Pi computers.

Currently it only supports static content, you can upload entire websites as long as the website uses only client-side javascript. You can't run code on the server-side (yet).  But there is a dynamic naming system, the IPNS, so that you can update your online content without needing to break old links or create new ones.

It's not just for websites though, there's a ton of cat pictures too.  You can basically store any file-type on IPFS, and it's free to use.

I've set up a writable IPFS Gateway at RelayRouter.com:8080/ipfs/, in order to see anything there you'll need an IPFS hash like this one QmTbhNNgnSzDnQj8mLELcxqZKwUwbzpnHj2iMeqscjpDEF.  It goes at the end of the url like this: http://relayrouter.com:8080/ipfs/QmTbhNNgnSzDnQj8mLELcxqZKwUwbzpnHj2iMeqscjpDEF

![QmTbhNNgnSzDnQj8mLELcxqZKwUwbzpnHj2iMeqscjpDEF.jpg]()

Writing data to IPFS through a gateway can be a little bit tricky though, you will need to post the data from your own html or application, and the hash is what gets returned in an http header named Ipfs-Hash.  (I have no idea why the IPFS developers decided to return the data as an http header instead of just putting it in the body of the response, but there's nothing I can do about it at this point.)

Also, please note that anything you upload to any writable IPFS Gateway will still need to be pinned by you as an IPFS User to make that data persistent.  Otherwise the data that you upload may get wiped at the next garbage collection process, which happens regularly.

Anyway, I believe there should be many more IPFS gateways out there so I wanted to share with you how I set mine up.

First I got a VPS from https://www.ssdnodes.com/startup-specials/ and I chose Ubuntu 16 as the operating system for this VPS.  Then I SSH'd into the VPS from my local PC and entered the following commands:

apt-get update
apt-get install tar wget nano
wget https://dist.ipfs.io/go-ipfs/v0.4.10/go-ipfs_v0.4.10_linux-amd64.tar.gz
tar xfv go-ipfs_v0.4.10_linux-amd64.tar.gz
cd go-ipfs
./install.sh
adduser ipfs
su ipfs
ipfs init
ipfs config --json Discovery.MDNS.Enabled false
ipfs config --json Swarm.AddrFilters '[   "/ip4/10.0.0.0/ipcidr/8",   "/ip4/100.64.0.0/ipcidr/10",   "/ip4/169.254.0.0/ipcidr/16",   "/ip4/172.16.0.0/ipcidr/12",   "/ip4/192.0.0.0/ipcidr/24",   "/ip4/192.0.0.0/ipcidr/29",   "/ip4/192.0.0.8/ipcidr/32",   "/ip4/192.0.0.170/ipcidr/32",   "/ip4/192.0.0.171/ipcidr/32",   "/ip4/192.0.2.0/ipcidr/24",   "/ip4/192.168.0.0/ipcidr/16",   "/ip4/198.18.0.0/ipcidr/15",   "/ip4/198.51.100.0/ipcidr/24",   "/ip4/203.0.113.0/ipcidr/24",   "/ip4/240.0.0.0/ipcidr/4" ]'
ipfs daemon &

At this point we have IPFS installed and running on the VPS, but we can only use it from the command line.  So now for the fun part, we need to make it a writable gateway and enable CORS.  We need to open the file at ~/.ipfs/config.

First make sure your in user "ipfs" instead of "root", if your not sure then type whoami to find out and then type su ipfs if you need to switch to the ipfs user.

Once you've made sure that you're logged in to your VPS as the ipfs user then enter the following command.

nano ~/.ipfs/config

Now we will change line where it reads "Gateway": "/ip4/127.0.0.1/tcp/8080",

We need to change it to "Gateway": "/ip4/0.0.0.0/tcp/8080",

Then scroll down further to the Gateway section.  We need to change it to look like this:

  "Gateway": {
    "HTTPHeaders": {
      "Access-Control-Allow-Headers": [
        "X-Requested-With",
        "Access-Control-Expose-Headers",
        "Range"
      ],
      "Access-Control-Expose-Headers": [
        "Location",
        "Ipfs-Hash"
      ],
      "Access-Control-Allow-Methods": [
        "GET",
        "POST"
      ],
      "Access-Control-Allow-Origin": [
        "*"
      ],
      "X-Special-Header": [
        "Access-Control-Expose-Headers: Ipfs-Hash"
      ]
    },
    "PathPrefixes": [],
    "RootRedirect": "",
    "Writable": true
  },

What we just did there was change the gateway settings to allow writing (posting) and CORS for cross-domain requests.  There only one thing left to do now.  Enter the following command to instruct your VPS to run IPFS every time it boots up:

sed -i -e '$i /bin/su ipfs -c "/usr/local/bin/ipfs daemon &"\n' /etc/rc.local

Then you should reboot your VPS, give it a minute to finish booting, and then check your newly set up gateway by entering its IP Address or URL into a web browser (please keep in mind the default port is 8080) with an IPFS hash, just like at http://relayrouter.com:8080/ipfs/QmTbhNNgnSzDnQj8mLELcxqZKwUwbzpnHj2iMeqscjpDEF but with your own VPS server's address.

Congratulations, you now have your very own IPFS Gateway!  Once it's set up you may want to consider announcing it here on SteemIt so that more people can find more content.

Now, IPFS doesn't just need Gateways, it also needs a Search Engine.  So I would like to encourage anyone who's interested to visit https://github.com/ipfs-search/ipfs-search and check out this awesome project which I totally had nothing at all to do with.  They are in need of hosting and developers.

Sort:  

My own gateway! About time :)
Upvoted indeed

Hello guys please register through this link i will be very thankful to you please i need referrals and you can also earn big amount you can check but please help me in getting my target of 40 referrals. I need 40 people to register this link please. I will give you referrals in return but please kindly login through this link.

http://padyredrgi.loan/5317664160364/

Followed and upvoted . Please follow me .

Followed n upvoted. Please follow me .

I am older and just beginning to code, but this protocol is making it worth it. I can remember when the internet was mostly decentralized and would like to see that again. Thanx!

Loading...

Nice informative post with coding implications!!!

Followed n upvoted . Please follow me .

Good job.
Very well explained. Thanks for sharing

I loved your post, a greeting for you, my brother @anomaly

I believe the alt coin Ark will implement this in a future release, interesting concept and hopefully we can see more about this.

Keep me follow and Upvote same for You 😊

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 64689.90
ETH 3450.92
USDT 1.00
SBD 2.50