Skip to content

Instantly share code, notes, and snippets.

@Hendrikv1990
Forked from marinoszak/server_configuration.sh
Created November 17, 2015 16:30
Show Gist options
  • Save Hendrikv1990/9b4057eef80701ca1e04 to your computer and use it in GitHub Desktop.
Save Hendrikv1990/9b4057eef80701ca1e04 to your computer and use it in GitHub Desktop.
ubuntu 14.04, passenger, apache, rails
#!/bin/bash
# ================================================================== #
# Ubuntu 14.04 web server build shell script
# ================================================================== #
#
# ================================================================== #
#
#
#
# ================================================================== #
# Define system specific details in this section #
# ================================================================== #
HOSTNAME=localhost
SERVER_IP=127.0.0.1
DOMAIN=localhost
LANGUAGE=
CHARSET=
SSHPORT=
IGNOREIP=
USER=marinos
PASSWORD=password
ADMINEMAIL=
PUBLICKEY=
# Application credentials.
DATABASE_USER=marinos
DATABASE_PASSOWRD=password
# Repository url to clone automatically.
REPOSITORY_URL=
# Should be the same as repository app name.
APP_NAME=
# ================================================================== #
# End system specific details #
# ================================================================== #
#
echo
echo "System updates and basic setup"
echo "==============================================================="
echo
echo
echo "Entering is sudo user mode"
echo "---------------------------------------------------------------"
#
sudo su
#
echo
echo
echo
echo "First things first, let's make sure we have the latest updates."
echo "---------------------------------------------------------------"
#
apt-get update -y
#
echo
echo
echo
echo "Setting the proper timezone."
echo "---------------------------------------------------------------"
#
dpkg-reconfigure tzdata
#
echo
echo
echo
echo "Synchronize the system clock with an NTP server"
echo "---------------------------------------------------------------"
#
apt-get install -y ntp
#
echo
echo
echo
echo "Setting the language and charset"
echo "---------------------------------------------------------------"
# TODO
echo
echo
echo
echo "Disabling root ssh login"
echo "---------------------------------------------------------------"
#
sed -i "s/PermitRootLogin yes/PermitRootLogin no/g" /etc/ssh/sshd_config
#
echo
echo
echo
echo "Disabling password authentication"
echo "---------------------------------------------------------------"
#
sed -i "s/#PasswordAuthentication yes/PasswordAuthentication no/g" /etc/ssh/sshd_config
#
echo
echo
echo
echo "Disabling X11 forwarding"
echo "---------------------------------------------------------------"
#
sed -i "s/X11Forwarding yes/X11Forwarding no/g" /etc/ssh/sshd_config
#
echo
echo
echo
echo "Disabling sshd DNS resolution"
echo "---------------------------------------------------------------"
#
echo "UseDNS no" >> /etc/ssh/sshd_config
#
echo
echo
echo
echo "Creating new primary user"
echo "---------------------------------------------------------------"
# -------------------------------------------------------------------------
# Script to add a user to Linux system
# -------------------------------------------------------------------------
# Copyright (c) 2007 nixCraft project <http://bash.cyberciti.biz/>
# This script is licensed under GNU GPL version 2.0 or above
# Comment/suggestion: <vivek at nixCraft DOT com>
# -------------------------------------------------------------------------
# See url for more info:
# http://www.cyberciti.biz/tips/howto-write-shell-script-to-add-user.html
# -------------------------------------------------------------------------
if [ $(id -u) -eq 0 ]; then
egrep "^$USER" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$USER exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $PASSWORD)
useradd -s /bin/bash -m -d /home/$USER -U -p $pass $USER
[ $? -eq 0 ] && echo "$USER has been added to system!" || echo "Failed to add a $USER!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi
# -------------------------------------------------------------------------
# End script to add a user to Linux system
# -------------------------------------------------------------------------
#
echo
echo
echo
echo "Adding $USER to SSH AllowUsers"
echo "---------------------------------------------------------------"
#
echo "AllowUsers $USER" >> /etc/ssh/sshd_config
#
echo
echo
echo
echo "Adding $USER to sudoers"
echo "---------------------------------------------------------------"
#
cp /etc/sudoers /etc/sudoers.tmp
chmod 0640 /etc/sudoers.tmp
echo "$USER ALL=(ALL) ALL" >> /etc/sudoers.tmp
chmod 0440 /etc/sudoers.tmp
mv /etc/sudoers.tmp /etc/sudoers
#
echo
echo
echo
echo "Adding ssh key"
echo "---------------------------------------------------------------"
#
ssh-keygen
cat ~/.ssh/id_rsa.pub
read -p "Copy the the id_rsa.key and PUT it on bitbucket before you continue" ack
/etc/init.d/ssh restart
#
echo
echo
echo
echo "Entering in $USER user mode"
echo "---------------------------------------------------------------"
#
su $USER
#
# ================================================================== #
# Application specific configuration #
# ================================================================== #
#
echo
echo
echo
echo "Application specific configuration"
echo "==============================================================="
echo
echo
echo
echo "Install the rbenv and Ruby dependencies with apt-get"
echo "---------------------------------------------------------------"
#
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev -y
#
echo
echo
echo
echo "Install .rbenv"
echo "---------------------------------------------------------------"
#
cd
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
#
echo
echo
echo
echo "Install ruby"
echo "---------------------------------------------------------------"
#
rbenv install -v 2.2.1
rbenv global 2.2.1
echo "gem: --no-document" > ~/.gemrc
#
echo
echo
echo
echo "install bundler"
echo "---------------------------------------------------------------"
#
gem install bundler
#
echo
echo
echo
echo "install rails"
echo "---------------------------------------------------------------"
#
gem install rails
#
echo
echo
echo
echo "A few Rails features, such as the Asset Pipeline, depend on a Javascript runtime. We will install Node.js to provide this functionality."
echo "---------------------------------------------------------------"
#
sudo add-apt-repository ppa:chris-lea/node.js -y
sudo apt-get update -y
sudo apt-get install nodejs -y
#
echo
echo
echo
echo "install postgresql"
echo "---------------------------------------------------------------"
#
sudo apt-get update -y
sudo apt-get install postgresql postgresql-contrib libpq-dev -y
#
echo
echo
echo
echo "Set the database user's password"
echo "---------------------------------------------------------------"
#
sudo -u postgres createuser -s DATABASE_USER
sudo -u postgres psql -c "ALTER USER $DATABASE_USER WITH PASSWORD $DATABASE_PASSOWRD;"
#
echo
echo
echo
echo "Install Passenger and Nginx."
echo "---------------------------------------------------------------"
#
sudo apt-get install nginx-extras passenger
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
echo "deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main" >> /etc/apt/sources.list.d/passenger.list
sudo chown root: /etc/apt/sources.list.d/passenger.list -y
sudo chmod 600 /etc/apt/sources.list.d/passenger.list -y
sudo apt-get update -y
sudo apt-get install libapache2-mod-passenger -y
sudo a2enmod passenger
sudo service apache2 restart
#
echo
echo
echo
echo "Configure apache and passenger"
echo "---------------------------------------------------------------"
#
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/APP_NAME.conf -y
echo "<VirtualHost *:80> /n
ServerName $SERVER_IP /n
ServerAlias $SERVER_IP /n
ServerAdmin webmaster@localhost /n
DocumentRoot /home/rails/$APP_NAME/public /n
RailsEnv development /n
ErrorLog ${APACHE_LOG_DIR}/error.log /n
CustomLog ${APACHE_LOG_DIR}/access.log combined /n
<Directory '/home/rails/'$APP_NAME'/public'> /n
Options FollowSymLinks /n
Require all granted /n
</Directory> /n
</VirtualHost>" > "/etc/apache2/sites-available/$APP_NAME.conf"
#
echo
echo
echo
echo "Set the production secret key and database password using environment variables."
echo "---------------------------------------------------------------"
#
# http://stackoverflow.com/questions/12058705/a-better-way-to-do-git-clone
function lazyclone {
url=$1;
reponame=$(echo $url | awk -F/ '{print $NF}' | sed -e 's/.git$//');
git clone $url $reponame;
cd $reponame;
}
cd ~/.rbenv/plugins
git clone https://github.com/sstephenson/rbenv-vars.git
cd ~/
git lazyclone REPOSITORY_URL
secret=$(rake secret)
echo "SECRET_KEY_BASE=$secret /n APP_DATABASE_USER=$DATABASE_USER /n APP_DATABASE_PASSWORD=$DATABASE_PASSOWRD" >> .rbenv-vars
#
echo
echo
echo
echo "Rails warming up."
echo "---------------------------------------------------------------"
#
RAILS_ENV=production rake db:create
RAILS_ENV=production rake db:migrate
RAILS_ENV=production rake assets:precompile
#
echo
echo
echo
echo "Start rails."
echo "---------------------------------------------------------------"
#
sudo a2dissite 000-default
sudo a2ensite APP_NAME
sudo service apache2 restart
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment