Installing and Configuring Apache with Ruby on Rails and MySQL on Debian Jessie

What's up folks, here I will show you how to install and configure the Apache supporting Ruby on Rails and how to install and configure the MySQL, the process is very easy.

Set up your system with the follow script to ensure that nothing will be missing http://wiki.douglasqsantos.com.br/doku.php/confinicialjessie_en

  • The Apache will use the follow ip: 192.168.25.100

Let's install the dependencies for Ruby on Rails, here we have some packets that is very usual in a lot of gems used by Rails so let's think ahead.

aptitude install bison autoconf automake ca-certificates ethtool flex g++ gcc gcc-4.4 make zlib1g-dev pkg-config checkinstall mysql-server default-jdk \
mysql-common mysql-client libmysqlclient-dev libyaml-dev git-core imagemagick libmagickwand-dev build-essential libssl-dev libreadline-gplv2-dev \
zlib1g-dev linux-headers-$(uname -r) libsqlite3-dev libxslt1-dev libxml2-dev libapache2-mod-passenger libmysql++-dev apache2-prefork-dev libssl-dev libtool \
apache2-mpm-worker libcurl4-openssl-dev ruby ruby-dev libcurl4-openssl-dev curl libmnl-dev libnfnetlink-dev libnetfilter-queue-dev -y

Note: Above the installation wizard will ask about the MySQL password then set up it.

Faster Gem Installation

By default, when you install gems, documentation files will be installed. Developers seldom use gem documentation files (they'll browse the web instead). Installing gem documentation files takes time, so many developers like to toggle the default so no documentation is installed.

Here’s how to speed up gem installation by disabling the documentation step:

echo "gem: --no-document" >> ~/.gemrc

Now we need to install the rails and rake and mysql

gem install bundler rails
gem install rake
gem install mysql2

Now let's access the apache document root and let's create a rails application test.

cd /var/www/html/
rails new posts -d mysql

Now we need to configure the Gemfile to enable the JavaScript

Note: Until now 27/09/2015 we have some problems with rails 4.2.4 and mysql2 with version 4.0.1 so we need to use the version 0.3.20 to work.

vim /var/www/html/posts/Gemfile
source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
# Use mysql as the database for Active Record
gem 'mysql2', '~> 0.3.20'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'execjs'
gem 'therubyracer', platforms: :ruby
[...]

Now we need to run the bundle install to install the new gems.

cd /var/www/html/posts
bundle install 

Now we need to create a copy of the database configuration file.

cd /var/www/html/posts/config/
cp database.yml database.yml.bkp

Now we need to create the database to posts

mysql -u root -p
CREATE DATABASE rails;
GRANT ALL PRIVILEGES ON rails.* TO rails@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
exit

Now we need to enable the remote access, because we usually work on your own desktop and needs to access the database to work with the database.

vim /etc/mysql/my.cnf
[...]
bind-address            = 0.0.0.0

Now we need to restart the mysql server as follows

/etc/init.d/mysql restart

Now we need to configure the database connection file.

vim /var/www/html/posts/config/database.yml
#/var/www/html/posts/config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: rails
  password: "password"
  port: 3306

development:
  <<: *default
  database: rails

Now let's create the scaffold of the posts page

cd /var/www/html/posts
rails generate scaffold posts title:string content:text

Now we need to run the rake db:migrate to load the database configuration

rake db:migrate RAILS_ENV=development

Now we need to create the virtual host that Rails will use

vim /etc/apache2/sites-available/rails.conf
<VirtualHost *:80>
      ServerName rails.douglasqsantos.com.br
      DocumentRoot /var/www/html/posts/public
      RailsEnv development
      <Directory /var/www/html/posts/public>
        AllowOverride all
        Options -MultiViews
      </Directory>
        ServerSignature Off
        LogLevel info
        CustomLog /var/log/apache2/rails.douglasqsantos.com.br-access.log combined
        ErrorLog /var/log/apache2/rails.douglasqsantos.com.br-error.log
</VirtualHost>

Now we need to set the permissions on the Rails directory

chown -R www-data:www-data /var/www/html/posts

Now let's disable the default virtual host and enable the Rails

a2dissite 000-default.conf
a2ensite rails.conf

Now we can restart the Apache server

/etc/init.d/apache2 restart

Now we can access the Rails application on http://rails.douglasqsantos.com.br or http://192.168.25.100

Now we can access the posts page on http://rails.douglasqsantos.com.br/posts or http://192.168.25.100/posts

Now we can check the log files as follows

tail -f /var/log/apache2/rails.douglasqsantos.com.br-*

Note The environment was configure on the development mode if you need to run in the production mode needs to configure the (`secret_token` and `secret_key_base` for 'production' environment, set these values in `config/secrets.yml`) after change the values, change the database configuration if you need, after that needs to change the RailsEnv on rails.conf inside /etc/apache2/sites-available/

References