Differences

This shows you the differences between two versions of the page.

Link to this comparison view

installing_and_configuring_apache_with_ruby_on_rails_and_mysql_on_debian_jessie_en [2017/09/05 12:18] (current)
Line 1: Line 1:
 +====== 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 <​nowiki>​MySQL</​nowiki>,​ 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.
 +
 +<sxh bash>
 +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
 +</​sxh>​
 +
 +**Note:** Above the installation wizard will ask about the <​nowiki>​MySQL</​nowiki>​ 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:
 +<sxh bash>
 +echo "gem: --no-document"​ >> ~/.gemrc
 +</​sxh>​
 +
 +Now we need to install the rails and rake and mysql
 +<sxh bash>
 +gem install bundler rails
 +gem install rake
 +gem install mysql2
 +</​sxh>​
 +
 +Now let's access the apache document root and let's create a rails application test.
 +<sxh bash>
 +cd /​var/​www/​html/​
 +rails new posts -d mysql
 +</​sxh>​
 +
 +Now we need to configure the Gemfile to enable the <​nowiki>​JavaScript</​nowiki>​
 +
 +**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.
 +<sxh bash>
 +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
 +[...]
 +</​sxh>​
 +
 +Now we need to run the bundle install to install the new gems.
 +<sxh bash>
 +cd /​var/​www/​html/​posts
 +bundle install ​
 +</​sxh>​
 +
 +Now we need to create a copy of the database configuration file.
 +<sxh bash>
 +cd /​var/​www/​html/​posts/​config/​
 +cp database.yml database.yml.bkp
 +</​sxh>​
 +
 +
 +Now we need to create the database to posts
 +<sxh sql>
 +mysql -u root -p
 +CREATE DATABASE rails;
 +GRANT ALL PRIVILEGES ON rails.* TO rails@'​%'​ IDENTIFIED BY '​password';​
 +FLUSH PRIVILEGES;
 +exit
 +</​sxh>​
 +
 +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.
 +<sxh apache>
 +vim /​etc/​mysql/​my.cnf
 +[...]
 +bind-address ​           = 0.0.0.0
 +</​sxh>​
 +
 +Now we need to restart the mysql server as follows
 +
 +<sxh bash>
 +/​etc/​init.d/​mysql restart
 +</​sxh>​
 +
 +Now we need to configure the database connection file.
 +<sxh yaml>
 +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
 +</​sxh>​
 +
 +Now let's create the scaffold of the posts page
 +<sxh bash>
 +cd /​var/​www/​html/​posts
 +rails generate scaffold posts title:​string content:​text
 +</​sxh>​
 +
 +Now we need to run the rake db:migrate to load the database configuration
 +<sxh bash>
 +rake db:migrate RAILS_ENV=development
 +</​sxh>​
 +
 +
 +Now we need to create the virtual host that Rails will use
 +
 +<sxh apache>
 +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>​
 +</​sxh>​
 +
 +Now we need to set the permissions on the Rails directory
 +
 +<sxh bash>
 +chown -R www-data:​www-data /​var/​www/​html/​posts
 +</​sxh>​
 +
 +Now let's disable the default virtual host and enable the Rails
 +
 +<sxh bash>
 +a2dissite 000-default.conf
 +a2ensite rails.conf
 +</​sxh>​
 +
 +Now we can restart the Apache server
 +
 +<sxh bash>
 +/​etc/​init.d/​apache2 restart
 +</​sxh>​
 +
 +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
 +<sxh bash>
 +tail -f /​var/​log/​apache2/​rails.douglasqsantos.com.br-*
 +</​sxh>​
 +
 +**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 <​nowiki>​RailsEnv</​nowiki>​ on rails.conf inside /​etc/​apache2/​sites-available/ ​
 +====== References ======
 +
 +  - http://​www.ruby-lang.org/​pt/​
 +  - http://​www.ruby-lang.org/​pt/​documentacao/​