Skip to content

Instantly share code, notes, and snippets.

@WillSams
Last active January 25, 2019 23:46
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 WillSams/8006752e67c3b046230332fa7403b0d3 to your computer and use it in GitHub Desktop.
Save WillSams/8006752e67c3b046230332fa7403b0d3 to your computer and use it in GitHub Desktop.
Install script for Nginx & Keystone JS for Ubuntu 18.04 'bionic'
#!/bin/bash
# Nginx installer script - Keystone JS example
#
# Pre-req:
# * Ubuntu Bionic (18.04) flavored distro
# * Script must be executed under the account of the web admininstrator
# * linux-devel-setup.sh - https://gist.github.com/WillSams/f17427b5a4f5bccc23d6efe1389b33ca
# * mongodb-setup.sh - https://gist.github.com/WillSams/3588b69c805acf07ccb278c55fd91302
# Note: If you don't have Nginx installed, this script will install and do basic setup for you.
# Additional notes:
# If you want to setup Nginx w/ GrandNode (.NET), see my nginx-grandnode.sh gist.
# If you want to setup Nginx w/ Rails (Ruby), see my nginx-rails.sh gist.
# Running the two above scripts on the same web server won't break this script.
set -o nounset # unset variables are errors
__ScriptVersion="2018.12.08"
__ScriptName="nginx-keystone.sh"
__ScriptFullName="$0"
if [ -d /etc/nginx ]; then
echo "Nginx is already installed on this system."
else
echo "**************************** NGINX ******************************"
echo "Installing nginx web server."
echo "You may be prompted for root credentials to complete the install."
echo "*****************************************************************"
sudo bash -c "add-apt-repository ppa:certbot/certbot -y"
sudo sudo bash -c "apt update && apt upgrade -y"
sudo bash -c "apt install nginx python-certbot-nginx -y"
sudo bash -c "ufw allow 'NGINX FULL' && ufw enable"
sudo bash -c "rm -v /etc/nginx/sites-available/default"
sudo bash -c "sed -e '/include \/etc\/nginx\/sites-enabled/ s/^#*/#/' -i /etc/nginx/nginx.conf"
sudo bash -c "echo '#Image Caching
include \/etc\/nginx\/conf.d\/img-cache.conf;' >> /etc/nginx/nginx.conf"
sudo bash -c "echo 'location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 90d;
add_header Pragma public;
add_header Cache-Control \"public\";
}' >> /etc/nginx/conf.d/img-cache.conf"
touch /etc/nginx/conf.d/default.conf
sudo bash -c "systemctl enable nginx && service nginx start"
fi
if [ -f /etc/systemd/system/example-nodejs.service ]; then
echo "Web project 'example-nodejs' appears to be already served."
else
echo "*********************** EXAMPLE-nodejs **************************"
echo "Installing the example-nodejs.com project to the nginx web server."
echo "You may be prompted for root credentials to complete the install."
echo "*****************************************************************"
echo "What is existing username for your Mongo DB admin account?" && read mongo_admin
echo "What is the name or IP of the mongo database server?" && read mongo_server
mongo mongodb://$mongo_server:27017/example-nodejs-com -u $mongo_admin -p --authenticationDatabase admin --eval 'db.createCollection("dummy")'
mongo mongodb://$mongo_server:27017/example-nodejs-com -u $mongo_admin -p --authenticationDatabase admin --eval 'db.createUser({"user":"example-nodejs-web-user","pwd":"passw0rd123","roles":[{"role":"dbOwner","db":"example-nodejs-com"}]})'
yo keystone auto
mv keystone-starter $HOME/example-nodejs.com && cd $HOME/example-nodejs.com
npm install node-sass-middleware --save && npm rebuild node-sass
echo "MONGO_URI=mongodb://example-nodejs-web-user:passw0rd123@$mongo_server:27017/example-nodejs-com" >> .env
sed -e '/\/\/ Start Keystone to connect/r'<( echo "keystone.set('port',3010);") -i -- keystone.js
sudo bash -c "mv $HOME/example-nodejs.com /var/www"
sudo bash -c "chown $USER:$USER -R /var/www/example-nodejs.com"
sudo bash -c "echo '##################################
upstream example-nodejs {
server 127.0.0.1:3010 fail_timeout=0;
}
server {
listen 80;
server_name example-nodejs.com;
root /var/www/example-nodejs.com;
try_files "'$uri'"/index.html "'$uri'" @example-nodejs;
location @example-nodejs {
proxy_set_header X-Forwarded-For "'$proxy_add_x_forwarded_for'";
proxy_set_header Host "'$http_host'";
proxy_redirect off;
proxy_pass http://127.0.0.1:3010;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
##################################' >> /etc/nginx/conf.d/default.conf"
sudo bash -c "echo '[Unit]
Description=Example-NodeJS PM2 start
After=network.target
[Service]
Type=forking
WorkingDirectory=/var/www/example-nodejs.com/
ExecStart=$HOME/.npm-global/bin/pm2 start keystone.js
Restart=always
RestartSec=10
SyslogIdentifier=nodejs-pm2-example-nodejs.com
User=$USER
Environment=PATH=/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=$HOME/.pm2
[Install]
WantedBy=multi-user.target' >> /etc/systemd/system/example-nodejs.service"
sudo bash -c "systemctl enable example-nodejs.service && service example-nodejs start"
sudo bash -c "service nginx restart"
sudo bash -c "echo '
0.0.0.0 example-nodejs.com' >> /etc/hosts"
fi
sudo sudo bash -c "apt autoremove && apt clean -y"
echo "Script complete. Visit the website locally on the server at http://example-nodejs.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment