Skip to content

Instantly share code, notes, and snippets.

@hallahan
Last active February 14, 2017 13:00
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hallahan/60c956987bde4a02b6be to your computer and use it in GitHub Desktop.
Save hallahan/60c956987bde4a02b6be to your computer and use it in GitHub Desktop.
SpatialServer Installation Instructions

Preliminary Packages

Make sure you have done sudo apt-get update and sudo apt-get upgrade on your fresh install. This guide is geared toward Ubuntu 14.04 LTS (Trusty Tahr). Most of the guide will work for earlier versions of Ubuntu with some slight changes, but 14.04 is advised.

Enable the root user

sudo cp /home/ubuntu/.ssh/authorized_keys /root/.ssh/

SSH in as root

ssh -i /CountryMapper.pem root@54.191.215.52

Change Host Name

You might want to have the host name of your Ubuntu instance reflect the actual host name that it represents:

echo "spatialserver.spatialdev.com" > /etc/hostname
hostname -F /etc/hostname
reboot

Install PostGIS

You may install PostGIS before or after you install SpatialServer. If you do not plan on using PostGIS, you can skip this section.

sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" >> /etc/apt/sources.list'
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
apt-get update
apt-get install postgresql-9.3
apt-get install postgresql-9.3-postgis-2.1
apt-get install postgresql-contrib
su postgres
psql
CREATE EXTENSION adminpack;
\q
exit

Now you have a working PostgreSQL database with PostGIS extensions. If you are unable to run spatial functions on a database, you may need to explicitly enable PostGIS for that given database. Make sure you are in your database, then, execute the following SQL line by line:

-- Enable PostGIS (includes raster)
CREATE EXTENSION postgis;
-- Enable Topology
CREATE EXTENSION postgis_topology;
-- fuzzy matching needed for Tiger
CREATE EXTENSION fuzzystrmatch;
-- Enable US Tiger Geocoder
CREATE EXTENSION postgis_tiger_geocoder;

Tune Your Database

The default settings for your standard apt-get PostgreSQL install are not optimal for most machines. The following variables work well on an Amazon EC2 T2 Small Ubuntu 14.04 Instance. This instance has 1 virtual CPU and 2GB of RAM. It costs approx. $20 / month. Set the following variables in your /etc/postgresql/9.3/main/postgresql.conf file:

shared_buffers = 1GB
work_mem = 30MB
maintenance_work_mem = 300MB
max_stack_depth = 3MB
checkpoint_segments = 32

To enable these new settings, you need to restart Postgres. To do so, execute:

service postgresql restart

At this point, you have seen Postgres work on your system. You interacted with it within psql. By default, this database is not accessable from the outside world. To do so, you add the following line to pg_hba.conf:

host	all				all				0.0.0.0/0				md5

Then, edit postgresql.conf and uncomment:

listen_address = "localhost" to "*"

You will want to also give a password to your postgres user since it will be open to the outside world.

passwd postgres

And type in twice your intended password.

Now you will want the updates to take place, so restart Postgresql:

service postgresql restart

Though you just set a password for the system username postgres, you have not changed the password for the user postgres in actual PostgreSQL.

sudo -u postgres psql postgres
\password postgres

Enter your password twice.

At this point, you should be able to access Postgres on this EC2 server from your local machine. If not, make sure that port 5432 is open on your Amazon Security Group settings.

Install Git

apt-get install git

Install NodeJS

apt-get install nodejs
apt-get install npm
ln -s /usr/bin/nodejs /usr/bin/node

Install Forever

ForeverJS will run the SpatailServer NodeJS application. This tool restarts and keeps the app alive if it crashes. It also logs the output.

npm install -g forever

Install SpatialServer

Log in as ubuntu. We want to be doing this not as root -- a separation of responsibilities. Note that we are using the master_nopg branch.

git clone https://github.com/spatialdev/PGRestAPI.git
git checkout master_nopg
cd PGRestAPI
npm install

Now, we need a settings.js file to declare the settings that SpatialServer will use to run. Paste the following as settings.js in the PGRestAPI directory.

//Settings.js is not part of the repository.  However, it should be deployed with the application and contain deployment-specific settings.
//there is a settings.js.example file that should match the structure and properties of this file that IS checked in to the repo.
var settings = {}

settings.pg = {};
settings.application = {};
settings.tilestream = {};

//application port settings
settings.application.port = 3001;
//settings.application.ip = "localhost"; //Typically localhost.  If deploying on Amazon EC2 Ubuntu, comment out this line.

