Lampros - Weird Bricks

nginx/rails Fat Free CRM example with MySQL support on FreeBSD 8.2

14 December, 2011 | Ruby on Rails

So on a previous post, we saw how to setup nginx with SQLite and setup Fat Free CRM as a Ruby on Rails test application. 


This post picks up from where the last one ended, since most of the things here are the same.

Prerequisites:


Here we go:

Note: When starting, restarting or stopping MySQL, make sure you are NOT on the Bash shell. If you are just enter exit to quit.

Go to the Fat Free CRM application directory:

cd /var/fatfreecrm


Copy the sample MySQL config file:

cp config/database.mysql.linux.yml config/database.yml


Edit the MySQL config file:

ee config/database.yml


Under 'production' comment out the 'socket' line and add this:

 host: localhost


The production part should look like this now:

production:
  adapter: mysql2
  encoding: utf8
  database: fat_free_crm_production
  pool: 5
  username: root
  password:
#  socket: /var/run/mysqld/mysqld.sock
  host: localhost


Edit the Gemfile:

ee Gemfile


Make sure to comment out sqlite3 and postgres (pg) and uncomment mysql2:

When you're done it should look like this:

source 'http://rubygems.org'

gem 'rails', '3.0.7'
gem 'rake',  '0.8.7'

# Uncomment the database that you have configured in config/database.yml
# ----------------------------------------------------------------------
 gem "mysql2", "0.2.7"
# gem "sqlite3"
#gem "pg", ">= 0.9.0"

gem 'authlogic',           '~> 3.0.3'


Run bundler:

bundle install


You should see something like the following:

Installing mysql2 (0.2.7) with native extensions
...
...
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.


Now let's prepare the database for Fat Free CRM:

RAILS_ENV=production bundle exec rake db:create


That's OK - continue with setting up a default user:

RAILS_ENV=production bundle exec rake crm:setup USERNAME=admin PASSWORD=password EMAIL=admin@example.com


Now populate the database with some sample values:

RAILS_ENV=production bundle exec rake crm:demo:load


Let's have a look inside MySQL:

mysql


Type:

show databases;


You should get:

+-------------------------+
| Database                |
+-------------------------+
| information_schema      |
| fat_free_crm_production |
| mysql                   |
| performance_schema      |
| test                    |
+-------------------------+
5 rows in set (0.00 sec)


Type:

use fat_free_crm_production;
show tables;


You'll get lots of output:

+-----------------------------------+
| Tables_in_fat_free_crm_production |
+-----------------------------------+
| account_contacts                  |
| account_opportunities             |
| accounts                          |
| activities                        |
| addresses                         |
| avatars                           |
| campaigns                         |
| comments                          |
| contact_opportunities             |
| contacts                          |
| emails                            |
| field_groups                      |
| fields                            |
| leads                             |
| opportunities                     |
| permissions                       |
| preferences                       |
| schema_migrations                 |
| sessions                          |
| settings                          |
| taggings                          |
| tags                              |
| tasks                             |
| users                             |
+-----------------------------------+
24 rows in set (0.00 sec)


Let's have a look at the users table, see what's it's made of:

describe users;


lots of fields:

+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| id                  | int(11)      | NO   | PRI | NULL    | auto_increment |
| username            | varchar(32)  | NO   | MUL |         |                |
| email               | varchar(64)  | NO   | MUL |         |                |
| first_name          | varchar(32)  | YES  |     | NULL    |                |
| last_name           | varchar(32)  | YES  |     | NULL    |                |
| title               | varchar(64)  | YES  |     | NULL    |                |
| company             | varchar(64)  | YES  |     | NULL    |                |
| alt_email           | varchar(64)  | YES  |     | NULL    |                |
| phone               | varchar(32)  | YES  |     | NULL    |                |
| mobile              | varchar(32)  | YES  |     | NULL    |                |
| aim                 | varchar(32)  | YES  |     | NULL    |                |
| yahoo               | varchar(32)  | YES  |     | NULL    |                |
| google              | varchar(32)  | YES  |     | NULL    |                |
| skype               | varchar(32)  | YES  |     | NULL    |                |
| password_hash       | varchar(255) | NO   |     |         |                |
| password_salt       | varchar(255) | NO   |     |         |                |
| persistence_token   | varchar(255) | NO   | MUL |         |                |
| perishable_token    | varchar(255) | NO   | MUL |         |                |
| last_request_at     | datetime     | YES  | MUL | NULL    |                |
| last_login_at       | datetime     | YES  |     | NULL    |                |
| current_login_at    | datetime     | YES  |     | NULL    |                |
| last_login_ip       | varchar(255) | YES  |     | NULL    |                |
| current_login_ip    | varchar(255) | YES  |     | NULL    |                |
| login_count         | int(11)      | NO   |     | 0       |                |
| deleted_at          | datetime     | YES  |     | NULL    |                |
| created_at          | datetime     | YES  |     | NULL    |                |
| updated_at          | datetime     | YES  |     | NULL    |                |
| admin               | tinyint(1)   | NO   |     | 0       |                |
| suspended_at        | datetime     | YES  |     | NULL    |                |
| single_access_token | varchar(255) | YES  |     | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+
30 rows in set (0.00 sec)


Just exit MySQL and start the application by restarting nginx:

/etc/init.d/nginx-passenger restart