# Deploying a RhoConnect Application ## Deploying On-Premise The [sinatra book](http://sinatra-book.gittr.com/) has some great [deployment documentation](http://sinatra-book.gittr.com/#deployment) that you should review before running your RhoConnect application in production. We recommend using the [phusion passenger](http://modrails.com/index.html) stack for deploying an on-premise RhoConnect application. Please refer to the [section on rack applications](http://modrails.com/documentation/Users%20guide%20Apache.html#_deploying_a_rack_based_ruby_application) for information on deploying a RhoConnect sinatra application. ## Deploying packaged RhoConnect software on Linux servers You can create RhoConnect production environment on Linux servers by installing software packages for Ubuntu and CentOS respectively. At this moment supported formats are Debian (deb) and Red Hat (rpm) packages. Every package provides the following components: * Ruby 1.9.3-p194 * Nginx HTTP server, release 1.2.x * Thin application server, release 1.3.1 * Redis data store, release 2.4.x * Latest RhoConnect gem with all required dependencies In addition, RPM package provides latest sqlite3 headers and binaries, because standard Cent OS (5.x) libraries for sqlite3 outdated. ### Getting the Packages To download the RhoConnect debian package, you first must add our repo to your list of sources. #### Steps for Debian-Based Linux Users Add the following line to the end of your /etc/apt/sources.list: :::text deb http://rhoconnect.s3.amazonaws.com/packages/deb rhoconnect main Once the repo is added apt-get needs to be updated: :::term $ sudo apt-get update Once that is done, it is time to install RhoConnect: :::term $ sudo apt-get install rhoconnect #### Steps for RedHat-Based Linux Users Vanilla CentOS distribution does not include some packages required by RhoConnect installer. To resolve dependencies you might wanted to use either [RPMForge](http://wiki.centos.org/AdditionalResources/Repositories/RPMForge) or [Extra Packages for Enterprise Linux (EPEL) ](http://fedoraproject.org/wiki/EPEL) repository. For example, to add rpmforge repository for CentOS 5.8 release and x86_64 architecture execute this command: :::term $ wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm; rpm -Uhv rpmforge* For CentOS 6.2 release: :::term $ wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm; rpm -Uhv rpmforge* If you'd rather prefer EPEL repo, then run one of :::term $ wget http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm $ wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm Now you can create a file named rhoconnect.repo in the /etc/yum.repos.d/ directory: :::term $ sudo vim /etc/yum.repos.d/rhoconnect.repo Copy and paste these contents into the file. :::text [rhoconnect] name=Rhoconnect baseurl=http://rhoconnect.s3.amazonaws.com/packages/rpm enabled=1 gpgcheck=0 Once that is done, it is time to install RhoConnect: :::term $ sudo yum install rhoconnect ### Final Steps (After the installer is finished) Installer also created and configured RhoConnect rhoapp application in /opt/nginx/html directory. To test it you need to as a root user start redis, nginx, and thin servers: :::term $ sudo /etc/init.d/redis start $ sudo /etc/init.d/nginx start $ sudo /etc/init.d/thin start And verify that it's up and running by visiting application web console in your browser: http://servername ### Configuration of web and application servers Installer compiled and configured Nginx as reverse proxy web server (/opt/nginx) with the following settings: * Nginx start-up script (`/etc/init.d/nginx`) * Nginx logrotate settings (`/etc/logrotate.d/nginx`) * Nginx configuration file (`/opt/nginx/conf/nginx.conf`) * virtual host template for rhoconnect application (`/opt/nginx/conf/conf.d/rhoconnect.conf`) Installer also configured Thin app server with the following configuration files: * Thin start-up script (`/etc/init.d/thin`) * Thin configuration file (`/etc/thin/rhoapp.yml`) Default setup of Nginx server is to deal with back-end servers (called "upstreams") that are running on UNIX domain sockets: :::text # /opt/nginx/conf/conf.d/rhoconnect.conf file upstream thin_cluster { least_conn; server unix:/tmp/thin.0.sock; server unix:/tmp/thin.1.sock; # Add additional copies if need more Thin servers #server unix:/tmp/thin.2.sock; #server unix:/tmp/thin.3.sock; } server { listen 80; # Be sure to point to 'public' folder of your application! root /opt/nginx/html/rhoapp/public; # ... } Thin application server runs as a cluster with /etc/thin/rhoapp.yml configuration file: :::yaml --- chdir: /opt/nginx/html/rhoapp environment: production timeout: 30 log: /var/log/thin/thin.log pid: /var/run/thin/thin.pid max_conns: 1024 max_persistent_conns: 512 require: [] wait: 30 socket: /tmp/thin.sock servers: 2 daemonize: true Configuration file is generated by this command: :::term $ thin config -C /etc/thin/rhoapp.yml -c /opt/nginx/html/rhoapp/ --socket /tmp/thin.sock --servers 2 --log /var/log/thin/thin.log --pid /var/run/thin/thin.pid -e production ### Development and deployment of RhoConnect applications Packaged software does not setup automatically path to installed ruby bins and gems, so you need do it manually. Add necessary bins to the path(s) of the users who will be using this software. You may also wish to add these items to your bash scripts (i.e. `~/.profile` on Ubuntu; `~/.bash_profile` on CentOS) to automatically add them upon login. :::text export PATH=/opt/rhoconnect/bin:$PATH To deploy and develop your rhoconnect app on nginx and thin servers a) Copy your rhoconnect project (lets name it as `your_rhoconnect_app`) to default location to `/opt/nginx/html` directory b) Set up for it `nginx` owner :::term $ cd /opt/nginx/html $ sudo chown -R nginx:nginx your_rhoconnect_app/ c) Make sure that your app is bundled properly :::term $ cd your_rhoconnect_app $ sudo /opt/rhoconnect/bin/bundle install d) Configure Nginx virtual host for your rhoconnect application. For that edit the file `/opt/nginx/conf/conf.d/rhoconnect.conf`, so that it reflects your specifications (root directive) :::text # ... server { listen 80; # Be sure your app have 'public' folder and root directive # point to it! root /opt/nginx/html/your_rhoconnect_app/public; # ... } e) Edit Thin `/etc/thin/rhoapp.yml` configuration file directly :::yaml --- chdir: /opt/nginx/html/your_rhoconnect_app # ... or as root user generate a new one :::term $ env PATH=/opt/rhoconnect/bin:$PATH thin config -C /etc/thin/your_rhoconnect_app.yml \ -c /opt/nginx/html/your_rhoconnect_app/ \ --socket /tmp/thin.sock --servers 2 --log /var/log/thin/thin.log \ --user nginx --group nginx \ --pid /var/run/thin/thin.pid -e production f) As root user restart Nginx, and Thin servers :::term /etc/init.d/nginx restart /etc/init.d/thin restart For monitoring and troubleshooting purposes visit web console of your app and look at log files in `/opt/nginx/logs`. Also you can use RhoConnect `/opt/nginx/html/rhoapp` application as a template and modify it as you wanted. **NOTE: You should pay attentions to situations, if you have already Ruby installed on system level. Avoid usage of system gems in your application. It might lead to unpredictable results, if ruby versions are different. Either set up path to rhoconnect binaries for root user, or install required gems as `sudo /opt/rhoconnect/bin/gem install gem_name`** ## Deploying RhoConnect Redis and Push packages on Linux servers ### RhoConnect Redis RhoConnect Redis is a packaged Redis server for RhoConnect development and production environment on Linux servers. rhoconnect-redis package might be useful if you want to deploy Redis on separate server. The package compiles from sources latest stable Redis release (2.4.15) and installs it to `/opt/rhoconnect` directory with the following settings: * Redis server configuration file (`/opt/rhoconnect/etc/redis.conf`) * log files located in `/var/log/redis` directory * start-up scripts in `/etc/init` and `/etc/init.d` directories #### Steps for Debian-Based Linux Users Add the following line to the end of your /etc/apt/sources.list. :::term deb http://rhoconnect-repo.s3.amazonaws.com/packages/deb rhoconnect-repo main Then update the repo list and install RhoConnect Redis. :::term $ sudo apt-get update $ sudo apt-get install rhoconnect-redis When Redis server is successfully installed, start it using the following command. :::term $ sudo start rhoconnect-redis #### Steps for RedHat-Based Linux Users Create a file named rhoconnect-repo.repo in the /etc/yum.repos.d/ directory. :::term $ sudo nano /etc/yum.repos.d/rhoconnect-repo.repo Copy and paste these contents into the file. :::term [rhoconnect-redis] name=Rhoconnect Redis baseurl=http://rhoconnect-repo.s3.amazonaws.com/packages/rpm enabled=1 gpgcheck=0 Once that is done, install the RhoConnect Push service. :::term $ sudo yum install rhoconnect-redis You can start a redis server using the following command. :::term $ sudo /etc/init.d/redis start ### RhoConnect Push Service You can create a RhoConnect Push production environment on Linux servers by installing prepackaged software for Ubuntu (12.x) and CentOS (6.x). In a few clicks, you will have installed on your Linux server. * Node.js with Npm package manager * RhoConnect Push service * Upstart script to start, stop, and control Push service #### Steps for Debian-Based Linux Users Add the following line to the end of your /etc/apt/sources.list. :::term deb http://rhoconnect-repo.s3.amazonaws.com/packages/deb rhoconnect-repo main Then update the repo list and install RhoConnect Push. :::term $ sudo apt-get update $ sudo apt-get install rhoconnect-push #### Steps for RedHat-Based Linux Users Create a file named rhoconnect-repo.repo in the /etc/yum.repos.d/ directory. :::term $ sudo nano /etc/yum.repos.d/rhoconnect-repo.repo Copy and paste the following contents into the file. :::term [rhoconnect-push] name=Rhoconnect Push Service baseurl=http://rhoconnect-repo.s3.amazonaws.com/packages/rpm enabled=1 gpgcheck=0 Once that is done, install the RhoConnect Push service. :::term $ sudo yum install rhoconnect-push ## Deploying into J2EE environment In JRuby environment, there is an option to create the WAR container for the RhoConnect app and deploy it into the J2EE App Server. (see the corresponding 'rake' task for creating the WAR containers). Deploying the WAR container varies per J2EE App Server, for JBoss it is necessary to place the WAR file into the server's deploy directory. ## Deploying on Heroku Cedar #### Prerequisites * [Heroku account](https://api.heroku.com/login) and [Heroku Toolbelt](https://toolbelt.heroku.com/) * Rhoconnect gem version >= 3.2.0 #### Create Rhoconnect application Create your Rhoconnect app and make sure that it's up and running in your development environment using thin: :::term $ cd your_rhoconnect_app $ bundle exec thin start #### Declare `Procfile` This step is optional, but it will give you more control and flexibility when application deployed on Heroku. Create in your rhoconnect project file named `Procfile`: :::term web: bundle exec thin start -p $PORT If your application is using resque worker, then you should specify it in the `Procfile` as well: :::term web: bundle exec thin start -p $PORT worker: bundle exec rake resque:work Now you can run your app locally: :::term $ gem install foreman $ foreman start #### Deploy to Heroku Before moving your code to Git you need comment in your project's `Gemfile` declaration of `sqlite3` gem, because target Heroku server has no development libraries required to build that gem. :::term $ git init $ git add . $ git commit -m ' ... ' $ heroku create --stack cedar $ heroku addons:add redistogo:nano $ git push heroku master $ heroku open #### Monitoring app on Heroku Use `heroku ps` to determine the number of processes that are executing. Use `heroku logs` to view an aggregated list of log messages from all process types. :::term $ heroku ps $ heroku logs #### Get connected with Rhodes client If you have a rhodes client and wanted to sync it with rhoconnect app on Heroku, then set proper `syncserver` URL in `rhoconfig.txt` file: :::term syncserver = 'http://.herokuapp.com/application'