bchruby

Deploy a new Rails application

NOTE: We are in the process of modifying the file structure and configuration for many Bitnami stacks. On account of these changes, the file paths stated in this guide may change depending on whether your Bitnami stack uses native Linux system packages (Approach A), or if it is a self-contained installation (Approach B). To identify your Bitnami installation type and what approach to follow, run the command below:

 $ test ! -f "/opt/bitnami/common/bin/openssl" && echo "Approach A: Using system packages." || echo "Approach B: Self-contained installation."

The output of the command indicates which approach (A or B) is used by the installation, and will allow you to identify the paths, configuration and commands to use in this guide. Refer to the FAQ for more information on these changes.

There are different ways to deploy a Rails application depending on your installation type.

Approach A: Bitnami installations using system packages

Enable web server support in the the Rails application

By default, the web server is configured to run the Rails application in production mode. This requires a few changes to your Rails project:

  • Configure the database credentials for production use. Edit the production section in the config/database.yml file inside your Rails project directory as shown below:

    production:
      <<: *default
      username: USERNAME
      password: PASSWORD
      database: DATABASE
    

    NOTE: USERNAME, PASSWORD and DATABASE are placeholders for the database username, password and the database name, respectively. Learn how to obtain the MariaDB database credentials and how to create a MariaDB database and user.

  • Initialize the database in production mode:

    $ cd /opt/bitnami/projects/APPNAME
    $ bundle exec rails db:prepare RAILS_ENV=production
    

    NOTE: Replace the APPNAME placeholder with the actual name of your project.

  • Precompile the assets used by the Rails application:

    $ bundle exec rails assets:precompile RAILS_ENV=production
    
  • Add write permissions for the web server to the Rails project. The web server runs as the daemon user and does not have write privileges by default. To fix this, run the following commands:

    $ sudo chown -R $USER:daemon /opt/bitnami/projects/APPNAME
    $ sudo chmod -R g+rwX /opt/bitnami/projects/APPNAME
    

    NOTE: Replace the APPNAME placeholder with the actual name of your project.

Use Apache with Passenger

Enable predefined virtual hosts for a Rails application

The Bitnami installation comes with predefined HTTP and HTTPS virtual hosts for running Rails applications with Phusion Passenger. To enable them, follow the steps below:

  • Copy the files to remove the .disabled suffix:

    $ sudo cp /opt/bitnami/apache/conf/vhosts/sample-vhost.conf.disabled /opt/bitnami/apache/conf/vhosts/sample-vhost.conf
    $ sudo cp /opt/bitnami/apache/conf/vhosts/sample-https-vhost.conf.disabled /opt/bitnami/apache/conf/vhosts/sample-https-vhost.conf
    

    IMPORTANT: These sample virtual hosts will run the Rails application in production mode with RAILS_ENV=production.

  • Restart Apache for the changes to be taken into effect:

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    
Create a custom virtual host

If the predefined virtual hosts are not available to you, or if you prefer to apply a custom configuration, follow the steps below:

  • Create and edit the /opt/bitnami/apache/conf/vhosts/myapp-vhost.conf file and add the following lines:

    IMPORTANT: This virtual host will run the Rails application in production mode with RAILS_ENV=production.

    PassengerPreStart http://localhost:80/
    <VirtualHost _default_:80>
      ServerAlias *
      DocumentRoot "/opt/bitnami/projects/APPNAME/public"
      <Directory "/opt/bitnami/projects/APPNAME/public">
        Require all granted
        RailsEnv production
        PassengerEnabled on
      </Directory>
    </VirtualHost>
    

    NOTE: Replace the APPNAME placeholder with the actual name of your project.

  • Create and edit the /opt/bitnami/apache/conf/vhosts/myapp-https-vhost.conf file and add the following lines:

    IMPORTANT: This virtual host will run the Rails application in production mode with RAILS_ENV=production.

    PassengerPreStart http://localhost:443/
    <VirtualHost _default_:443>
      ServerAlias *
      SSLEngine on
      SSLCertificateFile "/opt/bitnami/apache/conf/bitnami/certs/server.crt"
      SSLCertificateKeyFile "/opt/bitnami/apache/conf/bitnami/certs/server.key"
      DocumentRoot "/opt/bitnami/projects/APPNAME/public"
      <Directory "/opt/bitnami/projects/APPNAME/public">
        Require all granted
        RailsEnv production
        PassengerEnabled on
      </Directory>
    </VirtualHost>
    

    NOTE: Replace the APPNAME placeholder with the actual name of your project.

  • Restart the Apache server:

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

Use NGINX with Passenger

Enable predefined server blocks for a Rails application

The Bitnami installation comes with predefined HTTP and HTTPS server blocks for running Rails applications with Phusion Passenger. To enable them, follow the steps below:

  • Copy the files to remove the .disabled suffix:

    $ sudo cp /opt/bitnami/nginx/conf/server_blocks/sample-server-block.conf.disabled /opt/bitnami/nginx/conf/server_blocks/sample-server-block.conf
    $ sudo cp /opt/bitnami/nginx/conf/server_blocks/sample-https-server-block.conf.disabled /opt/bitnami/nginx/conf/server_blocks/sample-https-server-block.conf
    

    IMPORTANT: These sample virtual hosts will run the Rails application in production mode with RAILS_ENV=production.

  • Restart NGINX:

    $ sudo /opt/bitnami/ctlscript.sh restart nginx
    
Create a custom virtual host

If the predefined virtual hosts are not available to you, or if you prefer to apply a custom configuration, follow the steps below:

  • Create and edit the /opt/bitnami/nginx/conf/server_blocks/myapp-http-server-block.conf file to add the following lines:

    IMPORTANT: This virtual host will run the Rails application in production mode with RAILS_ENV=production.

    server {
        # Port to listen on, can also be set in IP:PORT format
        listen 80 default_server;
        root /opt/bitnami/projects/APPNAME/public;
        # Catch-all server block
        # See: https://nginx.org/en/docs/http/server_names.html#miscellaneous_names
        server_name _;
        passenger_enabled on;
        rails_env production;
        include  "/opt/bitnami/nginx/conf/bitnami/*.conf";
    }
    

    NOTE: Replace the APPNAME placeholder with the actual name of your project.

  • Create and edit the /opt/bitnami/nginx/conf/server_blocks/myapp-https-server-block.conf file to add the following lines:

    IMPORTANT: This virtual host will run the Rails application in production mode with RAILS_ENV=production.

    server {
        # Port to listen on, can also be set in IP:PORT format
        listen 443 ssl default_server;
        root /opt/bitnami/projects/APPNAME/public;
        # Catch-all server block
        # See: https://nginx.org/en/docs/http/server_names.html#miscellaneous_names
        server_name _;
        ssl_certificate      bitnami/certs/server.crt;
        ssl_certificate_key  bitnami/certs/server.key;
        passenger_enabled on;
        rails_env production;
        include  "/opt/bitnami/nginx/conf/bitnami/*.conf";
    }
    

    NOTE: Replace the APPNAME placeholder with the actual name of your project.

  • Restart NGINX:

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

Approach B: Self-contained Bitnami installations

Use Apache with Passenger

The most common way to deploy a Rails application is with Apache and Passenger. Follow the steps below:

  • Create a new file at /home/bitnami/httpd-vhosts.conf and fill it with the following content.

    <VirtualHost *:80>
        ServerName HOST_NAME
        DocumentRoot "/home/bitnami/APPNAME/public/"
        <Directory "/home/bitnami/APPNAME/public/">
            Options -MultiViews
            <IfVersion < 2.3 >
            Order allow,deny
            Allow from all
            </IfVersion>
            <IfVersion >= 2.3>
            Require all granted
            </IfVersion>
            RailsEnv development
            PassengerEnabled on
            PassengerAppRoot "/home/bitnami/APPNAME/"
        </Directory>
    </VirtualHost>
    

    Remember to replace the HOST_NAME placeholder with the host name of your cloud server and the APPNAME placeholder with the correct directory for your Rails application.

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

    Include /home/bitnami/httpd-vhosts.conf
    
  • Restart the Apache server.

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

After restarting Apache, the application should be accessible by browsing to http://SERVER-IP/.

NOTE: Remember that you might need to adjust the permissions of your application directory and sub-directories to make them accessible to the user that the Apache server runs as - for example, with the command sudo chown bitnami:daemon /home/bitnami/APPNAME.

Use NGINX with Passenger

Follow these steps:

  • To configure NGINX to run at port 80, stop the Apache service. To do this, run the command below:

    $ sudo /opt/bitnami/ctlscript.sh stop apache
    
  • Once the Apache service has been stopped, modify the server configuration at /opt/bitnami/nginx/conf/nginx.conf to be similar to this.

    server {
        listen 80;
        root /opt/bitnami/APPNAME/public;
        passenger_enabled on;
        rails_env development;
    }
    

    NOTE: The configuration above runs Passenger in development mode because, by default, Passenger starts in production mode which requires a secret to be configured. Using development mode bypasses this requirement and lets you test that everything is working correctly. When deploying to production, remember to change this value and provide a

  • Before restarting NGINX to apply this configuration, disable Apache as shown below:

    $ sudo /opt/bitnami/ctlscript.sh stop apache
    $ sudo mv /opt/bitnami/apache2/scripts/ctl.sh /opt/bitnami/apache2/scripts/ctl.sh.disabled
    $ sudo mv /opt/bitnami/config/monit/conf.d/apache.conf /opt/bitnami/config/monit/apache.conf.disabled
    
  • Restart the NGINX service to apply the new configuration:

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

With this, your Ruby on Rails application will run at the root of your domain through the default HTTP port (80). You should now be able to access your Ruby on Rails applications with NGINX and Passenger on http://SERVER-IP/.

Last modification July 29, 2020