Skip to content

Instantly share code, notes, and snippets.

@tsrivishnu
Last active May 31, 2016 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsrivishnu/88852ae643d92683acd626073bbd2268 to your computer and use it in GitHub Desktop.
Save tsrivishnu/88852ae643d92683acd626073bbd2268 to your computer and use it in GitHub Desktop.
Script to setup pgis on a new ubuntu machine. https://github.com/OpenGridMap/pgis
#!/bin/bash
set -e
# Install required packages
sudo apt-get update
sudo apt-get install -y git unzip \
postgresql-9.3 \
postgresql-9.3-postgis-2.1 \
postgresql-server-dev-9.3 \
nginx
# Create gis database and enable postgis extension
cd / # being in root's home and doing `sudo -u postgres` will warn
sudo -u postgres -H -- psql -c "CREATE DATABASE gis;"
sudo -u postgres -H -- psql -d gis -c "CREATE EXTENSION postgis;"
# Install miniconda
wget http://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
./Miniconda3-latest-Linux-x86_64.sh -b # -b for batch mode so no confirmation is needed
# Create virtual environment for the project
$HOME/miniconda3/bin/conda create -y -n pgisenv anaconda python=3.4.1
# TODO: Clone the project and cd into it
cd ~/
git clone git://github.com/OpenGridMap/pgis.git
cd pgis
cp database.yml.example database.yml
source $HOME/miniconda3/bin/activate pgisenv
pip install -r requirements.txt
pip install flask-resize
# Change postgresql password and add it to config.
rand_number=`openssl rand -base64 32`
POSTGRES_PASSWORD=${rand_number//\//} # Remove slashes other sed command with fail
sudo -u postgres psql -U postgres \
-d postgres \
-c "alter user postgres with password '$POSTGRES_PASSWORD';"
# Add postgres username and password to database.yml file.
sed -i s/password:\s*.*/password:\ $POSTGRES_PASSWORD/g database.yml
sed -i s/user:\s*.*/user:\ postgres/g database.yml
./manage db upgrade
# Install node and npm for less asset compilation
sudo apt-get -y install nodejs
sudo ln -s /usr/bin/nodejs /usr/bin/node
sudo apt-get -y install npm
sudo npm install -g less
cd ~
# Install k-means extension for postgres
wget http://api.pgxn.org/dist/kmeans/1.1.0/kmeans-1.1.0.zip
unzip kmeans-1.1.0.zip
cd kmeans-1.1.0/
export USE_PGXS=1 # in bash
sudo make
sudo make install
sudo -u postgres psql -f /usr/share/postgresql/9.3/extension/kmeans.sql \
-U postgres \
-d gis
cd ../pgis
sudo bash -c "cat >/etc/init.d/pgis-server" <<EOL
#!/bin/bash
# Run pgis server in a screen
screen -dmS pgis-server \
bash -c "\
source $HOME/miniconda3/bin/activate pgisenv;\
cd $HOME/pgis;\
gunicorn app:GisApp --bind localhost:3000"
sudo service nginx restart
EOL
# Install init.d script to run server on boot
sudo chmod +x /etc/init.d/pgis-server
sudo update-rc.d pgis-server defaults
## nginx setup
sudo mv /etc/nginx/sites-enabled/default /etc/nginx/sites-available/default.enabled
sudo bash -c "cat >/etc/nginx/sites-enabled/pgis" <<EOL
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
EOL
/etc/init.d/pgis-server
echo "If all good, your server must be running"
@tsrivishnu
Copy link
Author

tsrivishnu commented Apr 30, 2016

To install with the script,

wget https://gist.githubusercontent.com/tsrivishnu/88852ae643d92683acd626073bbd2268/raw/ -O setup_pgis_server
chmod 755 setup_pgis_server
./setup_pgis_server

A oneliner?

wget https://gist.githubusercontent.com/tsrivishnu/88852ae643d92683acd626073bbd2268/raw -O setup_pgis_server; chmod 755 setup_pgis_server; ./setup_pgis_server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment