HOWTO: Describe self-signed SSL cert creation and use
[electrum-server.git] / HOWTO.md
1 How to run your own Electrum server
2 ===================================
3
4 Abstract
5 --------
6
7 This document is an easy to follow guide to installing and running your own
8 Electrum server on Linux. It is structured as a series of steps you need to
9 follow, ordered in the most logical way. The next two sections describe some
10 conventions we use in this document and hardware, software and expertise
11 requirements.
12
13 The most up-to date version of this document is available at:
14
15     https://github.com/spesmilo/electrum-server/blob/master/HOWTO.md
16
17 Conventions
18 -----------
19
20 In this document, lines starting with a hash sign (#) or a dollar sign ($)
21 contain commands. Commands starting with a hash should be run as root,
22 commands starting with a dollar should be run as a normal user (in this
23 document, we assume that user is called 'bitcoin'). We also assume the
24 bitcoin user has sudo rights, so we use '$ sudo command' when we need to.
25
26 Strings that are surrounded by "lower than" and "greater than" ( < and > )
27 should be replaced by the user with something appropriate. For example,
28 <password> should be replaced by a user chosen password. Do not confuse this
29 notation with shell redirection ('command < file' or 'command > file')!
30
31 Lines that lack hash or dollar signs are pastes from config files. They
32 should be copied verbatim or adapted, without the indentation tab.
33
34 apt-get install commands are suggestions for required dependencies.
35 They conform to an Ubuntu 13.04 system but may well work with Debian
36 or earlier and later versions of Ubuntu.
37
38 Prerequisites
39 -------------
40
41 **Expertise.** You should be familiar with Linux command line and
42 standard Linux commands. You should have basic understanding of git,
43 Python packages. You should have knowledge about how to install and
44 configure software on your Linux distribution. You should be able to
45 add commands to your distribution's startup scripts. If one of the
46 commands included in this document is not available or does not
47 perform the operation described here, you are expected to fix the
48 issue so you can continue following this howto.
49
50 **Software.** A recent Linux 64-bit distribution with the following software
51 installed: `python`, `easy_install`, `git`, standard C/C++
52 build chain. You will need root access in order to install other software or
53 Python libraries. 
54
55 **Hardware.** The lightest setup is a pruning server with diskspace 
56 requirements well under 1 GB growing very moderately and less taxing 
57 on I/O and CPU once it's up and running. However note that you also need
58 to run bitcoind and keep a copy of the full blockchain, which is roughly
59 9 GB in April 2013. If you have less than 2 GB of RAM make sure you limit
60 bitcoind to 8 concurrent connections. If you have more ressources to 
61 spare you can run the server with a higher limit of historic transactions 
62 per address. CPU speed is also important, mostly for the initial block 
63 chain import, but also if you plan to run a public Electrum server, which 
64 could serve tens of concurrent requests. Any multi-core x86 CPU ~2009 or
65 newer other than Atom should do for good performance.
66
67 Instructions
68 ------------
69
70 ### Step 1. Create a user for running bitcoind and Electrum server
71
72 This step is optional, but for better security and resource separation I
73 suggest you create a separate user just for running `bitcoind` and Electrum.
74 We will also use the `~/bin` directory to keep locally installed files
75 (others might want to use `/usr/local/bin` instead). We will download source
76 code files to the `~/src` directory.
77
78     # sudo adduser bitcoin --disabled-password
79     # su - bitcoin
80     $ mkdir ~/bin ~/src
81     $ echo $PATH
82
83 If you don't see `/home/bitcoin/bin` in the output, you should add this line
84 to your `.bashrc`, `.profile` or `.bash_profile`, then logout and relogin:
85
86     PATH="$HOME/bin:$PATH"
87
88 ### Step 2. Download and install Electrum
89
90 We will download the latest git snapshot for Electrum and 'install' it in
91 our ~/bin directory:
92
93     $ mkdir -p ~/src/electrum
94     $ cd ~/src/electrum
95     $ sudo apt-get install git
96     $ git clone https://github.com/spesmilo/electrum-server.git server
97     $ chmod +x ~/src/electrum/server/server.py
98     $ ln -s ~/src/electrum/server/server.py ~/bin/electrum-server
99
100 ### Step 3. Download Bitcoind stable & patch it
101
102 In order for the latest versions of Electrum to work properly we currently recommend bitcoind 0.8.5 stable.  
103 0.8.5 can be downloaded from github or sourceforge and it needs to be patched with an electrum specific patch.
104 bitcoin@master i.e. git head may not currently work with electrum-server even if the patch applies cleanly.
105
106     $ cd ~/src && wget http://sourceforge.net/projects/bitcoin/files/Bitcoin/bitcoin-0.8.5/bitcoin-0.8.5-linux.tar.gz
107     $ tar xfz bitcoin-0.8.5-linux.tar.gz
108     $ cd bitcoin-0.8.5-linux/src
109     $ patch -p1 < ~/src/electrum/server/patch/patch
110     $ cd src
111     $ sudo apt-get install make g++ python-leveldb libboost-all-dev libssl-dev libdb++-dev 
112     $ make USE_UPNP= -f makefile.unix
113     $ strip ~/src/bitcoin-0.8.5-linux/src/src/bitcoind
114     $ ln -s ~/src/bitcoin-0.8.5-linux/src/src/bitcoind ~/bin/bitcoind
115
116 ### Step 4. Configure and start bitcoind
117
118 In order to allow Electrum to "talk" to `bitcoind`, we need to set up a RPC
119 username and password for `bitcoind`. We will then start `bitcoind` and
120 wait for it to complete downloading the blockchain.
121
122     $ mkdir ~/.bitcoin
123     $ $EDITOR ~/.bitcoin/bitcoin.conf
124
125 Write this in `bitcoin.conf`:
126
127     rpcuser=<rpc-username>
128     rpcpassword=<rpc-password>
129     daemon=1
130
131 Restart `bitcoind`:
132
133     $ bitcoind
134
135 Allow some time to pass, so `bitcoind` connects to the network and starts
136 downloading blocks. You can check its progress by running:
137
138     $ bitcoind getinfo
139
140 You should also set up your system to automatically start bitcoind at boot
141 time, running as the 'bitcoin' user. Check your system documentation to
142 find out the best way to do this.
143
144 ### Step 5. Install Electrum dependencies
145
146 Electrum server depends on various standard Python libraries. These will be
147 already installed on your distribution, or can be installed with your
148 package manager. Electrum also depends on two Python libraries which we will
149 need to install "by hand": `JSONRPClib`.
150
151     $ sudo apt-get install python-setuptools
152     $ sudo easy_install jsonrpclib
153     $ sudo apt-get install python-openssl
154
155 ### Step 6. Install leveldb
156
157     $ sudo apt-get install python-leveldb
158  
159 See the steps in README.leveldb for further details, especially if your system
160 doesn't have the python-leveldb package.
161
162 ### Step 7. Select your limit
163
164 Electrum server uses leveldb to store transactions. You can choose
165 how many spent transactions per address you want to store on the server.
166 The default is 100, but there are also servers with 1000 or even 10000.
167 Few addresses have more than 10000 transactions. A limit this high
168 can be considered to be equivalent to a "full" server. Full servers previously
169 used abe to store the blockchain. The use of abe for electrum servers is now
170 deprecated.
171
172 The pruning server uses leveldb and keeps a smaller and
173 faster database by pruning spent transactions. It's a lot quicker to get up
174 and running and requires less maintenance and diskspace than abe.
175
176 The section in the configuration file looks like this:
177
178      [leveldb]
179      path = /path/to/your/database
180      # for each address, history will be pruned if it is longer than this limit
181      pruning_limit = 100
182
183 ### Step 8. Import blockchain into the database or download it
184
185 As of April 2013 it takes between 6-24 hours to import 230k of blocks, depending
186 on CPU speed, I/O speed and selected pruning limit.
187
188 It's considerably faster to index in memory. You can use /dev/shm or indexing in RAM
189 or create a tmpfs which will also use swap if you run out of memory:
190
191     $ sudo mount -t tmpfs -o rw,nodev,nosuid,noatime,size=6000M,mode=0777 none /tmpfs
192
193 At limit 100 the database comes to 2,6 GB with 230k blocks and takes roughly 6h to import in /dev/shm.
194 At limit 1000 the database comes to 3,0 GB with 230k blocks and takes roughly 10h to import in /dev/shm.
195 At limit 10000 the database comes to 3,5 GB with 230k blocks and takes roughly 24h to import in /dev/shm.
196
197 Alternatively you can fetch a pre-processed leveldb from the net
198
199 You can fetch recent copies of electrum leveldb databases and further instructions 
200 from the Electrum full archival server foundry at:
201 http://foundry.electrum.org/ 
202
203
204 ### Step 9. Create a self-signed SSL cert
205
206 To run SSL / HTTPS you need to generate a self-signed certificate
207 using openssl. You could just comment out the SSL / HTTPS ports in the config and run 
208 without, but this is not recommended.
209
210 Use the sample code below to create a self-signed cert with a recommended validity 
211 of 5 years. You may supply any information for your sign request to identify your server.
212 They are not currently checked by the client except for the validity date.
213 When asked for a challenge password just leave it empty and press enter.
214
215     $ openssl genrsa -des3 -passout pass:x -out server.pass.key 2048
216     $ openssl rsa -passin pass:x -in server.pass.key -out server.key
217     writing RSA key
218     $ rm server.pass.key
219     $ openssl req -new -key server.key -out server.csr
220     ...
221     Country Name (2 letter code) [AU]:US
222     State or Province Name (full name) [Some-State]:California
223     Common Name (eg, YOUR name) []: electrum-server.tld
224     ...
225     A challenge password []:
226     ...
227
228     $ openssl x509 -req -days 730 -in server.csr -signkey server.key -out server.crt
229
230 The server.crt file is your certificate suitable for the ssl_certfile= parameter and
231 server.key corresponds to ssl_keyfile= in your electrum server config
232
233 Starting with Electrum 1.9 the client will learn and locally cache the SSL certificate 
234 for your server upon the first request to prevent man-in-the middle attacks for all
235 further connections.
236
237 If your certificate is lost or expires on the server side you currently need to run
238 your server with a different server name along with a new certificate for this server.
239 Therefore it's a good idea to make an offline backup copy of your certificate and key
240 in case you need to restore it.
241
242 ### Step 10. Configure Electrum server
243
244 Electrum reads a config file (/etc/electrum.conf) when starting up. This
245 file includes the database setup, bitcoind RPC setup, and a few other
246 options.
247
248     $ sudo cp ~/src/electrum/server/electrum.conf.sample /etc/electrum.conf
249     $ sudo $EDITOR /etc/electrum.conf
250
251 Go through the sample config options and set them to your liking.
252 If you intend to run the server publicly have a look at README-IRC.md 
253
254 ### Step 11. Tweak your system for running electrum
255
256 Electrum server currently needs quite a few file handles to use leveldb. It also requires
257 file handles for each connection made to the server. It's good practice to increase the
258 open files limit to 16k. This is most easily achived by sticking the value in .bashrc of the
259 root user who usually passes this value to all unprivileged user sessions too.
260
261     $ sudo sed -i '$a ulimit -n 16384' /root/.bashrc
262
263 We're aware the leveldb part in electrum server may leak some memory and it's good practice to
264 to either restart the server once in a while from cron (preferred) or to at least monitor 
265 it for crashes and then restart the server. Weekly restarts should be fine for most setups.
266 If your server gets a lot of traffic and you have a limited amount of RAM you may need to restart
267 more often.
268
269 Two more things for you to consider:
270
271 1. To increase security you may want to close bitcoind for incoming connections and connect outbound only
272
273 2. Consider restarting bitcoind (together with electrum-server) on a weekly basis to clear out unconfirmed
274    transactions from the local the memory pool which did not propagate over the network
275
276 ### Step 12. (Finally!) Run Electrum server
277
278 The magic moment has come: you can now start your Electrum server:
279
280     $ electrum-server
281
282 You should see this on the screen:
283
284     starting Electrum server
285     cache: yes
286
287 If you want to stop Electrum server, open another shell and run:
288
289     $ electrum-server stop
290
291 You should also take a look at the 'start' and 'stop' scripts in
292 `~/src/electrum/server`. You can use them as a starting point to create a
293 init script for your system.
294
295 ### Step 13. Test the Electrum server
296
297 We will assume you have a working Electrum client, a wallet and some
298 transactions history. You should start the client and click on the green
299 checkmark (last button on the right of the status bar) to open the Server
300 selection window. If your server is public, you should see it in the list
301 and you can select it. If you server is private, you need to enter its IP
302 or hostname and the port. Press Ok, the client will disconnect from the
303 current server and connect to your new Electrum server. You should see your
304 addresses and transactions history. You can see the number of blocks and
305 response time in the Server selection window. You should send/receive some
306 bitcoins to confirm that everything is working properly.
307
308 ### Step 13. Join us on IRC, subscribe to the server thread
309
310 Say hi to the dev crew, other server operators and fans on 
311 irc.freenode.net #electrum and we'll try to congratulate you
312 on supporting the community by running an Electrum node
313
314 If you're operating a public Electrum server please subscribe
315 to or regulary check the following thread:
316 https://bitcointalk.org/index.php?topic=85475.0
317 It'll contain announcements about important updates to Electrum
318 server required for a smooth user experience.