Skip to content

Instantly share code, notes, and snippets.

@deepakkumarnd
Last active April 3, 2020 15:14
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 24 You must be signed in to fork a gist
  • Save deepakkumarnd/8d2c597d10dc9a0bddb0 to your computer and use it in GitHub Desktop.
Save deepakkumarnd/8d2c597d10dc9a0bddb0 to your computer and use it in GitHub Desktop.
Server setup script
# This script has to be run as a root user
echo "* Updating system"
apt-get update
apt-get -y upgrade
echo "* Installing packages"
apt-get -y install build-essential libmagickcore-dev imagemagick libmagickwand-dev libxml2-dev libxslt1-dev git-core nginx redis-server curl nodejs htop
id -u deploy &> /dev/null
if [ $? -ne 0 ]
then
echo "* Creating user deploy"
useradd -m -g staff -s /bin/bash deploy
echo "* Adding user deploy to sudoers"
chmod +w /etc/sudoers
echo "deploy ALL=(ALL) ALL" >> /etc/sudoers
chmod -w /etc/sudoers
else
echo "* deploy user already exists"
fi
echo "* Installing rvm"
. /etc/profile.d/rvm.sh &> /dev/null
type rvm &> /dev/null
if [ $? -ne 0 ]
then
curl -L https://get.rvm.io | bash -s
echo "source /etc/profile.d/rvm.sh" >> /etc/bash.bashrc
. /etc/profile.d/rvm.sh &> /dev/null
else
echo "* rvm already installed"
fi
cat /etc/environment | grep RAILS_ENV
if [ $? -ne 0 ]
then
echo "RAILS_ENV=production" >> /etc/environment
fi
echo "* Adding a gemrc file to deploy user"
echo -e "verbose: true\nbulk_threshold: 1000\ninstall: --no-ri --no-rdoc --env-shebang\nupdate: --no-ri --no-rdoc --env-shebang" > /home/deploy/.gemrc
chmod 644 /home/deploy/.gemrc
chown deploy /home/deploy/.gemrc
chgrp staff /home/deploy/.gemrc
echo "* Adding ssh key to authorized_keys"
test -d /home/deploy/.ssh
if [ $? -ne 0 ]
then
mkdir /home/deploy/.ssh
chmod 700 /home/deploy/.ssh
chown deploy /home/deploy/.ssh
chgrp staff /home/deploy/.ssh
fi
# deepak's public key
echo "deepak's public key" > /home/deploy/.ssh/authorized_keys
chmod 600 /home/deploy/.ssh/authorized_keys
chown deploy /home/deploy/.ssh/authorized_keys
chgrp staff /home/deploy/.ssh/authorized_keys
echo "* Install ruby version 2.0.0"
ruby -v &> /dev/null
if [ $? -ne 0 ]
then
rvm install 2.1.0
else
echo "* Ruby already installed"
fi
echo "* Add user deploy to rvm group"
usermod -a -G rvm deploy
rvm --default use 2.1.0
ruby -v
echo "* DONE *"
echo "* Rebooting system *"
reboot
@vijayn
Copy link

vijayn commented Jan 5, 2015

Good one :)

@nicolas-besnard
Copy link

Seems like there a probleme here(L7 to L9) a got a "* deploy user already exists" message. Fix it by removing &> /dev/null

@deepakkumarnd
Copy link
Author

@vijayn Thanks

@deepakkumarnd
Copy link
Author

@nicolas-besnard If deploy user does not exists it "id -u deploy &> /dev/null" returns 1, it returns 0 if the user exists, have you ran the script two times or do u have the user already ?

@petrosp
Copy link

petrosp commented Feb 21, 2015

@42races Excellent! Excuse my ignorance, does this script generate a deploy user who is not practically capable of running anything with sudo? I tried sudo apt-get update and I get a prompt for a password which was never set. Is that by design or am I missing s'thing obvious?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment