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} 
https://{gateway URL}/ipfs/{content ID}/{optional path to resource}

More details about the IPFS gateway:

Other references:

Introduction to IPFS: Run Nodes on Your Network, with HTTP Gateways:

Cloudflare IPFS gateway: Setting up a Server:

Install IPFS on Linux Server

Official tutorial:

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 in two ways:
1. Manual installation of go-ipfs
cd ~/

// Alibaba Cloud Server probably can not access directly
// Alternatively, use: wget -q

// Unzip the file: tar xvfz go-ipfs_v0.4.18_linux-amd64.tar.gz

> x go-ipfs/
> x go-ipfs/ipfs
> x go-ipfs/LICENSE
> x go-ipfs/LICENSE-APACHE
> x go-ipfs/LICENSE-MIT
> x go-ipfs/

// change directory into go-ipfs and run script
cd go-ipfs
sudo ./
> 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 ~/
tar xvfz ipfs-update_v1.5.2_linux-amd64.tar.gz
cd ipfs-update
sudo ./
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:

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:
Description=IPFS Daemon

ExecStart=/usr/local/bin/ipfs daemon


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


  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": [
     ** "Access-Control-Expose-Headers", **
      "Access-Control-Allow-Methods": [
     ** "POST", **
      "Access-Control-Allow-Origin": [
   ** "Access-Control-Expose-Headers": [
      ], **
   **  "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/" to "Gateway": "/ip4/"

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:


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