IPFS Blog Code Tutorial

2021 June 1 Twitter Substack See all posts

This tutorial Assumes you have a remote Ubuntu Linux server up and running with SSH access. If you don't know how to provision a server from a cloud provider, here is a tutorial.

This tutorial is abbreviated, links to further documentation are at the bottom of the page.

First we need add a non-root user and allow ssh access. We login as root, add the user, give them sudo privileges, then switch to that user.

    ssh root@your-server-ip

    adduser your-user-name

    usermod -aG your-user-name

    su - your-user-name

Our ssh keys need to be added to log directly into this user. We have to create the directory and file to hold our public key, plus change some permissions.

    mkdir ~/.ssh

    chmod 700 ~/.ssh

    nano ~/.ssh/authorized_keys

Copy and paste your public key into the file. This is usually in the ~/.ssh/id_rsa.pub file on your laptop. Hit "control+x", y, and "enter" to save and exit.

Restrict permissions for the new file:

    chmod 600 ~/.ssh/authorized_keys

Update and install Go on your web server:

    sudo apt-get update

    sudo apt-get -y upgrade

    sudo apt-get install golang

Next, download IPFS:

    wget https://dist.ipfs.io/go-ipfs/v0.8.0/go-ipfs_v0.8.0_linux-amd64.tar.gz

    tar -xvzf go-ipfs_v0.8.0_linux-amd64.tar.gz


    '> x go-ipfs/install.sh'
    '> x go-ipfs/ipfs'
    '> x go-ipfs/LICENSE'
    '> x go-ipfs/LICENSE-APACHE'
    '> x go-ipfs/LICENSE-MIT'
    '> x go-ipfs/README.md'

Change directory and install:

    cd go-ipfs

    sudo bash install.sh


    '> Moved ./ipfs to /usr/local/bin'

Check for a version to verify the installation:

    ipfs --version


    '> ipfs version 0.8.0'


    ipfs init --profile server

For the node to communicate with the network, it has to have its daemon operating. We want to configure the server so that it runs automatically in the background. I haven't set up many services and googled until I found one set of settings that worked.

First create the service file:

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

Then copy this into the file (make sure to add your username):

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

    ExecStart=/usr/local/bin/ipfs daemon --enable-namesys-pubsub


Now we will enable the service:

    sudo systemctl daemon-reload

    sudo systemctl enable ipfs

    sudo systemctl start ipfs

Test to see if it is working:

    sudo systemctl status ipfs


     ipfs.service - IPFS Daemon
     Loaded: loaded (/etc/systemd/system/ipfs.service; enabled; vendor preset: >
     Active: active (running) since Thu 2021-06-03 16:18:02 UTC; 8s ago
     Main PID: 13651 (ipfs)
      Tasks: 10 (limit: 9513)
     Memory: 61.1M
     CGroup: /system.slice/ipfs.service
             └─13651 /usr/local/bin/ipfs daemon --enable-namesys-pubsub

        Jun 03 16:18:03 tutorial-test2 ipfs[13651]: Swarm announcing /ip4/>
        Jun 03 16:18:03 tutorial-test2 ipfs[13651]: Swarm announcing /ip4/>
        Jun 03 16:18:03 tutorial-test2 ipfs[13651]: Swarm announcing /ip6/2604:a880:400>
        Jun 03 16:18:03 tutorial-test2 ipfs[13651]: Swarm announcing /ip6/2604:a880:400>
        Jun 03 16:18:03 tutorial-test2 ipfs[13651]: Swarm announcing /ip6/::1/tcp/4001
        Jun 03 16:18:03 tutorial-test2 ipfs[13651]: Swarm announcing /ip6/::1/udp/4001/>
        Jun 03 16:18:03 tutorial-test2 ipfs[13651]: API server listening on /ip4/127.0.>
        Jun 03 16:18:03 tutorial-test2 ipfs[13651]: WebUI:
        Jun 03 16:18:03 tutorial-test2 ipfs[13651]: Gateway (readonly) server listening>
        Jun 03 16:18:03 tutorial-test2 ipfs[13651]: Daemon is ready

Ok, now we are ready to move our files to the server. In the terminal or shell on your laptop/desktop, move the HTML directory to the server. You must have the directory configured old school with an index.html home and only use relative links to other pages and files. We will use secure copy protocol. Use your file path, IP address, and username on your remote server.

    scp -r /path/to/directory your-user-name@serverIP:/home/your-user-name

Return to the remote server command line. I am calling my website directory "test-site" for the example below. Change to the directory you copied the website files to.

    ipfs add -r test-site


    '>added QmNiBYXmgwLvT4xBiL8cX9j5H3AckiEjAnLZsoBiK6xEEr test-site/index.html'
    '>added QmZhCL5rkWjH4MotDxKHUDaUESEKhTxSE7Xr16zwe59sjT test-site'
    '>432.98 KiB / 432.98 KiB [=============================================] 100.00%'

The last hash associated with the test-site directory is the hash that can access your webpage. Copy your hash and try to visit your site: https://ipfs.io/ipfs/your-webpage-hash. The example above would be https://ipfs.io/ipfs/QmZhCL5rkWjH4MotDxKHUDaUESEKhTxSE7Xr16zwe59sjT.

Now the problem is that each hash is unique. If you change one letter on your website, it changes that hash. We need to link our domain to a permanent address. We do that by publishing the content. Note: It may take a few minutes for this command to run.

    ipfs name publish your-webpage-hash


    'Published to QmRX....xQTp: (your peer id)'

The permanent link to the site is your node peer id. ENS, Handshake, or DNS records work by pointing to this hash. There are further options to create multiple site id's in the IPFS Primer/tutorial linked below. Now you can visit your site via https://ipfs.io/ipns/your-peer-id. Notice it is "ipns" now instead of "ipfs".

Finally, go to the management page for your ENS domain name. Among the listed fields to place addresses, there is one called content. Copy in /ipns/your-peer-id. Update the contract on Ethereum. Your website should load by using your-name.ens.link.

If you update your website, repeat the steps of copying the files to the web server, adding the files to IPFS, and publishing them to your peer id.

Further Resources:

IPFS Documentation

IPFS Primer/tutorial

Configuring Digital Ocean Server for IPFS

Host a Website on IPFS Tutorial