How to Quickly Build Your Own IPFS Gateway

What is IPFS?

InterPlanetary File System (IPFS) is a network transport protocol designed to enable distributed storage, sharing, and persistence of files. It is a content-addressable peer-to-peer hypermedia distribution protocol. The nodes in an IPFS network constitute a distributed file system.

IPFS Gateway

IPFS gateways allow visitors to access data from an IPFS network via HTTP requests. By default, the IPFS gateway is configured to run on port 8080 and data will be fetched from the server running IPFS in the following ways:

http://{your_ip_address}:8080/ipfs/{content ID} 
or
https://{gateway URL}/ipfs/{content ID}/{optional path to resource}

More details about the IPFS gateway: https://docs.ipfs.io/concepts/ipfs-gateway/#overview

Other references:

Introduction to IPFS: Run Nodes on Your Network, with HTTP Gateways: https://rossbulat.medium.com/introduction-to-ipfs-set-up-nodes-on-your-network-with-http-gateways-10e21ea689a4

Cloudflare IPFS gateway: Setting up a Server: https://developers.cloudflare.com/distributed-web/ipfs-gateway/setting-up-a-server/

Install IPFS on Linux Server

Official tutorial: https://docs.ipfs.io/install/command-line/#official-distributions

Here we take Alibaba Cloud ECS server as an example and select CentOS 8.5.

  1. Click “Connect” and select Workbench Connection to remotely access your ECS instance via the web

  1. After connecting to the server, IPFS can be installed from dist.ipfs.io in two ways:
1. Manual installation of go-ipfs
cd ~/
wget https://dist.ipfs.io/go-ipfs/v0.12.2/go-ipfs_v0.12.2_linux-amd64.tar.gz

// Alibaba Cloud Server probably can not access dist.ipfs.io directly
// Alternatively, use: wget -q https://github.com/ipfs/go-ipfs/releases/download/v0.12.2/go-ipfs_v0.12.2_linux-amd64.tar.gz

// Unzip the file: tar xvfz go-ipfs_v0.4.18_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 into go-ipfs and run install.sh script
cd go-ipfs
sudo ./install.sh
> Moved ./ipfs to /usr/local/bin

// Test if ipfs is installed correctly
ipfs --version
> ipfs version 0.12.2

2. Install via ipfs-update

cd ~/
wget https://dist.ipfs.io/ipfs-update/v1.8.0/ipfs-update_v1.8.0_linux-amd64.tar.gz
tar xvfz ipfs-update_v1.5.2_linux-amd64.tar.gz
cd ipfs-update
sudo ./install.sh
ipfs-update versions
ipfs-update install latest

Running ipfs-update install latest will install the latest version of go-ipfs, the output shows:

fetching go-ipfs version v0.4.22
binary downloaded, verifying...
success! tests all passed.
stashing old binary
installing new binary to /usr/local/bin/ipfs
checking if repo migration is needed...

Installation complete!
Remember to restart your daemon before continuing

Initializing repository

See the official initialization and operation tutorial in detail: https://docs.ipfs.io/how-to/command-line-quick-start/#initialize-the-repository

After a successful installation, first, run ipfs init to initialize the repository. IPFS places all settings and internal data in a directory called “repository” by default. The output shows:

ipfs init

> initializing ipfs node at /Users/jbenet/.ipfs
> generating 2048-bit RSA keypair...done
> peer identity: Qmcpo2iLBikrdf1d6QU6vXuNb6P7hwrbNPW9kLAH8eG67z
> to get started, enter:
>
>   ipfs cat /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme

Attention: If you run sudo ipfs init, the repository will be created for the root user instead of your local user account. IPFS does not require root privileges, so it’s better to run all commands as a normal user!

Use Systemd to start the IPFS daemon

Systemd is a package that comes with most newer Linux distributions. It allows users to create and manage background services. These services start automatically when the server starts, restart if it fails, and their output logs are saved on disk.

Create the following Systemd unit file to keep the IPFS server on during a reboot:

  1. Type in the command: sudo bash -c 'cat >/lib/systemd/system/ipfs.service <<EOL
  2. Type in the following content:
[Unit]
Description=IPFS Daemon
After=network.target

[Service]
ExecStart=/usr/local/bin/ipfs daemon
User=root
Restart=always
LimitNOFILE=10240
Environment="IPFS_PATH=/mnt/disks/ipfs-disk/ipfs"

[Install]
WantedBy=multi-user.target

The line User=root is filled in according to the username running IPFS, as shown below:

image

  1. Type in EOL' to end
  2. Run the following command to tell Systemd to start the new service
sudo systemctl daemon-reload
sudo systemctl enable ipfs
sudo systemctl start ipfs

Configuring IPFS HTTP gateways

  1. Run the following command to configure CORS
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["<your domain or all (*)>"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]'
  1. Run vi ~/.ipfs/config to edit the configuration file
    a. Find “Gateway” and change its content as follows:
// The ** symbols on both sides indicate the content being modified
"Gateway": {
    "APICommands": [],
    "HTTPHeaders": {
      "Access-Control-Allow-Headers": [
        "X-Requested-With",
     ** "Access-Control-Expose-Headers", **
        "Range",
        "User-Agent"
      ],
      "Access-Control-Allow-Methods": [
     ** "POST", **
        "GET"
      ],
      "Access-Control-Allow-Origin": [
        "*"
      ],
   ** "Access-Control-Expose-Headers": [
        "Location",
        "Ipfs-Hash"
      ], **
   **  "X-Special-Header": [
        "Access-Control-Expose-Headers: Ipfs-Hash"
      ] **
    },
    "NoDNSLink": false,
    "NoFetch": false,
    "PathPrefixes": [],
    "PublicGateways": null,
    "RootRedirect": "",
 ** "Writable": true **
  },

b. Find "Addresses" and change "Gateway": "/ip4/127.0.0.1/tcp/8080" to "Gateway": "/ip4/0.0.0.0/tcp/8080"

After the gateway configuration is complete, restart the IPFS service: sudo systemctl restart ipfs

Attention: IPFS Desktop supports setting the default storage (repository default in /~.ipfs), and port number (8080, 5001, 4001), you need to pay attention to prevent the port conflict with other processes.

Access to your gateway

Type the following URL in a browser:

http://<your_ip_address>:8080/ipfs/QmSgvgwxZGaBLqkGyWemEDqikCqU52XxsYLKtdy3vGZ8uq

Seeing the picture means your gateway is working.

Also, to fetch the files from IPFS by the following command:

ipfs cat /ipfs/QmSgvgwxZGaBLqkGyWemEDqikCqU52XxsYLKtdy3vGZ8uq

Welcome to leave your question during the building process~

1 Like