链接:https://pan.baidu.com/s/1vil6sO3AoqlfNK-WnkTebw 密码:4jkt
将下载的安装包放在/usr/local
目录下,对压缩包进行解压:
tar zxvf go1.9.2.linux-amd64.tar.gz
配置GOROOT和PATH
echo "export GOROOT=/usr/local/go" >> /etc/profile
echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile
source /etc/profile //生效环境变量
查看go语言版本
>go version
>go version go1.9.2 linux/amd64
>git clone https://github.com/ethereum/go-ethereum.git
>cd go-ethereum
>make geth //编译
编译成功后,会出现go-ethereum/build/bin
文件夹,里面有geth执行文件。
在这里需要说明一下,geth安装好后,执行geth console可以进入客户端,产生的文件会存储在/root/.ethereum
,如果搭建私有链不成功,会提示这个文件已存在,删除即可。
在go-ethereum/build/bin
下创建创世块文件genesis.json,
>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
其实我们创建之后可以执行下面的命令,私有链就搭建起来了。
//其中-rpcaddr '192.168.1.245'参数为云服务器的私有IP,不设置的话,公网IP无法访问这个节点的。
>geth —rpc --rpccorsdomain '*' --rpcapi 'personal,web3,eth,net' --rpcaddr '192.168.1.245' console
pm2 是一个带有负载均衡功能的Node应用的进程管理器,PM2 (github上的源码)是开源的基于Nodejs的进程管理器,包括守护进程,监控,日志的一整套完整的功能,基本是Nodejs应用程序不二的守护进程选择,事实上它并不仅仅可以启动Nodejs的程序,只要是一般的脚本的程序它同样可以胜任。
$ 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
接下来编写一个pm2配置文件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" } ]
执行pm2 start geth.json
就可以一直启动这个进程,pm2 list可以看到当前进程以及状态。
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
│ app │ 6 │ fork │ 3692 │ online │ 2 │ 13D │ 0% │ 70.2 MB │ root │ disabled │
│ app │ 7 │ fork │ 25853 │ online │ 3 │ 37m │ 0% │ 58.4 MB │ root │ disabled │
│ geth │ 0 │ fork │ 17172 │ online │ 95 │ 20h │ 0% │ 1.2 GB │ root │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘
我们的私有链节点就搭建成功了!