Setup Your Own IPFS Gateway

Setup Your Own IPFS Gateway

Create VPS on cloud

  1. Create Instance on DO/AWS
  2. Configure a security group with the following options:
Custom TCP: 8080
Custom TCP: 4001–4002
  1. SSH into your EC2 instance

Install “go-ipfs”

  1. Select a Linux binary from https://dist.ipfs.io/go-ipfs/
    e.g. https://dist.ipfs.io/go-ipfs/v0.14.0/go-ipfs_v0.14.0_linux-amd64.tar.gz
  2. Unzip: tar -xvzf go-ipfs_v0.14.0_linux-amd64.tar.gz
  3. Navigate to the correct directory: cd go-ipfs
  4. Run the install script: sudo bash install.sh
  5. Check that IPFS was installed correctly by running: ipfs --version

Initialize the IPFS repository

  1. Create a directory: mkdir /home/ubuntu/data/ipfs
  2. Add the path to your Bash profile:
echo 'export IPFS_PATH=/home/ubuntu/data/ipfs >> ~/.profile'
  1. Reload your bash profile: source ~/.profile
  2. Initialize the IPFS repository: ipfs init --profile server
  3. Open the Gateway on port 8080: ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/8080

Set up an IPFS daemon

  1. Create a systemd service: sudo vim /lib/systemd/system/ipfs.service
  2. Add the following contents to the file:
[Unit]
Description=ipfs daemon
[Service]
ExecStart=/usr/local/bin/ipfs daemon --enable-gc
Restart=always
User=ubuntu
Group=ubuntu
Environment=”IPFS_PATH=/home/ubuntu/data/ipfs”
[Install]
WantedBy=multi-user.target
  1. Save the file
  2. Restart the daemon: sudo systemctl daemon-reload
  3. Enable the service: sudo systemctl enable ipfs.service
  4. Start the service: sudo systemctl start ipfs
  5. Check the status: sudo systemctl status ipfs

Make your IPFS Writable

  1. Edit IPFS configuration: sudo vim /home/ubuntu/data/ipfs/config
  2. Set json key .Gateway.Writable = true
{
  ......
  "Gateway": {
    "APICommands": [],
    "HTTPHeaders": {
      "Access-Control-Allow-Headers": [
        "X-Requested-With",
        "Range",
        "User-Agent"
      ],
      "Access-Control-Allow-Methods": [
        "GET",
        "POST",
        "PUT"
      ],
      "Access-Control-Allow-Origin": [
        "*"
      ]
    },
    "NoDNSLink": false,
    "NoFetch": false,
    "PathPrefixes": [],
    "PublicGateways": null,
    "RootRedirect": "",
    "Writable": true
  },
  ......
}
  1. Restart ipfs: sudo systemctl restart ipfs

Support Form-Data upload

  1. Nginx proxy
    cat /etc/nginx/conf.d/ipfs_gw.conf
upstream gateway {
    server 127.0.0.1:8080;
}

upstream api {
    server 127.0.0.1:5001;
}

server {
    server_name _;
    root /var/www/html;

    access_log /var/log/nginx/access.log;

    listen 80 default_server;
    listen [::]:80 default_server;

    #include conf.d/gateway/denylist.conf;

    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
    add_header 'Access-Control-Allow-Headers' 'X-Requested-With, Range, Content-Range, X-Chunked-Output, X-Stream-Output' always;
    add_header 'Access-Control-Expose-Headers' 'Content-Range, X-Chunked-Output, X-Stream-Output' always;

    client_max_body_size 100M;
    proxy_pass_header Server;
    proxy_read_timeout 1800s;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Ipfs-Gateway-Prefix "";
        proxy_pass http://gateway;
    }

    location /api/v0/ {
        proxy_set_header Host $host;
        proxy_set_header X-Ipfs-Gateway-Prefix "";
        proxy_pass http://api;
    }
}

1 Like