Installing ElasticSearch on Webfaction
You read everywhere that setting up and running elasticsearch is very simple. In fact, it is. There are some hurdles to clear when you’re setting it up on Webfaction, however. These instructions will hopefully provide some guidance.
- Install JRE7
- cd ~/share/ (I use share; use whatever folder makes sense for you)
- wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/7u55-b13/jdk-7u55-linux-x64.tar.gz
- Extract into ~/share/java7 so you end up with ~/share/java7/bin, ~/share/java7/jre, etc.
- Edit ~/.bash_profile, add JAVA_HOME and java bin directory. Here’s an example:
123export JAVA_HOME=$HOME/share/java7PATH=$JAVA_HOME/bin:$PATH:$HOME/binexport PATH - source ~/.bash_profile
- java -version (should show 1.7x)
- Setup new app in my.webfaction.com
- Name: elasticsearch
- Type: Custom app (listening on port)
- Check the box to open firewall port
- Save
- Find new app in the list and see which port WF chose
- Copy the port number
- Download ElasticSearch
- Browse to ~/webapps/elasticsearch
- wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.0.0/elasticsearch-2.0.0.tar.gz
- Extract into ~/webapps/elasticsearch so you end up with ~/webapps/elasticsearch/bin, ~/webapps/elasticsearch/config, etc.
- Configure ElasticSearch
- vim ~/webapps/elasticsearch/config/elasticsearch.yml
- Set http.port to the port Webfaction chose when you setup the app
- Set http.host to 127.0.0.1 (if app accessing ElasticSearch is on same server)
- Set network.host to 127.0.0.1 (if app accessing ElasticSearch is on same server)
- Run elasticsearch
- ~/webapps/elasticsearch/bin/elasticsearch
Running Automatically
If you have a working elasticsearch, you may be wondering how to keep it running all the time, even after you log out.
One solution is to create a simple bash script that starts a process if it’s not already running, and then execute this script regularly via a cronjob.
elasticsearch creates many processes that appear to ps as “java”. This is not helpful. Using -eLf we can see which processes are related to elasticsearch AND java. If there is less than 1 such process, we start elasticsearch, otherwise we do nothing.
- vim ~/bin/elasticsearch_autostart.sh
-
12345678# This should be executed regularly via a cronjobif [ `ps -u YOURUSERNAME -eLf | grep -i elasticsearch | grep -i java | wc -l` -lt 1 ]thenecho "Starting elasticsearch."/home/YOURUSERNAME/webapps/elasticsearch/bin/elasticsearchelseecho "elasticsearch is running."fi
- chmod +x ~/bin/elasticsearch_autostart.sh
- crontab -e
- Example: (notice we redefine JAVA_HOME and PATH (without using $HOME or $PATH))
12345JAVA_HOME=/home/YOURUSERNAME/share/java7PATH=/home/YOURUSERNAME/share/java7/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/YOURUSERNAME/bin# Make sure elasticsearch is running (every minute)* * * * * /home/YOURUSERNAME/bin/elasticsearch_autostart.sh
5 Comments
Eduardo
Thank you very much Adam! I struggled with this a lot. I got it running now!
John Henry Donovan
Thanks for the tips. Did you have any trouble with a tmp directory when trying to start elasticsearch?
I’m getting an error like this.
unable to load JNA native support library, native methods will be disabled.
java.lang.UnsatisfiedLinkError: /tmp/jna-1971603474/jna7540736777577824257.tmp: /tmp/jna-1971603474/jna7540736777577824257.tmp: failed to map segment from shared object: Operation not permitted
And I have tried
~/webapps/elasticsearch/bin/elasticsearch -Djna.tmpdir=~/tmp
from elasticsearch docs but to no avail.
Any thought on this would be helpful. Thanks
John Henry Donovan
Fixed by adding an environment variable
export JAVA_OPTS=”-Djava.io.tmpdir=$HOME/tmp”
Brad Buran
FYI, one of my sites got moved to a new Webfaction server with many more CPU cores. Eleasticsearch determins how many threads to generate based on the number of cores the serve has. As a result, Elasticsearch was spawning way too many threads (exceeding the maximum limit of 600 per user on the server). To get around this, add the following line to your configuration.yml file:
processors: 4
Elasticsearch will then limit the number of threads it creates to a number that’s more reasonable for the shared hosting plan.
Jesse Hodges
Thanks so much for this! It’s very useful.
I think, however, that creating a webfaction app and opening a port is not necessary if you’re accessing locally.