Skip to content

Instantly share code, notes, and snippets.

@trongthanh
Created March 5, 2018 08:11
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 trongthanh/e6c3ae16a39c847184dd4f3ad825e97d to your computer and use it in GitHub Desktop.
Save trongthanh/e6c3ae16a39c847184dd4f3ad825e97d to your computer and use it in GitHub Desktop.
Goalify Chat server instance init
#!/bin/bash
# NOTE: The commands here only applicable for Ubuntu 16.04 Xenial, do not use it for other distros
# Get user inputs for some customizable variables
# NOTE: map domain to this VPS instance first
read -p "Domain: " DOMAIN
# Update server to latest packages
sudo apt update && sudo apt upgrade -y
# Install prerequisite software
sudo apt install -y software-properties-common nginx git graphicsmagick
# Add Mongo DB app source
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
# Add NodeJS app source (which already include npm)
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
# Add certbot app source
sudo add-apt-repository -y ppa:certbot/certbot
# Install needed software
sudo apt update && sudo apt install -y python-certbot-nginx mongodb-org nodejs
# TODO: enable mongo replica set (https://rocket.chat/docs/installation/manual-installation/ubuntu/)
# Start mongod service
sudo systemctl start mongod
# Enable mongod service auto restart
sudo systemctl enable mongod
# Create app user
sudo useradd goalifychat
# Add rocketchat build public key
gpg --keyserver ha.pool.sks-keyservers.net --recv-keys 0E163286C20D07B9787EBE9FD7F9D0414FD08104
# TODO: we may skip this verification step
RC_VERSION="0.63.0-develop"
# OR
# RC_VERSION="latest"
# Download built Rocket Chat server
# adapted from https://github.com/RocketChat/Rocket.Chat/blob/develop/.docker/Dockerfile
set -x \
&& curl -SLf "https://releases.rocket.chat/${RC_VERSION}/download" -o rocket.chat.tgz \
&& curl -SLf "https://releases.rocket.chat/${RC_VERSION}/asc" -o rocket.chat.tgz.asc \
&& gpg --verify rocket.chat.tgz.asc \
&& sudo mkdir -p /app \
&& sudo tar -zxf rocket.chat.tgz -C /app \
&& rm rocket.chat.tgz rocket.chat.tgz.asc \
&& cd /app/bundle/programs/server \
&& sudo npm install \
&& sudo npm cache clear --force \
&& sudo chown -R goalifychat:goalifychat /app
# Create systemd service file for rocketchat server
NODE_PATH=`which node`
APP_PATH="/app/bundle"
cat > goalifychat.service <<EOF
[Unit]
Description=A Goalify chat app
Requires=mongod.service
After=mongod.service
[Service]
ExecStart=$NODE_PATH ${APP_PATH}/main.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=goalifychat
User=goalifychat
Group=goalifychat
Environment=MONGO_URL=mongodb://localhost:27017/goalifychat
Environment=ROOT_URL=https://$DOMAIN
Environment=PORT=3000
[Install]
WantedBy=multi-user.target
EOF
sudo cp goalifychat.service /etc/systemd/system/goalifychat.service
rm goalifychat.service
# start rocketchat server and make it run as service (auto start)
sudo systemctl start goalifychat.service && sudo systemctl enable goalifychat.service
# Create Nginx reversed proxy config:
cat > nginx-site.conf <<EOF
# Fix websocket proxying
map \$http_upgrade \$connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
listen [::]:80;
index index.html index.htm;
server_name $DOMAIN;
location / {
proxy_redirect off;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_set_header Host \$http_host;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_pass http://127.0.0.1:3000;
# Websocket proxying
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection \$connection_upgrade;
}
}
EOF
sudo cp nginx-site.conf /etc/nginx/sites-available/default
rm nginx-site.service
# Enable web proxies with secured SSL certificate from let's encrypt
sudo certbot --nginx
# NOTE: mannual, interactive inputs ahead
# Restart nginx with new config
sudo systemctl restart nginx
echo "Note: Remember to enable HTTP2 at /etc/nginx/sites-available/default"
echo "Goalifychat server system initialization complete!"
# Fix slack import:
# sudo rsub /app/bundle/programs/server/packages/rocketchat_importer.js
# Find and remove .native() at the bson import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment