Skip to content

Instantly share code, notes, and snippets.

@olearytd
Last active January 10, 2018 23:59
Show Gist options
  • Save olearytd/4f18a31ec506e3ba7eee173261dfd8a6 to your computer and use it in GitHub Desktop.
Save olearytd/4f18a31ec506e3ba7eee173261dfd8a6 to your computer and use it in GitHub Desktop.

Instructions for Deploying Tasking Manager 3 on Centos 7 using Apache or Nginx

These instructions were tested using a fresh Centos 7 VM

Install Dependencies

General Deps

sudo yum install -y epel-release
sudo yum update -y
sudo yum install -y vim git wget bzip2 nano htop

PostgreSQL-9.5

Download and Install
sudo rpm -ivh http://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm
sudo yum install postgresql95 postgresql95-server postgresql95-libs postgresql95-contrib postgresql95-devel
sudo /usr/pgsql-9.5/bin/postgresql95-setup initdb
sudo service postgresql-9.5 start
sudo chkconfig postgresql-9.5 on
sudo yum install postgis2_95 

Create Postgresql Users

sudo -u postgres createuser -s centos
sudo -u postgres createuser -s apache

Node

Install deps
sudo yum install gcc gcc-c++
sudo yum groupinstall "Development Tools"
Download/Install Node

https://www.liquidweb.com/kb/how-to-install-nvm-node-version-manager-for-node-js-on-centos-7/

curl https://raw.githubusercontent.com/creationix/nvm/v0.25.0/install.sh | bash

---Exit and Re-enter server

nvm install 8.4
nvm alias default 8.4
Check node version
node --version

Python 3.6

Download Python3.6
sudo yum install https://centos7.iuscommunity.org/ius-release.rpm
sudo yum install python36u python36u-pip python36u-devel
python3.6 -V

Install Tasking-Manager-3

Clone Repo

cd /var/www/html
sudo git clone https://github.com/hotosm/tasking-manager
sudo chown -R centos:centos tasking-manager/

Install Node Deps

cd tasking-manager/
cd client/
npm install

npm install gulp -g
gulp build

Create Python Virtual Env

cd ../
python3.6 -m venv ./venv
. ./venv/bin/activate
pip3 install -r requirements.txt

Create Postgresql Database

sudo -u postgres psql
CREATE USER "hottm" PASSWORD 'hottm';
CREATE DATABASE "tasking-manager" OWNER "hottm";
\c "tasking-manager";
CREATE EXTENSION postgis;

Create Server Env Variables

sudo vim ~/.bash_profile
PATH=$PATH:$HOME/.local/bin:$HOME/bin
TM_DB=postgresql://hottm:hottm@localhost/tasking-manager
TM_SECRET=secret-key-here
TM_CONSUMER_KEY=oauth-consumer-key-goes-here
TM_CONSUMER_SECRET=oauth-consumer-secret-key-goes-here
TM_SMTP_PASSWORD=smtp-server-password-here
TASKING_MANAGER_ENV=Dev

export PATH
export TM_DB
export TM_SECRET
export TM_CONSUMER_KEY
export TM_CONSUMER_SECRET
export TM_SMTP_PASSWORD
export TASKING_MANAGER_ENV
source ~/.bash_profile

Initialize DB

python3.6 manage.py db upgrade

Run Dev Server

Test out your current installation

python3.6 manage.py runserver -d

Run Gunicorn

Now try out running the server via gunicorn. This should be installed already in your venv via the project's requirements.txt.

gunicorn manage:application

Or run gunicorn with host if you're using a VM

gunicorn -b 0.0.0.0:8000 -w 7 --timeout 179 manage:application

Run TM3 as a UNIX file socket

We want to set up the application with a unix file socket so we can work with NGINX or Apache.

Note that -w 7 means that we have 7 workers. This could be calculated by: ((cores x 2) + 1))

gunicorn -b unix:tm3.sock -w 7 --timeout 179 manage:application

Apache

Install Apache
sudo yum install httpd httpd-devel
Edit Apache config file with new VirtualHost
sudo vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
  ServerName http://localhost

  DocumentRoot /var/www/html/tasking-manager/server/web/static/dist

  <Location /api>
        ProxyPass http://unix:/var/www/html/tasking-manager/tm3.sock
        ProxyPassReverse http://unix:/var/www/html/tasking-manager/tm3.sock
  </Location>

</VirtualHost>

Replace the paths to wherever the static assets as well as socket file are on your file system.

sudo service httpd restart

If you're using Apache, skip the Nginx section

NGINX

Install NGINX
sudo yum install nginx
Create proxy_params file
sudo vim /etc/nginx/proxy_params
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Create server config in nginx file (comment out default port 80 server entry)
sudo vim /etc/nginx/nginx.conf
    server {
        listen 80;
        location / {
         root /var/www/html/tasking-manager/server/web/static/dist;
         try_files $uri $uri/ @api;
         expires max;
            client_max_body_size 200M;
         access_log off;
     }
     location @api {
         include proxy_params;
          proxy_pass http://unix:/var/www/html/tasking-manager/tm3.sock;
     }
    }

Replace the paths to wherever the static assets as well as socket file are on your file system.

sudo service nginx start

Create systemd Service

sudo vim /etc/systemd/system/tm3.service
[Unit]
Description=tm3 gunicorn daemon
After=network.target
[Service]
User=centos
Group=centos
WorkingDirectory=/var/www/html/tasking-manager
Environment=TM_DB=postgresql://hottm:hottm@localhost/tasking-manager
Environment=TM_CONSUMER_SECRET=OSM-Oauth-Consumer-Secret-Key
Environment=TM_SECRET=secret-key-here
Environment=TM_CONSUMER_KEY=OSM-Oauth-Consumer-Key
Environment=TM_ENV=Prod
ExecStart=/var/www/html/tasking-manager/venv/bin/gunicorn -b unix:/var/www/html/tasking-manager/tm3.sock -w 7 --timeout 179 manage:application
Restart=always
[Install]
WantedBy=multi-user.target

Set your environment variables, paths, and the correct user for your needs.

Setup Prod in config.py

You are not HOT, so your production setup will be different. For my personal prod setup, I changed ProdConfig in config.py to:

class ProdConfig(EnvironmentConfig):
 APP_BASE_URL = 'http://127.0.0.1'
    API_DOCS_URL = f'{APP_BASE_URL}/api-docs/swagger-ui/index.html?' + \
                   'url=http://127.0.0.1/api/docs'
    LOG_DIR = '/var/log/tasking-manager-logs'
    LOG_LEVEL = logging.DEBUG

Create Log Dir

sudo mkdir /var/log/tasking-manager-logs
sudo chmod a+rwx /var/log/tasking-manager-logs

Initalize Service

sudo systemctl start tm3
sudo systemctl enable tm3

View tm3 site at your web address root

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