Ruby on Rails

    Quick reference card

      Cloud Servers Virtual Machines Native Installers
    Command line interface (Ruby) /opt/bitnami/ruby/bin/ruby /opt/bitnami/ruby/bin/ruby install_dir/ruby/bin/ruby (Linux and Mac OS X)
    install_dir/ruby/ruby.exe (Windows)
    Command line interface (Rails) /opt/bitnami/ruby/bin/rails /opt/bitnami/ruby/bin/rails install_dir/ruby/bin/rails

    How to create a new Rails Web application

    This guide walks you through the process of downloading and installing a custom Ruby on Rails (RoR) application on a Bitnami Cloud Hosting server. It assumes that you already have your RoR application in Github and that your application uses a PostgreSQL or MySQL database.

    1. Create a new server using Amazon EC2 or Bitnami Cloud Hosting.

    • For Amazon EC2, simply select the Bitnami Ruby Stack from the Amazon Marketplace.
    • For Bitnami Cloud Hosting, create a new server using the following settings as a guideline:
      • Application options: Choose a general user name and set an application password. Store this information in a secure location, as you will need it later.
      • Development options: Add Phusion Passenger and PostgreSQL.

      • Choose the default 64-bit version of Ubuntu Linux.
        IMPORTANT: Bitnami does not support deploying RoR applications using Amazon Linux or Red Hat Linux.
      • Choose an instance and disk size according to your needs. A T1 micro instance with 10 GB of disk storage is usually fine for most development tasks.
      • Build and launch the cloud server.
         
    TIP: Bitnami allows you to restore your entire machine to a new, more powerful machine if needed, so you can always scale up or down later.

    2. Log in using your favorite terminal emulator. You'll need the .pem or .ppk file from the Amazon EC2 or Bitnami interface for this (detailed instructions).

    3. Verify that you have everything you need by running the following commands. All of them should return a valid path.

    $ which rvm
    $ which rails
    $ which psql
    $ which passenger
    

    4. Create a directory for your application. Here, we'll assume the directory is /opt/bitnami/apps/myapp.

    $ sudo mkdir /opt/bitnami/apps/myapp
    $ cd /opt/bitnami/apps/myapp
    $ sudo chown bitnami . 
    

    5. Clone your project into this directory using Git (download a sample application).

    $ cd /opt/bitnami/apps/myapp
    $ git clone http://url-to-git-repository/myapp.git .
    

    6. You may want to check and modify your Gemfile if needed at this point. Once these changes are made, use the following commands to install the necessary dependencies.

    $ cd /opt/bitnami/apps/myapp
    $ bundle install
    

    7. Make the directories myapp/, myapp/config, myapp/public and their parent directories readable and executable by the Apache Web server. This server runs as the user 'daemon', so change the group and permissions accordingly.

    $ cd /opt/bitnami/apps/myapp
    $ sudo chgrp daemon .
    $ sudo chgrp daemon -R config public
    $ chmod 755 .
    $ chmod 750 -R config public        
    

    8. Next, export your application database to an SQL text file and then import it into the database on your cloud server.

    • If you’re using PostgreSQL, you can either use a Web-based tool like phpPgAdmin or the PostgreSQL command-line tools. If you're using the command-line tools, here's an example command to backup the database to a file named backup.sql:
      $  pg_dump -U postgres -C mydb > /tmp/backup.sql
      

      Now, upload your backup file to the cloud server using SFTP (detailed instructions) and save it to your /home/bitnami directory. Then, import its contents using the PostgreSQL command-line tool at the server console. Here is an example command to restore the database using the psql command-line tool.
      $  psql -U postgres < ~/backup.sql
      

      You will need to enter the application password you set in Step 1.
    • If you’re using MySQL, you can either use a Web-based tool like phpMyAdmin or the MySQL command-line tools. If you're using the command-line tools, here's an example command to backup the database to a file named backup.sql:
      $ mysqldump -u root -p --add-drop-database --databases mydb > /tmp/backup.sql
      

      Now, upload your backup file to the cloud server using SFTP (detailed instructions) and save it to your /home/bitnami directory. Then, import its contents using the MySQL command-line tool at the server console. Here is an example command to restore the database using the mysql command-line tool.
      $  mysql -u root -p < ~/backup.sql
      

      You will need to enter the application password you set in Step 1.

     

    NOTE: The Bitnami Ruby Stack includes phpPgAdmin and phpMyAdmin, Web-based tools to work with PostgreSQL and MySQL databases respectively. If you’d like to use these, refer to these instructions to configure a secure tunnel. You can log in to these tools with username 'postgres' for phpPgAdmin or ‘root’ for phpMyAdmin and the application password you set in Step 1. You can now create a new database and import your backup file through the corresponding Web interface.


    9. To secure your database, it's a good idea to create a separate database user for the RoR app and give it privileges only to the new database. Here, we’ll assume the user is named ‘john’ with password ‘guessme’.

    • If you’re using PostgreSQL:
      $ psql -U postgres
      postgres=# \c mydb
      postgres=# CREATE USER john WITH PASSWORD 'guessme';
      postgres=# GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO john;
      postgres=# GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO john;
      postgres=# \q
      

      Once the new user account is created and granted privileges, you should test it by connecting as the new user and performing a quick SELECT query on one of the tables to ensure it is all working correctly. For example:

      $ psql -U john mydb
      postgres=# SELECT * from table;
      postgres=# \q
      
    • If you’re using MySQL:
      $ mysql -u root -p
      mysql>  GRANT ALL ON mydb.* TO 'john'@'localhost' IDENTIFIED BY 'guessme';
      mysql> exit
      

      Once the new user account is created and granted privileges, you should test it by connecting as the new user and performing a quick SELECT query on one of the tables to ensure it is all working correctly. For example:

      $ mysql -u john -p
      mysql> USE mydb
      mysql> SELECT * from table;
      mysql> exit
      

    10. The system variable RAILS_ENV determines the environment Rails will use, so depending on your requirements, make sure RAILS_ENV is set to whatever you need (we'll assume 'production' for the remainder of this article). To set the variable, execute this command:

    $ export RAILS_ENV=production
    

    11. Update the database configuration for the Rails application and environment by editing the application's config/database.yml file. Add a section like the one below (or modify the section if it already exists). Remember to replace the dummy values shown with actual values for your database, user and password, and to replace ‘postgresql’ with ‘mysql’ if you’re using MySQL.

     production:
       adapter: postgresql
       database: my-db
       host: localhost
       user: john
       password: guessme
    

     

    NOTE: The database.yml file should contain only spaces, not tabs.

    12. Run the following command to complete the database settings.

    $ rake db:migrate
    

    13. You will also need a secret key for the application. Obtain this by running the following command:       

    $ cd /opt/bitnami/apps/myapp
    $ rake secret
    

    Ideally, the output from this command should be saved as an environment variable named $SECRET_KEY_BASE, which will then be read by Rails as needed. If this doesn’t work for you, you can also hardcode the key into the config/secrets.yml file, although this is less secure.

    production:
         # secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
         secret_key_base: output_from_rake_secret
    

    14. Pre-compile the assets for your application by running the following command:

    $ cd /opt/bitnami/apps/myapp
    $ rake assets:precompile        
    

    15. Enable the Passenger module (disabled by default) by editing the file /opt/bitnami/apache2/conf/bitnami/passenger.conf and changing the line

    PassengerEnabled off
    

    to

    PassengerEnabled on
    

    The Passenger module runs as the 'daemon' user. If your application needs to write files into a server directory, such as myapp/uploads/,  you must change the owner of the directory to 'daemon':

    $ sudo chown daemon:daemon uploads
    

    16. Create a virtual host for the RoR application. Within the myapp/ directory, create a new conf/ subdirectory. Within the new conf/ subdirectory, use your text editor to create and populate a file named httpd-vhosts.conf with the following content:

    <VirtualHost *:80>
        ServerName some-cool-domain-name.com
        DocumentRoot /opt/bitnami/apps/myapp/public
        #RailsEnv production # optional - uncomment to make effective
    
        <Directory "/opt/bitnami/apps/myapp/public">
          AllowOverride all
          Require all granted
        </Directory>
    </VirtualHost>
    

    Remember to replace some-cool-domain-name.com with an actual domain name for your RoR application, and to update your domain's DNS to point to the IP address of your cloud server.

    Then, edit the /opt/bitnami/apache2/conf/bitnami/bitnami-apps-vhosts.conf file and add the following line to the end:

    Include "/opt/bitnami/apps/myapp/conf/httpd-vhosts.conf"
    

    17. Restart the Apache Web server for these changes to take effect.

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    

    You should now be able to access your Rails application at the specified domain name.
     

    TIP: If something goes wrong, you should be able to find out why by inspecting the Apache error log, at /opt/bitnami/apache2/logs/error_log. It may be helpful to keep the file open and updated in real-time while fixing issues, using the command below:

     $ tail -f /opt/bitnami/apache2/logs/error_log
    

    How to create a Rails development environment on Windows with Aptana RadRails

    Follow these steps to create an Aptana RadRails development environment on Windows with the Bitnami Ruby Stack. This environment includes the Ruby, Rails, ImageMagick and other libraries and provides visual debugging.

    1. Download and install the Bitnami Ruby Stack native installer for Windows. In this example, we'll assume the Bitnami Ruby Stack is installed in the C:\rubystack directory.

    2. Download and install Aptana RadRails.

    3. Within Aptana, select the “Window -> Preferences -> Ruby -> Installed Interpreters” menu item. Click the "Add" button and enter “C:\rubystack\ruby” in the "RubyVM home directory” field. Click “OK” to save your changes.

    image1.png


    4. In the resulting list, select the Ruby VM you just added as the default interpreter.

    image2.png
    5. Within Aptana, select the “Window -> Preferences -> Ruby -> Rake” menu item. Enter “C:\rubystack\ruby\bin\rake” in the "rake path" field. Click “OK” to save your changes.

    image3.png


    6. Within Aptana, select the ”Window -> Preferences -> Rails” menu item. Enter “C:\rubystack\ruby\bin\rails” in the “rails path” fields. Click “OK” to save your changes.

    image4b.png

     

    NOTE: Remember to update the directory paths in Steps 3, 5 and 6 to reflect the correct installation directory for your system.

     
    7. Within Aptana, select the “Window -> Preferences -> Ruby -> Debugger” item. Select the “Use ruby-debug library" option.
    image5.png


    8. Open a new Windows command prompt (type command or cmd in the Windows Start Menu or Start screen search box). Change to the Bitnami Ruby Stack installation directory and run the following commands to install the ruby-debug-ide gem.

    use_ruby.bat
    gem install ruby-debug-ide
    

     

    NOTE: In case you see SSL-related errors, edit the file C:\rubystack\ruby\lib\ruby\2.0.0\rubygems\defaults.rb and replace all instances of the URL https://rubygems.org with http://rubygems.org. Then, try installing the gem again.

    9. Open the Windows Control Panel and select the “System” icon. In the resulting “System Properties” dialog box, select the “Advanced -> Environment Variables” option. Make the following changes:

    • In the “System variables” section, select the PATH variable, click “Edit…” and prepend the following paths to it: C:\rubystack\sqlite;C:\rubystack\subversion\bin;C:\rubystack\imagemagick;C:\rubystack\php;C:\rubystack\mysql\bin;C:\rubystack\ruby\bin;. Then, click “OK” to save your changes.

    image6.png

    • In the “System variables” section, click “New…” and add a new variable named MAGICK_CODER_MODULE_PATH with value C:\rubystack\imagemagick\modules\coders. Click “OK” to save your changes.

    image7.png


    10. Restart Aptana.

    You should now be able to add or start Ruby servers through Aptana, in debug mode or otherwise.
     

    Tag page (Edit tags)
    • No tags
    Pages that link here
    Page statistics
    49704 view(s), 19 edit(s) and 19153 character(s)

    Comments

    You must login to post a comment.