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.

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.

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:

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:

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:

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.

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.

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:

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.

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.

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.

10. Restart Aptana.

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