Codementor Events

Installing Ethereum on a VPS

Published Mar 24, 2018

1*l2Q6F5FcBizIdk8KMZyubw.png
on the smallest VPS you may only get 512mb memory, this is a big issue for geth without a big swap file.

This weekend I am going to install the Ethereum Go Client, it will allow me to do a little bit of Ether mining and more importantly act as a node that I can connect to via the web3.js library on the client. Diving right in…

make a new droplet and attach a volume of at least 6GB. The initial download/sync up will be about 3.8GB as of April 2017. I called mine “eth-blockchain” as you will see further down, so be sure to name it the same or change the name if you are following along.

Follow the instructions for mounting the drive and increase the swap size and re boot

fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile 
sudo swapon /swapfile
reboot

check the file and permissions

ls -lh /swapfile

and you should see somthing like

-rw------- 1 root root 2.0G Apr  8 01:58 /swapfile

and check the swap is on

sudo swapon -s

let’s make this change permanent

sudo nano /etc/fstab

and enter this line at the bottom

/swapfile   none    swap    sw    0   0

set the swappiness and cache pressure

sudo nano /etc/sysctl.conf

enter these lines at the end

vm.swappiness=10
vm.vfs_cache_pressure = 50

Install geth

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

Start the console

geth attach ipc:/mnt/eth-blockchain/geth.ipc

and start up the RPC

admin.startRPC("127.0.0.1", 8545, "*", "web3,db,net,eth")

Now we can exit the console by typing “exit”.

we need geth to start on reboot..

nano /lib/systemd/system/geth.service

Copy in

[Unit]
Description=Ethereum Go Client
[Service]
ExecStart=/usr/bin/geth --fast --cache=16 --datadir=/mnt/eth-blockchain --identity=@bkawk --keystore=/mnt/eth-blockchain --rpc --rpcport=8882 --rpccorsdomain=* --rpcapi=web3,db,net,eth
Restart=always
RestartSec=30
Type=simple
User=root
Group=root
[Install]
WantedBy=multi-user.target

and Ctrl + X to save it and then reload it with:

systemctl daemon-reload
systemctl enable geth.service

Start it up and let’s have a look and see what magic happened

reboot
journalctl -u geth -n20

Now walk away, dont tweak around , dont restart, reboot, resize, just let it run.

Geth console
Are we done already?

geth attach ipc:/mnt/eth-blockchain/geth.ipc

start RPC

admin.startRPC("127.0.0.1", 8882, "*", "web3,db,net,eth")

get the complete status

geth.syncing

for a % done past in the below

setInterval(function(){var s = eth.syncing; console.log("%"+( s.currentBlock/s.highestBlock*100).toFixed(3)) }, 1000);

The sync will start fast and then slow down a bit towards the end.

From time to time you can check the block number which is the one after the # symbol when issuing

journalctl -u geth -n40

Once we are up to date it’s important to remove the fast flag

nano /lib/systemd/system/geth.service

and remove the

--fast 

restart and check all is good

reboot
journalctl -u geth -n40

Time to mine?

Whether it's just to feel good that you are contributing or if having a chance to win the jackpot, mining can be setup quickly, its interesting but not practical, so feel free to skip it.

Once the node is all synced-up, make a new account.

geth account new

enter a password twice, make a note of the account

0d4c318aded3d9933256e0e83021227b82bc7f91

unlock the account

geth account update 0d4c318aded3d9933256e0e83021227b82bc7f91

enter the password and then enter a new password twice and again edit the service

nano /lib/systemd/system/geth.service

and copy in

[Unit]
Description=Ethereum Go Client
[Service]
ExecStart=/usr/bin/geth --cache=16 --datadir=/mnt/eth-blockchain --identity=@bkawk --keystore=/mnt/eth-blockchain --rpc --rpcport=8882 --rpccorsdomain=https://bkawk.com --mine --minerthreads=1 --etherbase=0d4c318aded3d9933256e0e83021227b82bc7f91
Restart=always
RestartSec=30
Type=simple
User=root
Group=root
[Install]
WantedBy=multi-user.target

and Ctrl + X to save it and then reload it with:

systemctl daemon-reload
systemctl enable geth.service

Start it up and let’s have a look and see what magic happened

systemctl start geth
journalctl -u geth -n20

Follow the logs

journalctl -u geth -f

Backup the keystore

change directory to

cd /root/.ethereum/keystore

and change locally to

lcd /Users/will/Desktop/

get the file

get /root/.ethereum/keystore/UTC--2017-04-08T14-13-40.418444512Z--3ff341f6248e36c576ef0dba3db3f36e251af9b3

You can now import wallet this into Meta Mask or whatever you like and see the mining money flow in! Well it’s not going to flow in, you are solo mining on the CPU of a not so powerful mache, this is like waiting for the jackpot, if you are lucky enough to find a block you will get a full block reward of 5 Ether, about 240 usd at today’s rates, expect to wait months if not years.

if you want to throw in the cards at this point just edit

nano /lib/systemd/system/geth.service

by removing

--mine --minerthreads=1 --etherbase=0d4c318aded3d9933256e0e83021227b82bc7f91

then delete the DAG’s found here

rm -fr ~/.ethash

Restart!

systemctl daemon-reload
systemctl start geth

Point an A record

It looks nice when a Geth’s RPC port has an address so point the A record at the server's IP address.

Even though we limited the RPC CORS domain to https://www.bkawk.com, for anything more than just testing you will need some added protection from nginx…

apt-get install nginx

setup a site

nano /etc/nginx/conf.d/eth.bkawk.com.conf

copy in the below

Notice the port is 8882, the same port we set in geth.service for the rpc port.

server {
    listen 80;
server_name eth.bkawk.com;
location / {
        proxy_pass http://localhost:8545;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

check there is no errors

sudo nginx -t

restart nginx

sudo systemctl restart nginx

wait for the domain to propagate

We can now connect to this node directly from the browser using the wb3 javascript library or have a little express API requests from the client and pass them to geth, this gives you the option to keep geth away from the public and allows you full control through an API.

Discover and read more posts from Jin Ji
get started