Ubuntu Server Setup Script

steveyken edited this page Feb 21, 2013 · 2 revisions

Script to setup Ubuntu Linux Server


This procedure is provided without any express or implied warranty and using it is entirely at your own risk. Installing any software on a computer can have unintended consequences. You should back up your important files before trying this or any other installation.

You can run the following command to quickly install Fat Free CRM on a Debian-based server. It will take about 20 minutes to complete.

sudo apt-get install -y --force-yes curl; sudo su - -c "bash < <(curl -sL http://is.gd/q6sEHA)"

It has been tested on a clean Ubuntu 10.10. (It does not work properly on later versions at the moment.) If you are hosting Fat Free CRM on a public site then you will need to reconfigure the database for security reasons.

It runs the following steps:

  • Install required packages (git, postgresql, etc.)
  • Configure PostgreSQL
  • Install RVM & Ruby 1.9.2
  • Install Passenger
  • Install Fat Free CRM
  • Configure Fat Free CRM database settings
  • Install Fat Free CRM production gems to vendor/gems
  • Create & migrate Fat Free CRM database & load settings
  • Create default admin user
  • Configure machine hostname
  • Configure passenger standalone service (runs on startup)
  • Set permissions
  • Start passenger service, which:
    • Downloads and compiles Nginx
    • Installs Phusion Passenger Standalone
    • Starts the web server on port :80

Here is the script that is executed:

# Deploy Fat Free CRM (rails3 branch) to a freshly installed Ubuntu machine.
# --------------------------------------------------------------------------
# After this process has finished, you should be able to load http://localhost in your browser.
# 'http://fat_free_crm' and http://xx.xx.xx.xx (IP address) should be accessible from other machines.
# (type 'ifconfig' in terminal to find your IP address)

# Variables
# ----------------------------------------------------------

# *) Install required packages
# ----------------------------------------------------------
apt-get update
apt-get install -y --force-yes curl git-core postgresql libpq-dev build-essential libcurl4-openssl-dev zlib1g-dev

# *) Configure PostgreSQL
# ----------------------------------------------------------
sed -e "s|local *all *postgres .*|local    all         postgres                   trust|g" \
    -e "s|local *all *all .*|local    all         all                   trust|g" \
    -e "s|host *all *all * .*|host    all         all           trust|g" \
    -e "s|host *all *all *::1/128 .*|host    all         all        ::1/128           trust|g" \
    -i /etc/postgresql/8.*/main/pg_hba.conf
/etc/init.d/postgresql restart

# *) Install RVM & Ruby
# ----------------------------------------------------------
if ! (which rvm) then
  bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
# Loads RVM into the current shell session
. "/usr/local/rvm/scripts/rvm"
# Install the configured ruby version
if ! (rvm list | grep -q $ruby_version); then
    rvm install $ruby_version
# Use the installed ruby environment
rvm use "$ruby_version"
# Configure .gemrc to ignore ri and rdoc
echo "gem: --no-ri --no-rdoc" > $HOME/.gemrc
# Install bundler and passenger
if ! (gem list | grep -q "bundler"); then gem install bundler; fi
if ! (gem list | grep -q "passenger"); then gem install passenger; fi

# *) Install Fat Free CRM
# ----------------------------------------------------------
mkdir -p /opt/rails
git clone $app_repo $app_path
cd $app_path
git checkout -t origin/rails3

# *) Configure Fat Free CRM database settings
# ----------------------------------------------------------
cat << EOF > $app_path/config/database.yml
  adapter: postgresql
  database: fat_free_crm_production
  username: postgres
  host:     localhost
  port:     5432
  schema_search_path: public

# *) Install Fat Free CRM production gems
# ----------------------------------------------------------
# Make passenger look in the right place for gems
mkdir -p $app_path/.bundle
cat << EOF > $app_path/.bundle/config
BUNDLE_WITHOUT: development:test
BUNDLE_PATH: /usr/local/rvm/gems/$ruby_version/bundler
cd $app_path
bundle install --without development test

# *) Create & migrate Fat Free CRM database
# ----------------------------------------------------------
RAILS_ENV=production rake db:create db:migrate

# *) Create default admin user
# ----------------------------------------------------------
RAILS_ENV=production rake ffcrm:setup:admin USERNAME=admin PASSWORD=admin EMAIL=admin@example.com

# *) Configure machine hostname
# ----------------------------------------------------------
echo "fat_free_crm" > /etc/hostname
restart hostname

# *) Configure passenger standalone init script
#    (Uses https://github.com/ndbroadbent/passenger-initscript)
# --------------------------------------------------------------
groupadd passenger
useradd passenger -g passenger
cd /etc/init.d
curl https://github.com/ndbroadbent/passenger-initscript/raw/master/passenger -o passenger
chmod +x passenger
# Setup 'passenger-initscripts'
/etc/init.d/passenger setup
# Configure Fat Free CRM passenger config
cat << EOF > /etc/passenger.d/$app_name.yml
rvm: $ruby_version
cwd: $app_path
user: passenger
port: 80
environment: production
max-pool-size: 4
min-instances: 1
pid-file: $app_path/tmp/pids/passenger.pid
log-file: $app_path/log/passenger.log
# Make directory for passenger pids
mkdir -p $app_path/tmp/pids
mkdir -p $app_path/log

# *) Set permissions
# ----------------------------------------------------------
chown -R passenger:passenger $app_path

# *) Start passenger service
# ----------------------------------------------------------
/etc/init.d/passenger start