Michal Zalecki
Michal Zalecki
software development, testing, JavaScript,
TypeScript, Node.js, React, and other stuff

Set up IPFS node on the server

IPFS (InterPlanetary File System) is protocol establishing the peer-to-peer network with resources addressed based on their content instead of the physical location like in HTTP. IPFS gives us some guarantees of blockchains like decentralization and unalterable storage at a fraction of the price you would have to give in transaction fees. Participation in the IPFS network is free.

The critical thing to understand about IPFS is that the network is not going to store files once you add them. Adding files to IPFS does not upload them anywhere and only means that you add them to the local repository you host on your node. Unless other peers are interested in hosting your content on their nodes, once you shut down your node, files you have added will not be available for others until you are back online. The caching mechanism mitigates that issue as peers that were interested in fetching your content keep it in the cache. Aggressive garbage collector quickly removes unused files. You should not rely on the cache to keep your files online.

To reliably share files with other peers and use IPFS e.g. to host a webpage you would like to set up a server and does not rely on your personal device connectivity.

Install and setup IPFS

I am going to use AWS EC2 to spin up my server with Amazon Linux 2 using the default VPC. A t2.micro instance will not cost you a dime during the free tier and is good enough for IPFS node and you a few web services.

Install Golang and IPFS. Make sure to use the latest version.

sudo yum update -y
sudo yum install -y golang

wget https://dist.ipfs.io/go-ipfs/v0.4.17/go-ipfs_v0.4.17_linux-amd64.tar.gz
tar -xvf go-ipfs_v0.4.17_linux-amd64.tar.gz
./go-ipfs/install.sh

If the installation fails, then you can move executable to your bin path manually.

sudo mv ./go-ipfs/ipfs /usr/local/bin

Initialize local IPFS configuration and add your first file.

> ipfs init --profile server
echo "<h1>Michal</h1>" > index.html
> ipfs add index.html
added Qma1PYYMwbgR3GBscmBV7Zx8YgWdhBUAY6z27TznhrBet9 index.html
> ipfs cat Qma1PYYMwbgR3GBscmBV7Zx8YgWdhBUAY6z27TznhrBet9
<h1>Michal</h1>

Congratulation! You've just added a file to IPFS repository. Although you can fetch it, it works only locally. To join the network, you should run the IPFS daemon.

ipfs daemon

If your firewall does not block connection, then you should be able to fetch your files from the remote node or use a public gateway like https://ipfs.io/ipfs/.

Run IPFS daemon on start

It would be better to start IPFS daemon as a service instead of the terminal attached process. Let's define a simple unit file responsible for running IPFS daemon service.

sudo vi /etc/systemd/system/ipfs.service

Copy and paste the unit file definition. Make sure to change the User, so it corresponds to the account you have on your server.

[Unit]
Description=IPFS Daemon
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
ExecStart=/usr/local/bin/ipfs daemon --enable-namesys-pubsub
User=ec2-user

[Install]
WantedBy=multi-user.target

Running daemon with --enable-namesys-pubsub benefits from nearly instant IPNS updates. IPNS is IPFS naming system that allows for mutable URLs. After editing a unit file, reload the daemon, enable service to start on boot and start the service.

sudo systemctl daemon-reload
sudo systemctl enable ipfs
sudo systemctl start ipfs

You can now reboot your instance and make sure whether IPFS is back and running.

sudo systemctl status ipfs

Make gateway publicly accessible

If you want to, you can make your IPFS gateway publicly accessible. Change gateway configuration to listen on all available IP addresses.

In ~/.ipfs/config change

"Gateway": "/ip4/127.0.0.1/tcp/8080"

to

"Gateway": "/ip4/0.0.0.0/tcp/8080"

Conclusion

I am running IPFS nodes on EC2 and GCP Compute Engine VM for some time now and I did not have any major problems with it. You can use scp to copy files over ssh to your remote server. For programmatic access, you can make your IPFS gateway writable and use IPFS HTTP API. There are plenty of creative use cases for IPFS!

Photo by James Traf on Unsplash.