Skip Navigation

13 May 2022

Getting started with IPFS and hosting a website on it.

Sahil Kokamkar
Unsplash Credit:@nasa

What is IPFS?

IPFS stands for InterPlanetary File System. It is a P2P protocol that aims to surpass HTTP. And retrieves pieces from multiple nodes at once, enabling to create a substantial bandwidth savings. Something like BitTorrent. It is used to store NFTs or any file, as blockchain aren't suitable to store data.

You can read its paper from here.

Note: IPFS isn't an anonymous network like tor. And anyone can view your files with CID, so if you're storing some sensitive data, make sure you encrypt it.

Getting started with IPFS

  • Desktop App - The easiest way of getting started with IPFS

  • CLI - Which we will be using inside Docker container

  • Browser Extension - This allows us to connect to our IPFS node or use a local or public gateway. It also adds support for ipfs:// addresses.

Hosting a node of IPFS

You can either go-ipfs or js-ipfs to host your node.

We will be using go-ipfs with docker.

You can host your IPFS node on a server or locally, the installation will be same for both of them.

Run docker --version to check if you have docker installed, and it's up-to-date.

Docker version

You will get an output like above one. If not, follow this to install docker.

Now that you have docker installed. Configure your file path for ipfs_staging and ipfs_data to import and export files from IPFS by running the following:

export ipfs_staging=<path>
export ipfs_data=<path>

Start the docker container by running

sudo docker run -d --name ipfs-node -v $ipfs_staging:/export -v $ipfs_data:/data/ipfs -p 4001:4001 -p 4001:4001/udp -p 127.0.0.1:8080:8080 -p 127.0.0.1:5001:5001 ipfs/go-ipfs:latest

You can add -e IPFS_PROFILE=server if you are running on a server. It will prevent IPFS from creating a lot of data center-internal traffic trying to discover local nodes.

This will start the container and expose the ports 4001 (P2P TCP/QUIC transports), 5001 (RPC API) and 8080 (Gateway) in localhost

Now to enable swarm run sudo docker exec ipfs-node ipfs swarm peers you can also Private swarms.

Now check the log by running sudo docker logs -f ipfs-node

You should see the following output on the screen

Docker IPFS log

You can also do curl http://localhost:5001 to verify if it's running.

If you are on localhost, you can simply go to localhost:5001, and you will be redirected to the dashboard to configure the HTTPHeaders.

If you are on a server, you can either use VPN to access it or configure a password protected web server.

I am going to configure a password protected access.

We will be using NGINX you can use any web server of your choice, just make sure you restrict its access.

First, install nginx and, apache2-utils which will be need to generate username and password using htpasswd.

For that run and replace username with yours.

sudo htpasswd -c /etc/nginx/.htpasswd <username>

It will then prompt and ask for new password, make sure you remember it which will be needed for accessing the server.

First run sudo ufw allow "Nginx Full" to open up public traffic to ports 80 and 443.

Then create a /etc/nginx/sites-available/ipfs-node.conf and the use text editor of your choice to edit it and paste the following: (Just make sure you add your domain name in config as well as DNS server)

server {
    listen       80;
    listen       [::]:80;
    server_name  <your-domain>;

    access_log  /var/log/nginx/ipfs-node.access.log;
    error_log   /var/log/nginx/ipfs-node.error.log;

    location / {
      proxy_pass http://localhost:5001;
      auth_basic "Restricted Content";
      auth_basic_user_file /etc/nginx/.htpasswd;
  }
}

Then create a symbolic link using the below command:

sudo ln -s /etc/nginx/sites-available/ipfs-node.conf /etc/nginx/sites-enabled/

Then test the NGINX config by running, sudo nginx -t you should get a test is successful message.

Now, do sudo systemctl reload nginx to make changes take place.

You can visit your domain name, and you should be prompt to enter username and password before entering anything, make sure you have https enable because your credentials will get exposed. You can follow this steps to enable it using certbot.

After enabling https now can visit your site you can enter your username and password.

Now you should be seeing your dashboard saying “Could not connect to the IPFS API”.

Could not connect to the IPFS API

To connect it with your domain, run the following: (Add your Domain in CORS list)

sudo docker exec ipfs-node ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://localhost:3000", "http://127.0.0.1:5001", "https://<your-domain>"]'
sudo docker exec ipfs-node ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "POST"]'

After that run sudo docker restart ipfs-node to make changes take place.

Now refresh your page. You should be seeing a dashboard working now.

IPFS Dashboard

Now you can explore the dashboard.

First, make sure you have a production build of your website, then can upload files through your dashboard by navigating to Files tab or by below command.

docker exec ipfs_host ipfs add -r <build-folder>

After the upload, you should get a CID.

And also see it on files tab or paste the CID in explorer and make sure you pin it.

IPFS upload tab

Now you should be able to see your website live by visiting ipfs://<your-CID> you can visit mine or use a public gateway if didn't have IPFS enabled in browser. As the address is hard to remember, we are going to enable DNSLink.

To enable DNS, first we have to configure our CNAME and TXT DNS record.

First add CNAME record NAME pointing to your domain and add TARGET pointing to any public gateway, here is a list of it.

Next add a TXT record NAME _dnslink.<your-domain> and in CONTENT add dnslink=/ipfs/<CID>.

You can configure TTL according to your choice, but it's best to keep it low.

Verify your DNS records.

Verify DNS record

Then visit your domain, it should load through public IPFS gateway.

That's it, you have successfully hosted your site on IPFS.

You can also view my blog and my website loading through IPFS