Building an Ethereum environment based on centos - the pit

Ethereum environment construction

1. Install the go locale

Using go1.9.linux-amd64.tar.gz will report an error when compiling the Ethereum source code. The online solution is to switch the version to go1.9.2.linux-amd64.tar.gz. Share this version here:

链接:https://pan.baidu.com/s/1vil6sO3AoqlfNK-WnkTebw  密码:4jkt

Put the downloaded installation package in the /usr/localdirectory and decompress the compressed package:

tar zxvf go1.9.2.linux-amd64.tar.gz

Configure GOROOT and PATH

echo "export GOROOT=/usr/local/go" >> /etc/profile  
echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile  
source /etc/profile   //生效环境变量

View the go language version

>go version
>go version go1.9.2 linux/amd64

2. Clone go-ethereum

>git clone https://github.com/ethereum/go-ethereum.git
>cd go-ethereum
>make geth //编译

After the compilation is successful, a folder will appear go-ethereum/build/binwith the geth executable file in it.

3. Build a private chain

It needs to be explained here that after geth is installed, execute the geth console to enter the client, and the generated files will be stored in the file /root/.ethereum. If the private chain is unsuccessful, it will prompt that the file already exists, and you can delete it. Create a genesis block file
undergo-ethereum/build/bin

>vim genesis.json
>{
    "config": {},
    "nonce": "0x0000000000000042",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "difficulty": "0x100",
    "alloc": {},
    "coinbase": "0x0000000000000000000000000000000000000000",
    "timestamp": "0x00",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x00",
    "gasLimit": "0xffffffffffff"
}
>geth init genesis.json

In fact, after we create it, we can execute the following command, and the private chain is built.

//其中-rpcaddr '192.168.1.245'参数为云服务器的私有IP,不设置的话,公网IP无法访问这个节点的。
>geth —rpc --rpccorsdomain '*' --rpcapi 'personal,web3,eth,net' --rpcaddr '192.168.1.245' console

4. Use the pm2 process management tool to manage the geth process

pm2 is a process manager for Node applications with load balancing function, PM2 (source code on github) is an open source Nodejs-based process manager, including a complete set of functions for daemon, monitoring, and logging, basically Nodejs The only daemon process choice for applications, in fact, it can not only start Nodejs programs, as long as it is a general script program, it can also be competent.

$ pm2 start app.js              # 启动app.js应用程序

$ pm2 start app.js -i 4         # cluster mode 模式启动4个app.js的应用实例     # 4个应用程序会自动进行负载均衡

$ pm2 start app.js --name="api" # 启动应用程序并命名为 "api"

$ pm2 start app.js --watch      # 当文件变化时自动重启应用

$ pm2 start script.sh           # 启动 bash 脚本


$ pm2 list                      # 列表 PM2 启动的所有的应用程序

$ pm2 monit                     # 显示每个应用程序的CPU和内存占用情况

$ pm2 show [app-name]           # 显示应用程序的所有信息


$ pm2 logs                      # 显示所有应用程序的日志

$ pm2 logs [app-name]           # 显示指定应用程序的日志

$ pm2 flush


$ pm2 stop all                  # 停止所有的应用程序

$ pm2 stop 0                    # 停止 id为 0的指定应用程序

$ pm2 restart all               # 重启所有应用

$ pm2 reload all                # 重启 cluster mode下的所有应用

$ pm2 gracefulReload all        # Graceful reload all apps in cluster mode

$ pm2 delete all                # 关闭并删除所有应用

$ pm2 delete 0                  # 删除指定应用 id 0

$ pm2 scale api 10              # 把名字叫api的应用扩展到10个实例

$ pm2 reset [app-name]          # 重置重启数量


$ pm2 startup                   # 创建开机自启动命令

$ pm2 save                      # 保存当前应用列表

$ pm2 resurrect                 # 重新加载保存的应用列表

$ pm2 update                    # Save processes, kill PM2 and restore processes

$ pm2 generate                  # Generate a sample json configuration file


$ pm2 deploy app.json prod setup    # Setup "prod" remote server

$ pm2 deploy app.json prod          # Update "prod" remote server

$ pm2 deploy app.json prod revert 2 # Revert "prod" remote server by 2


$ pm2 module:generate [name]    # Generate sample module with name [name]

$ pm2 install pm2-logrotate     # Install module (here a log rotation system)

$ pm2 uninstall pm2-logrotate   # Uninstall module

$ pm2 publish 

Next, write a pm2 configuration file geth.json

>vim geth.json
>[
    {
        "name": "geth",
        "script": "geth",
        "args": "--rpc --rpccorsdomain '*' --rpcapi 'personal,web3,eth,net' --rpcaddr '192.168.1.245'",
        "log_date_format": "YYYY-MM-DD HH:mm Z",
        "out_file": "/root/go-ethereum/build/bin/log/geth_out.log",
        "error_file": "/root/go-ethereum/build/bin/log/geth_err.log",
        "log_file": "/root/go-ethereum/build/bin/log/geth_log.log",
        "merge_logs": false,
        "watch": false,
        "max_restarts": 10,
        "exec_interpreter": "none",
        "exec_mode": "fork_mode"
    }
]

Execution pm2 start geth.jsoncan always start the process, and pm2 list can see the current process and status.

┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
│ App name │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
│ app      │ 6fork3692  │ online │ 213D    │ 0%  │ 70.2 MB   │ root │ disabled │
│ app      │ 7fork25853 │ online │ 337m0%  │ 58.4 MB   │ root │ disabled │
│ geth     │ 0fork17172 │ online │ 9520h    │ 0%  │ 1.2 GB    │ root │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘

Our private chain node has been successfully built!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324956792&siteId=291194637