//Settings for postgres DB
settings.pg.username = 'postgres';
settings.pg.password = '<password>';
settings.pg.server = 'localhost';
settings.pg.port = '5432';
settings.pg.database = 'test';
settings.pg.featureLimit = 1000; //how many features to return by default

settings.tilestream.host = ""; //54.212.254.185
settings.tilestream.path = ""; ///api/Tileset
settings.tilestream.port = ""; //8888

//Should the API display postgres views?
settings.displayViews = true;

//Should the API display postgres tables?
settings.displayTables = true;

//Should the API hide any postgres tables or views?
settings.pg.noFlyList = ["att_0"];

//The list of formats to be returned by the Table Query REST endpoint.  If ogr2ogr is installed, .shp will be added automatically.
settings.application.formatList =[ 'html', 'GeoJSON', 'esriJSON'];

//Where to write out GeoJSON files?
settings.application.geoJsonOutputFolder = "/public/geojson/output/";

//Optional.  If you're using port forwarding or URL rewriting, but need to display full URLs to your assets, this will stand in for the host.
settings.application.publichost = ""; //like myhost.com. Keep this empty if you want to use the default host
settings.application.publicport = "80";

//Epxress Session Secret
settings.epxressSessionSecret = "spdv";

// //MongoDB Authentication
// settings.mongodb = {};
// settings.mongodb.db = "mongodb://localhost/authentication";

// //Facebook App ID
// settings.facebook = {};
// settings.facebook.appid = "12345";
// settings.facebook.secret = "secret";


//Leaflet version reference
settings.leaflet = {};
settings.leaflet.js = 'http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js?2';
settings.leaflet.css = 'http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css';

settings.jquery = {};
settings.jquery.js = 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js';

module.exports = settings;

Now, try out running SpatialServer and see if it works. To do this temporarily, execute:

node app.js

It should show Chubbs server listening on port 3001

Ctrl-c to get outa there...

Now, we want to have forever run this app. The following command and arguments have shown to be more robust when the app crashes:

sudo forever --minUptime 500ms --spinSleepTime 500ms start app.js

To see which apps you have running in forever, you execute:

sudo forever list

Then, you use that number in the brackets ([0]) to stop the desired process. in this case, it is 0, so:

sudo forever stop 0

Turn the app back on with sudo forever --minUptime 500ms --spinSleepTime 500ms start app.js. Now, we want the app to start automaically when you restart the EC2. To do this, we add a line to cron.

To edit cron, type:

crontab -e

Then, a text editor will open and you add this line at the end:

@reboot sudo PORT=3100 forever --minUptime 500ms --spinSleepTime 500ms start /home/ubuntu/PGRestAPI/app.js
@reboot sudo PORT=3101 forever --minUptime 500ms --spinSleepTime 500ms start /home/ubuntu/PGRestAPI/app.js
@reboot sudo PORT=3102 forever --minUptime 500ms --spinSleepTime 500ms start /home/ubuntu/PGRestAPI/app.js
@reboot sudo PORT=3103 forever --minUptime 500ms --spinSleepTime 500ms start /home/ubuntu/PGRestAPI/app.js

Install and Setup NGINX

For this work, you should be logged in as root.

apt-get install nginx
service nginx start

If you go to http://54.191.215.52/, you will see that NGINX is running on port 80.

Save the following as /etc/nginx/sites-available/countrymapper.

# the IP(s) on which your node server is running. I chose port 3001.
upstream app_countrymapper {
    server 127.0.0.1:3001;
}

# the secure nginx server instance
server {
    listen 0.0.0.0:80;
    server_name 54.191.215.52 countrymapper;
    access_log /var/log/nginx/countrymapper.log;
    error_log /var/log/nginx/countrymapper.log;
    # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options

    location /favicon.ico { alias /home/ubuntu/public/favicon.ico; }

    location / {
      # auth_basic "Restricted";
      # auth_basic_user_file /home/ubuntu/GeoForce/GeoAngular/app/.htpasswd;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_set_header X-Ssl off;

      proxy_pass http://app_countrymapper;
      proxy_redirect off;
    }
}


Also, we want to enable gzip compression of responses via NGINX. Do this by setting the folling Gzip Settings in /etc/nginx/nginx.conf:

	##
	# Gzip Settings
	##

    # from http://aspyct.org/blog/2012/08/20/setting-up-http-cache-and-gzip-with-nginx/
	gzip on;
	gzip_http_version 1.1;
	gzip_vary on;
	gzip_proxied any;
	gzip_comp_level 6;
	gzip_proxied any;
	gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
	gzip_buffers 16 8k;
	gzip_disable "MSIE [1-6]\.(?!.*SV1)";

We then enable this config:

cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/countrymapper countrymapper
service nginx restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment