eomeroff
eomeroff

Reputation: 9925

Run rails application with apache and passenger on ubuntu

I have got some source code or rails project and I need to to run in on local machine. Actually on Ubuntu virtual machine.

I am not sure what all I need to install and configure in order to run it locally. Is there a standard way to find it all throughout source code?

I have figure out that application is using SQL Lite in development environment and MySQL in production environment.

from config/database.rb

# MySQL.  Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
#   gem install mysql2
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

staging:
  adapter: mysql2
  database: appname_staging
  username: rails
  password: somepassword
  host: localhost

production:
  adapter: mysql2
  database: appname_production
  username: rails
  password: somepassword
  host: localhost

Also I have set proper version of ruby and rails and install it with rvm. I know that the versions are correct because of .ruby-gemset .ruby-version files. Also I did bundle install and all gems from Gemfile installed successfully.

ruby -v => ruby 1.9.3p551 rails -v => 3.2.13

apache2 -v => Server version: Apache/2.4.7 (Ubuntu) Server built: Oct 14 2015 14:18:49

mysql -V => mysql Ver 14.14 Distrib 5.5.46, for debian-linux-gnu (i686) using readline 6.3

lsb_release -a => No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 14.04.3 LTS Release: 14.04 Codename: trusty

This is pretty much where my expertise stops. I am not sure which web server application should use. Is it predefined in source code somewhere?

I spot that there is apache, passenger and moonshine are used.

from app/manifests/application_manifest.rb

# The default_stack recipe install Rails, Apache, Passenger, the database from
  # database.yml, Postfix, Cron, logrotate and NTP. See lib/moonshine/manifest/rails.rb
  # for details. To customize, remove this recipe and specify the components you want.
  recipe :default_stack
  recipe :ssh

I also not sure what is exactly purpose of each of these individually.

How apache as web server can recognize and interpret ruby code?

I would be happy if I could host any rails app on apache. I have created some very simple app and tries these steps https://www.linode.com/docs/websites/ror/ruby-on-rails-with-apache-on-ubuntu-10-04-lucid but it always opens default apache web page.

I am not usre where to start. I just have a bunch of questions adn a lot of confustion. I do not understand how the ROR on apache actually works.

Is the passenger the thing upon apache that interprets ruby code?

I show that nginx is alternative for passenger but it is also alternative for apache, it confuses me even more.

Thanks for any help.

Upvotes: 4

Views: 1406

Answers (3)

puneet18
puneet18

Reputation: 4427

As you stated that you use sqlite database so add gem 'sqlite3' in Gemfile and then run bundle install.

Then you have to create database and run all migrations to add tables.

Command:

rake db:create
rake db:migrate

Next, start server

Command:

rails server

Upvotes: 0

Rajkaran Mishra
Rajkaran Mishra

Reputation: 4952

The web is an extremely concurrent environment. Production web server such as Apache can work on several requests — even tens or hundreds of requests — at the same time. A single-process, Ruby-based web server can't possibly keep up, and luckily it doesn’t have to.

Instead, the way we deploy a Rails application into production is to use a front-end server, such as Apache, to handle requests from the client. Then, we use the HTTP proxying of Passenger to send requests that should be handled by Rails to one of any number of back-end application processes.

I assume you have installed Apache web server and it is running on your machine.

Run following commands to install Passenger:

$ gem install passenger -v 5.0.30
$ passenger-install-apache2-module

If the necessary dependencies aren’t met, the latter command will tell you what you need to do. If this happens, follow the provided instructions, and try the Passenger install command again.

During the process, it’ll ask you to update your Apache configuration. The first request will be to enable your freshly built module, which involves adding lines such as the following to your Apache configuration.

LoadModule passenger_module /home/devel/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/passenger-5.1.5/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
    PassengerRoot /home/devel/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/passenger-5.1.5
    PassengerDefaultRuby /home/devel/.rbenv/versions/2.4.0/bin/ruby
</IfModule>

(Note: Passenger will tell you the exact lines to copy and paste into this file, so use those, not these)

To find out where your Apache configuration file is, try issuing the following command:

$ apachectl -V | grep HTTPD_ROOT
$ apachectl -V | grep SERVER_CONFIG_FILE

Deploying the Application

The previous step needs to be done only once per server, this step is actually once per application. Substitute your host's name, your application's directory path, and a secret key in the following:

<VirtualHost *:80>
  ServerName depot.yourhost.com
  DocumentRoot /home/devel/depot/public/
  SetEnv SECRET_KEY_BASE "0123456789abcdef"
  SetEnv DEPOT_DATABASE_PASSWORD "some-password"
  <Directory /home/devel/depot/public/>
    AllowOverride all
    Options -MultiViews
    Require all granted
  </Directory>
</VirtualHost>

Use following command to generate a suitable key to be used as the secret. This key is used to encrypt cookies that are sent to the client.

$ bin/rails secret

Here we are using SetEnv directive to define environment variables, instead of keeping our secret key and database password in config files for production mode.

The final step is to restart our Apache web server:

$ sudo apachectl restart

Upvotes: 1

philomory
philomory

Reputation: 1767

The web server used by the application is not predefined, and can be chosen based on your deployment needs. There are a lot of different options, but before you start worrying about which server is your best choice for a production environment, I'd recommend you try running the application using WEBRick, a simple ruby HTTP server which is part of Ruby's standard library. It's trivially easy to get started with: from root directory of the app, the one that contains the Rakefile and the Gemfile, execute:

rake db:setup # setup an sqlite database for development
rails server # start the development server

(Note that if you don't already have it, you'll need to install SQLite with sudo apt-get install sqlite3 libsqlite3-dev)

Moving on from there, Moonshine is a deployment automation tool. It can help you deploy an application to a production server, but it's not an integral part of the application.

Phusion Passenger is the answer to your question of "How apache as web server can recognize and interpret ruby code?" However, it's not just for Apache; it can be used in standalone more or with nginx instead. In any case, the web server (Apache, nginx, or an embedded one for standalone mode) accepts the request and hands it to Passenger, which 'translates' it into a standardized format that Rails understands and passes it along.

Personally, if you're just trying to familiarize yourself with Rails, and don't have a specific reason to aim for Apache, I'd start with Standalone mode as it's simpler to set up. Either way, their Quickstart and Deployment Guides are excellent.

Postfix is an SMTP server; you'll need it (or some other SMTP server) to send email from your Rails app, though strictly speaking it doesn't need to be installed on the same server that rails is, and if you don't plan on sending email you don't need it at all.

Cron, logrotate and NTP are all system administration tools. Nothing in Rails inherently depends on any of them, though the specific app chosen may. Logrotate keeps your log files in check, NTP is for making sure the system clock has the right time, and cron is for running tasks on a schedule. Moonshine sets them all up by default, but it's entirely possible you don't actually need them, especially if you're just 'testing out' the Rails app.

Finally, you mentioned the MySQL installation, but it isn't enough to just have MySQL installed; you'll also need to set up the database tables. Fortunately, this isn't much harder in the production environment than it was for development; assuming you the mysql config in database.yml points to your actual MySQL installation with a valid username and password, you can run RAILS_ENV=production rake db:setup to get that going. Again, though, I'd recommend starting off in development mode with your sqlite database first, to keep it simple.

Upvotes: 0

Related Questions