Last active
February 19, 2017 05:35
-
-
Save lpimem/382d564065d6070b3de402c2d53114e9 to your computer and use it in GitHub Desktop.
setup a Ubuntu server for hosting ruby on rails application using nginx and passenger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# ------------------------------------------- | |
# The purpose of this script is to help you | |
# setup a production server for RubyOnRails | |
# applications quickly. | |
# | |
# It will | |
# + install nginx, passenger, git, nodejs | |
# and RVM | |
# + create user for the ruby app | |
# + fetch app code from configured source | |
# | |
# Script tested on Ubuntu 16.04 LTS | |
# | |
# Usage | |
# 1. Update configurations | |
# 2. sudo ./nginx_passenger_for_ubuntu.sh | |
# 3. Follow instructions on NEXT part | |
# ------------------------------------------- | |
# configurations | |
APP_NAME=MyApp | |
WWW_PATH=/var/www | |
GITHUB_URI=https://github.com/user/MyApp.git | |
APP_USER=red | |
RUBY_VERSION=2.2.1 | |
SERVER_NAME=yourdomain.com | |
SERVER_SCHEME=http:// | |
# check user privilege | |
if [[ "$EUID" -ne 0 ]]; then | |
echo "Error: please run with sudo." | |
exit | |
fi | |
apt update && apt upgrade -y | |
# Install passenger's PGP key and add HTTPS support for APT | |
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7 | |
sudo apt-get install -y apt-transport-https ca-certificates | |
# Add passenger's APT repository | |
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger xenial main > /etc/apt/sources.list.d/passenger.list' | |
sudo apt-get update | |
# Install Passenger + Nginx | |
sudo apt-get install -y nginx-extras passenger | |
sed -i 's_# include /etc/nginx/passenger.conf;_include /etc/nginx/passenger.conf;_' /etc/nginx/nginx.conf | |
service nginx restart | |
# check passenger config | |
# /usr/bin/passenger-config validate-install | |
# check if passenger is loaded by nginx | |
/usr/sbin/passenger-memory-stats | |
# install RVM | |
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 | |
\curl -sSL https://get.rvm.io | bash -s stable | |
echo "# update path | |
export PATH=$PATH:/usr/local/rvm/bin" >> ~/.bashrc | |
source ~/.bashrc | |
# add app user | |
adduser --disabled-password --gecos "" $APP_USER | |
# Install git and download code | |
apt install -y git nodejs | |
cd $WWW_PATH | |
mkdir $APP_NAME | |
cd $APP_NAME | |
git clone $GITHUB_URI code | |
cd .. | |
chown -R $APP_USER: $APP_NAME | |
echo ' | |
# NEXT: | |
# # As root: | |
# rvm install "$RUBY_VERSION" | |
# rvm use "$RUBY_VERSION" | |
# gem install bundler | |
# # As $APP_USER: | |
# cd $WWW_PATH/$APP_NAME/code | |
# bundle install # --deployment --without development test # may fail if no Gemfile.lock | |
# bundle exec rake secret > /tmp/_rb_dummy_txt | |
# SEC=$(</tmp/_rb_dummy_txt) | |
# rm /tmp/_rb_dummy_txt | |
# sed -i 's/<%=ENV["SECRET_KEY_BASE"]%>/$SEC' config/secrets.yml | |
# chmod 700 config db | |
# chmod 600 config/database.yml config/secrets.yml | |
# bundle exec rake assets:precompile db:migrate RAILS_ENV=production | |
# RAILS_ENV=production bundle exec rake db:reset | |
# RUBY_RUN_PATH=`passenger-config about ruby-command 2>/dev/null|awk '/Command:/ {print $2; exit}'|sed 's/Command://'` | |
# # As root: | |
# echo "server { | |
# listen 80; | |
# server_name $SERVER_NAME; | |
# root /var/www/$APP_NAME/code/public; | |
# passenger_enabled on; | |
# passenger_ruby $RUBY_RUN_PATH; | |
# }" > /etc/nginx/sites-enabled/$APP_NAME.conf | |
# service nginx restart | |
# # Test | |
# curl $SERVER_SCHEME$SERVER_NAME | |
# For more, reference | |
# https://www.phusionpassenger.com/library/walkthroughs/deploy/ruby/ownserver/nginx/oss/xenial/deploy_app.html | |
' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment