jamesfwz
6/13/2014 - 6:30 AM

Server Setup.md

This guide setup Apache2/Passenger/Ruby/Rails on a CentOS 6.4 LiveCD.
You can test it yourself using VirtualBox to run all the scripts - read VirtualBox.md for more info.

# install the necessary libraries
yum clean all
yum -y update
yum -y install curl-devel git sqlite-devel libxml2-devel
yum -y install libxml2 libxml2-devel libxslt libxslt-devel
yum -y install gcc-c++ readline-devel libyaml-devel libffi-devel openssl-devel libtool bison
curl -sL https://rpm.nodesource.com/setup | bash -
yum -y install nodejs # If this does not work, do it again after you installed RVM

# install RVM
curl -L https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
rvm requirements --autolibs=enable

# Install Ruby
rvm install 2.0.0
rvm --default use 2.0.0

# Install bundler
gem install bundler

# Install Rails gem
gem install rails --version 4.0.0 --no-ri --no-rdoc

# You can verify rails successful installation
rails -v

# Install Apache
yum -y install httpd httpd-devel

# Allow port 80 to server webpage
iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT
# Or to turn off firewall completely: service iptables stop

# starts the service
chkconfig httpd on
service httpd start

# You can check if your apache is running by viewing http://localhost:8888 on your browser

# Install passenger
# There will be more requirements to install
# Just follow the instructions
gem install passenger --version 4.0.8
passenger-install-apache2-module

# There will be instructions to load your passenger module via http conf
# Put the config in a new file at /etc/httpd/conf.d/passenger.conf

# Restart the service
service httpd restart

# Install postgres - Please follow Postgres.md

# All done! Now to deployment!!

Diagnostic

# see httpd error log
cat /var/log/httpd/error_log

# Error with yum update
yum clean all
yum check-update

# find out your server IP
ifconfig eth0 | grep inet | awk '{ print $2 }'

# If there are error loading passenger
passenger-config --root
chcon -R -h -t httpd_sys_content_t /path-to-passenger-root

# Ensure your rvm gemset use default before you do passenger-install-apache2-module

Pre-deployment

  • CentOS 6
  • SSH Access (You should not be using root)
  • depot is the assumed name of the project in this guide. Please change it to your own project name.
  • Server is installed with Apache/Passenger/Bundler/RVM.
# Setup a user for the access (instead of root)
adduser deploy
passwd -l deploy
# This locks the password of deploy.
# Use public key to access SSH.
# Read section Access SSH via Public Key below

# add t rvm group so that bundling can work for capistrano
usermod -a -G rvm deploy

# set directory for the app
deploy_to=/var/www/depot/staging
mkdir -p ${deploy_to}
chown deploy:deploy ${deploy_to}
umask 0002
chmod g+s ${deploy_to}
mkdir -p ${deploy_to}/{releases,shared}

# Remember to setup your database user/password/DB
# See Postgres.md

# Prebundling
# You should install all the gems necessary (in global gemset) for your app using your Gemfile
# so that later deployment will not face bundling issues
# create a new rails app and copy your app Gemfile to be bundled over
# You should make sure bundle install works for your Gemfile
rvm gemset use global # for root purposes only
bundle install

# Or to install gems in your gemset
# Use your user (not root) to do the below:
rvm gemset create <gemset>
rvm gemset use <gemset>
bundle install

Apache Config

  • Create a file at sudo vi /etc/httpd/conf.d/localhost.conf
<VirtualHost *:80>
    ServerName localhost
    
    # Note that current is the folder created by Capistrano
    # It is created as a sym-link to /releases which contains the latest codes
    DocumentRoot /var/www/depot/staging/current/public
      
      # Set the rails environment
      RailsEnv development
      
    <Directory /var/www/depot/staging/current/public>
      # This relaxes Apache security settings.
      AllowOverride all
      # MultiViews must be turned off.
      Options -MultiViews
        Order Allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Setup Git on Server

  • This setups the git server on the remote server
  • Developer will push to this folder (~/git/depot.git) and Capistrano will pull from this folder and deploy it
mkdir -p ~/git/depot.git
cd ~/git/depot.git
git --bare init

Next, we need to setup the SSH for Capistrano to access the Git:

test -e ~/.ssh/id_dsa.pub || ssh-keygen -t dsa
cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

# Need to do this for CentOS
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys 
restorecon -Rv ~/.ssh

# Accepting host authenticity for user
# Test it (you need to accept the host authenticity):
ssh localhost
# if you did not see any prompt for password, it is done!

# Test: accept host authenticity for git
git ls-remote ssh://user@domain/~/git/depot.git master

Access SSH via Public Key

  • This will skip the step of entering password everytime you push into the server
ssh-keygen -t rsa -C "your_email@example.com"
# go with the default file storage (~/.ssh/id_rsa.pub) and enter a good passphrase
  • Send your id_rsa.pub to the server
# Make sure .ssh directory exists in the server
scp ~/.ssh/id_rsa.pub root@localhost:~/.ssh/id_rsa.pub

# or send with port 2222 if you are using VirtualBox
scp -P 2222 ~/.ssh/id_rsa.pub root@localhost:~/.ssh/id_rsa.pub
  • Next, SSH into the server and run the following command:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

# Need to do this for CentOS
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys 

restorecon -Rv ~/.ssh
  • That's it!