Ethereum Private Chain Setup and Web3.js Usage Guide

·

Installing the Ethereum Client (Geth)

To install Geth on Ubuntu-based systems, run the following commands:

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

Setting Up Process Management with PM2

PM2 requires Node.js environment. Install it using these commands:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node
source ~/git/nvm/nvm.sh
nvm install 7
source ~/.bashrc
export npm_config_registry=https://registry.npm.taobao.org

Configuring and Launching Geth

Create a configuration file geth.json:

[
  {
    "name": "geth",
    "cwd": "/usr/bin/",
    "script": "geth",
    "args": "--rpcapi eth,web3 --rpc --dev --datadir /home/username/geth_private_data",
    "log_date_format": "YYYY-MM-DD HH:mm Z",
    "out_file": "/home/username/geth_private_data/log/geth_out.log",
    "error_file": "/home/username/geth_private_data/log/geth_err.log",
    "log_file": "/home/username/geth_private_data/log/geth_log.log",
    "merge_logs": false,
    "watch": false,
    "max_restarts": 10,
    "exec_interpreter": "none",
    "exec_mode": "fork_mode"
  }
]

Launch Geth with PM2:

pm2 start geth.json

Alternative Launch Method (Direct Execution)

  1. Create a shell script geth_private.sh:
#!/bin/bash
geth=${GETH:-geth}
$geth --datadir data --networkid 31415926 --rpc --rpcapi "admin,debug,eth,miner,net,personal,shh,txpool,web3" rpcaddr "0.0.0.0" --rpccorsdomain "*" --nodiscover console
  1. Make it executable:

    chmod +x geth_private.sh
  2. Choose your launch method:
nohup ./geth_private.sh 0<&- &>/dev/null & # Daemon mode
nohup ./geth_private.sh 0<&- &> /tmp/geth.log & # Daemon with logging
./geth_private.sh # Interactive console

👉 Learn more about Ethereum node management

Working with Ethereum JavaScript API

Ethereum provides a JavaScript runtime environment through its console, exposing:

Connect to Geth using these methods:

geth attach ipc:/some/custom/path
geth attach http://191.168.1.1:8545
geth attach ws://191.168.1.1:8546

Web3.js Integration

For Node.js applications, install web3.js:

npm install web3

Install Solidity compiler for smart contract development:

sudo add-apt-repository ppa:ethereum/ethereum
sudo add-apt-repository ppa:ethereum/ethereum-dev
sudo apt-get update
sudo apt-get install solc

Key considerations when deploying contracts:

👉 Essential Ethereum development resources

Frequently Asked Questions

How do I know if my private chain is working properly?

Monitor your logs for block mining messages like "Mined block (#72 / 517dcfd1)". This indicates successful mining operations.

What RPC APIs should I enable for web3.js development?

At minimum include: eth, web3, personal, and net. For full functionality consider adding: admin, debug, miner, shh, txpool.

How can I troubleshoot failed contract deployments?

Check your logs for error messages and verify:

  1. Your account is unlocked
  2. You have sufficient balance
  3. The mining process is active

What's the recommended network ID for private chains?

Use any non-zero number not conflicting with mainnet (1) or testnets. Common choices include 1337 or random large numbers like 31415926.

How do I ensure my private chain persists between restarts?

Configure proper data directory (--datadir) and use persistent storage. Consider regular backups of your chaindata.