Skip to content

Instantly share code, notes, and snippets.

@WillSams
Last active February 1, 2019 15:15
Show Gist options
  • Save WillSams/93a93f2a1dce81999d576ef8633d2ebb to your computer and use it in GitHub Desktop.
Save WillSams/93a93f2a1dce81999d576ef8633d2ebb to your computer and use it in GitHub Desktop.
Install script for Nginx & Ruby-on-Rails for Ubuntu 18.04 'bionic'
#!/bin/bash
# Nginx installer script - Ruby On Rails 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/ KeystoneJS (NodeJS), see my nginx-keystone.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.10.19"
SCRIPTNAME="nginx-rails.sh"
SCRIPTFULLNAME="$0"
RUBYVERSION=$(ruby -v)
RUBYVERSION=$(echo ${RUBYVERSION} | sed "s/.*ruby \([0-9.]*\).*/\1/")
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-ruby.service ]; then
echo "Web project 'example-ruby' appears to be already served."
else
echo "*********************** example-ruby **************************"
echo "Installing the example-ruby.com project to the nginx web server."
echo "You may be prompted for root credentials to complete the install."
echo "*****************************************************************"
if [ ! -d $HOME/example-ruby.com ]; then
rails new example-ruby.com --skip-active-record
cd $HOME/example-ruby.com
echo "
gem 'mongoid'
gem 'bson_ext'
gem 'unicorn'" >> Gemfile
bundle
else
cd $HOME/example-ruby.com
fi
echo "What is existing username for your Mongo DB admin account?" && read mongo_admin
echo "What is the name or IP address of the mongo database server?" && read mongo_server
mongo mongodb://$mongo_server:27017/example-ruby-com-dev -u $mongo_admin -p --authenticationDatabase admin --eval 'db.createUser({"user":"example-ruby-web-user-dev","pwd":"passw0rd123","roles":[{ "role": "dbOwner","db":"example-ruby-com-dev"}]})'
mongo mongodb://$mongo_server:27017/example-ruby-com-uat1 -u $mongo_admin -p --authenticationDatabase admin --eval 'db.createUser({"user":"example-ruby-web-user-uat","pwd":"passw0rd123","roles":[{ "role": "dbOwner", "db":"example-ruby-com-uat1"}]})'
rails g mongoid:config && rm config/mongoid.yml
echo "development:
clients:
default:
uri: mongodb://example-ruby-web-user-dev:passw0rd123@$mongo_server:27017/example-ruby-com-dev
test:
clients:
default:
uri: mongodb://example-ruby-web-user-uat:passw0rd123@$mongo_server:27017/example-ruby-com-uat1
options:
read:
mode: :primary
max_pool_size: 1
" >> config/mongoid.yml
sed -e '/require "rails\/test_unit\/railtie"/r'<(
echo "
require \"mongoid\"
Mongoid.load!(File.expand_path('mongoid.yml', './config'))"
) -i -- config/application.rb
#Adapted the following Rails tutorial into this script: https://richonrails.com/articles/mongodb-and-rails
rails g model person first_name last_name email notes
rails g controller people index new create edit update destroy
rails db:migrate
rm config/routes.rb
echo 'Rails.application.routes.draw do
resources :people, except: [:show]
root to: "people#index"
end' >> config/routes.rb
rm app/controllers/people_controller.rb
echo "class PeopleController < ApplicationController
def index
@people = Person.all
end
def new
@person = Person.new
end
def create
@person = Person.new(person_params)
if @person.save
redirect_to people_path, notice: \"The person has been created!\" and return
end
render 'new'
end
def edit
@person = Person.find(params[:id])
end
def update
@person = Person.find(params[:id])
if @person.update_attributes(person_params)
redirect_to people_path, notice: \"#{first_name} #{last_name} has been updated!\" and return
end
render 'edit'
end
def destroy
@person = Person.find(params[:id])
@person.destroy
redirect_to people_path, notice: \"#{first_name} #{last_name} has been deleted!\" and return
end
private
def person_params
params.require(:person).permit(:first_name, :last_name, :email, :notes)
end
end" >> app/controllers/people_controller.rb
sed -e '/<%= csp_meta_tag %>/r'<(
echo "
<%= stylesheet_link_tag 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css' %>
<%= javascript_include_tag 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js' %>"
) -i -- app/views/layouts/application.html.erb
echo '<%= form_for @person do |f| %>
<div class="form-group">
<%= f.label :first_name %>
<%= f.text_field :first_name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :last_name %>
<%= f.text_field :last_name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :notes %>
<%= f.text_area :notes, class: "form-control" %>
</div>
<%= f.submit class: "btn btn-primary" %>
<% end %>' >> app/views/people/_form.html.erb
echo '<h1>New Person</h1>
<div class="well">
<%= render "form" %>
</div>' >> app/views/people/new.html.erb
echo '<h1>New Person</h1>
<div class="well">
<%= render "form" %>
</div>' >> app/views/people/edit.html.erb
echo '<h1>People</h1>
<div class="well">
<%= link_to "New Person", new_person_path, class: "btn btn-primary" %>
</div>
<% if flash[:notice] %>
<div class="alert alert-success"><%= flash[:notice] %></div>
<% end %>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Notes</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</tr>
</thead>
<tbody>
<% if @people.size == 0 %>
<tr>
<td colspan="6">No people found.</td>
</tr>
<% else %>
<% @people.each do |person| %>
<tr>
<td><%= person.first_name %></td>
<td><%= person.last_name %></td>
<td><%= person.email %></td>
<td><%= person.notes %></td>
<td>
<%= link_to "Edit", edit_person_path(person), class: "btn btn-default" %>
</td>
<td>
<%= button_to "Delete", person, method: :delete, data: { confirm: "Are you sure you wish to delete #{person.first_name} #{person.last_name}?" }, class: "btn btn-danger" %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>' >> app/views/people/index.html.erb
echo '
preload_app true
working_directory "/var/www/example-ruby.com"
pid "/tmp/unicorn.pid"
stderr_path "/var/www/example-ruby.com/log/unicorn.log"
stdout_path "/var/www/example-ruby.com/log/unicorn.log"
listen "/tmp/unicorn.example-ruby.sock", :backlog => 64
worker_processes 2
timeout 30' >> server.rb
sudo bash -c "mv $HOME/example-ruby.com /var/www"
sudo bash -c "chown $USER:$USER -R /var/www/example-ruby.com"
sudo bash -c "echo '##################################
upstream example-ruby {
server unix:/tmp/unicorn.example-ruby.sock fail_timeout=0;
}
server {
listen 80;
server_name example-ruby.com;
root /var/www/example-ruby.com;
try_files "'$uri'"/index.html "'$uri'" @example-ruby;
location @example-ruby {
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:3011;
}
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 Rails start
[Service]
Type=forking
WorkingDirectory=/var/www/example-ruby.com/
ExecStart=$HOME/.rvm/gems/ruby-$RUBYVERSION/wrappers/bundle exec \"unicorn -c server.rb -p 3011 -D\"
ExecReload=/bin/kill -s USR2 "'$MAINPID'"
ExecStop=/bin/kill -s QUIT "'$MAINPID'"
Restart=always
TimeoutSec=15
RestartSec=10
SyslogIdentifier=rails-example-ruby.com
User=$USER
PIDFile=/tmp/unicorn.pid
KillMode=mixed
KillSignal=SIGQUIT
[Install]
WantedBy=multi-user.target' >> /etc/systemd/system/example-ruby.service"
sudo bash -c "systemctl enable example-ruby.service && service example-ruby start"
sudo bash -c "service nginx restart"
sudo bash -c "echo '
0.0.0.0 example-ruby.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-ruby.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment