Ubuntu Server Standby For Production

As a backend programer, we often need to deploy our service to Linux server, create deploy user, setting ssh config to login our server by ssh rsa public key and install build essential environment is step one for server basic DepOps. So, I write down these simple steps for memo, wish it would help for you at the same time.

Create deploy user

I saw a guy operate production server by root user, which is bad behavior for a professional programer. In daily server DepOps, we should use non root user, So we named it deploy user.

adduser deploy
adduser deploy sudo
su - deploy

Setup SSH for Auto Login withou password

There are two interact with server by SSH, one is login by user name and password, and other is by ssh rsa_pub key in your laptop. The last one is awesome, let's make it come ture.

Firstly, check is there a .ssh directory with id_rsa or id_rsa.pub for your laptop login user directory, if not, just run ssh-keygen -t rsa to generate the RSA Key Pair, go ahead.

Secondly, copy the local public ssh key to server. Thers are two command can do that, choose one whatever your like amony these two.

  • ssh-copy-id deploy@server_ip
  • cat ~/.ssh/id_rsa.pub | ssh deploy@server_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authoriz

Login server form config

If you have finished two steps by above mention. Now try to login server using ssh deploy@server_ip without password interact. Remember number is difficult for human, semantic letter is good for us. More conveniently, we can replace server_ip with custom string wirted in ssh configure in .ssh/config file, if not find it, create one.

// .ssh/config
...
Host server_name
    ForwardAgent yes
    User deploy
    Hostname server_ip

Now, you can login server by ssh server_name

Install compile environment and process monit tool

sudo apt-get update
sudo apt-get install build-essential git unzip wget ntp

There are many tools for process monit, which is very important to manage our program service. godrb written in Ruby, pm2 written in Node.js .I recommend pm2, use whatever your like.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash //install nvm
source ~/.bashrc
nvm install node // nvm install Node
npm install -g pm2 // install pm2
pm2 Usage

Get a live example to show how to use pm2 to manage our processFirstly, we should write a process file using json format:

//chain_cluster.json
{
  "name" : "ChainCluster",
  "script": "chain_cluster",
  "args": "sync -n ethereum",
  "log_file" :"/home/user/chain_cluster/ethereum.log"
}

or another example for Ethfan 星火节点

[
{
  "name"              : "geth",
  "cwd"               : "/usr/bin/",
  "script"            : "geth",
  "args"              : "--rpc -rpcaddr '0.0.0.0' --rpcport '8555' --rpcapi 'db,eth,net,web3' --maxpeers 100 --cache 512",
  "log_date_format"   : "YYYY-MM-DD HH:mm Z",
  "merge_logs"        : false,
  "watch"             : false,
  "max_restarts"      : 10,
  "exec_interpreter"  : "none",
  "exec_mode"         : "fork_mode"
},
{
  "name"              : "ethstats-client",
  "cwd"               : "/home/user/source_code/ethstats-client",
  "script"            : "app.js",
  "log_date_format"   : "YYYY-MM-DD HH:mm Z",
  "log_file"          : "/home/user/source_code/ethstats-client/logs/node-app-log.log",
  "out_file"          : "/home/user/source_code/ethstats-client/logs/node-app-out.log",
  "merge_logs"        : true,
  "watch"             : false,
  "max_restarts"      : 10,
  "exec_interpreter"  : "node",
  "exec_mode"         : "fork_mode",
  "env":
  {
    "NODE_ENV"        : "production",
    "RPC_HOST"        : "localhost",
    "RPC_PORT"        : "85xxx",
    "LISTENING_PORT"  : "30303",
    "INSTANCE_NAME"   : "xxx", //<-双引号内填写您的节点名称信息。
    "CONTACT_DETAILS" : "xxx", //<-双引号内填写您的联系信息,如网址或邮箱地址。
    "WS_SERVER"       : "wss://stats.ethfans.org",
    "WS_SECRET"       : "ethfans4you",
    "VERBOSITY"       : 2
  }
}
]

Start service by pm2 configure

pm2 start chain_cluster.json
pm2 start ethstats-client.json

pm2 status list all processes

pm2.png

If you are interesting for pm2, see detail usage in official docutment

0 条评论
您想说点什么吗?