Isaac Sloan - Setting up Rails, Passenger, Nginx, Mysql and xdelta3 on RED HAT ENTERPRISE LINUX
Banner700

Setting up Rails, Passenger, Nginx, Mysql and xdelta3 on RED HAT ENTERPRISE LINUX

Step 1: Install Redhat RHEL

Step 2: Add a deploy user

adduser deploy

Step 3: Update and Install dependencies

Make sure that each line completes before running the next. Don't just copy the lines in all at once or it will use the beginning letters of one line as an answer to questions the first may ask.

sudo su - root
yum groupinstall "Development Tools" "Development Libraries"
yum-config-manager --enable rhel-6-server-optional-rpms
yum remove libevent libevent-devel libevent-headers
yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison iconv-devel git-core gcc g++ curl-devel httpd-devel apr-devel apr-util-devel sqlite-devel ncurses-devel
cd /usr/tmp
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
tar xf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure
make
make install
#also install the latest version of xdelta3 in the same way.
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5

Step 4: Install ruby 2.1.2

curl -sSL https://get.rvm.io | sudo bash -s stable
sudo vim /etc/group
# Add your current user and deploy to the rvm group like so: rvm:x:101:isaac,deploy
# Logout and log back in.
rvm install ruby-2.1.2
which ruby
rvm --default use 2.1.2

Step 5: Install Node.js

sudo yum -y install nodejs

Step 6: Install your database

sudo yum install -y mysql-server mysql mysql-devel
sudo chkconfig mysqld on
sudo /etc/init.d/mysqld start

Step 7: Install your Web Server (Nginx and Passenger)

Install Passenger
gem install passenger
rvmsudo passenger-install-nginx-module

The following lines will setup you nginx configs with sites-enabled and sites-available directories. This way you can keep you config a lot cleaner as you won't ever have to edit the main conf file.

sudo sed -i '1iuser deploy rvm;' /opt/nginx/conf/nginx.conf #use replace staff with rvm as group if installed.
sudo sed -i '36iinclude /opt/nginx/conf/sites-enabled/*;' /opt/nginx/conf/nginx.conf
sudo sed -i '36iserver_names_hash_bucket_size   64;' /opt/nginx/conf/nginx.conf
sudo mkdir /opt/nginx/conf/sites-enabled
sudo mkdir /opt/nginx/conf/sites-available

To setup the following commands for nginx:

sudo service nginx stop
sudo service nginx start
sudo service nginx restart

run:

sudo vim /etc/init.d/nginx

# copy the following into files and save.

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/opt/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
sudo chmod +x /etc/init.d/nginx
sudo /sbin/chkconfig nginx on
sudo /sbin/chkconfig --list nginx
sudo service nginx start

Step 8: Host your rails app

Clone your rails app to a directory owned by deploy. Then create a file in /opt/nginx/conf/sites-available/ called yourdomain.com.

sudo touch /opt/nginx/conf/sites-available/yourdomain.com

Edit /opt/nginx/conf/sites-available/yourdomain.com with your favorite editor and add:

server {
  listen 80;
  server_name yourdomain.com; 
  rewrite     ^ http://www.yourdomain.com$request_uri? permanent;
}

server {
  client_max_body_size 40M;
  listen 80;
  server_name www.yourdomain.com;
  root /path/to/your/railsapp/public;   # <--- be sure to point to 'public'!
  passenger_enabled on;
}

Then enabled your site by symlinking your conf file to sites-enabled and restarting nginx.

sudo ln -s /opt/nginx/conf/sites-available/yourdomain.com /opt/nginx/conf/sites-enabled/yourdomain.com
sudo service nginx restart
October 29, 2014
railslinuxredhatrhelnginx
comments powered by Disqus