Ubuntu Lucid: Base Setup + Nginx + Optional MongoDb/MySQL

by bkirby81
32 deployments · 9 still active · last rev. 5 years ago

Base system setup including SSH config, hostname, optional nginx/mysql/mongo and optional RVM system wide install.

Compatible with: No distros currently supported
						#!/bin/bash
# 
# Ruby 1.9.2 with options for database, and nginx install. Adds option to install RVM system-wide.
# Outputs to /root/stackscript.log
#
# <UDF name="deploy_user" Label="Name of deployment user." />
# <UDF name="deploy_password" Label="Password for deployment user." />
# <UDF name="deploy_sshkey" Label="Deployment user public ssh key." />
# <UDF name="ssh_port" Label="SSH Port" />
# <UDF name="new_hostname" Label="Server's hostname." />
# <UDF name="default_editor" Label="Default text editor." Default="nano" />
# <UDF name="install_firewall" Label="Install firewall? (UFW and iptables)" Default="Yes" oneOf="Yes,No" />
# <UDF name="open_web_firewall" Label="Open firewall ports for web?" Default="Yes" oneOF="Yes,No" />
# <UDF name="nginx_options" Label="Install Nginx?" Default="NO" oneOf="No,Stable,With Upload Module" />
# <UDF name="databases_to_install" Label="Databases to install" Default="None" manyOf="None,MySQL,MongoDB" />
# <UDF name="install_rvm" Label="Install RVM? (system wide)" Default="No" oneOf="Yes,No" />


exec &> /root/stackscript.log

source <ssinclude StackScriptID=1>    # Common bash functions
source <ssinclude StackScriptID=123>  # Awesome ubuntu utils script

function log {
  echo "$1 `date '+%D %T'`"
}

function mongodb_install {
  echo "deb http://downloads.mongodb.org/distros/ubuntu 10.4 10gen" >> /etc/apt/sources.list
  apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
  aptitude update
  aptitude install -y mongodb-stable
}

function system_sshd_portnumber {
  sed -i "s/^#*\(Port\).*/\1 $1/" /etc/ssh/sshd_config
}

function system_sshd_append {
  # $1 configuration option
  # $2 value to set
  echo "$1 $2" >> /etc/ssh/sshd_config
}


function install_nginx {
  if [ "$NGINX_OPTIONS" == "Stable" ]; then
    add-apt-repository ppa:nginx/stable	
  else
    add-apt-repository ppa:brianmercer/nginx
  fi
  aptitude update
  aptitude -y install nginx
}


log "Adding universe repositories and updating."
system_enable_universe
log "Base packages updated"
log "Installing required packages for ruby/rails, as well as git..."
aptitude -y install git-core build-essential zlib1g-dev libssl-dev libreadline5-dev gcc g++ libxslt-dev libxml2-dev curl wget python-software-properties openssh-server $DEFAULT_EDITOR
log "Packages installed"

# Add deploy user
log "Adding deploy user, and ssh key."
system_add_user $DEPLOY_USER $DEPLOY_PASSWORD "users,sudo"
system_user_add_ssh_key $DEPLOY_USER "$DEPLOY_SSHKEY"
system_update_locale_en_US_UTF_8

log "Reconfiguring SSH"
system_sshd_portnumber $SSH_PORT
system_sshd_permitrootlogin No
system_sshd_passwordauthentication No
system_sshd_pubkeyauthentication Yes
system_sshd_append ClientAliveInterval 60
/etc/init.d/ssh restart

log "Set hostname to $NEW_HOSTNAME"
system_update_hostname $NEW_HOSTNAME

if [ "$INSTALL_FIREWALL" == "Yes" ]; then
  aptitude -y install ufw
  ufw logging on
  ufw default deny
  if [ "$OPEN_WEB_FIREWALL" == "Yes" ]; then
    ufw allow 80/tcp
    ufw allow 443/tcp
  fi	
  ufw allow $SSH_PORT
  ufw enable	
fi


if [ "$NGINX_OPTIONS" != "No" ]; then
  install_nginx
fi
 
if [[ -n "$(echo $DATABASES_TO_INSTALL | grep 'MySQL')" ]]; then
  log "Installing MySQL"
  aptitude -y install libmysqlclient-dev
  mysql_install "$DEPLOY_PASSWORD"
  mysql_tune 30
fi

if [[ -n "$(echo $DATABASES_TO_INSTALL | grep 'MongoDB')" ]]; then
  log "Installing MongoDB"
  mongodb_install
fi

if [ "$INSTALL_RVM" == "Yes" ]; then
  log "Installing RVM. Make sure to update .bashrc on first login."
  bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
	log "Installing openssl and readline"
	rvm package install openssl
	rvm package install readline
end