googlediscourse

Deploy a new Rails application

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 server blocks will run the Rails application in production mode with RAILS_ENV=production.

  • Restart NGINX:

      $ sudo /opt/bitnami/ctlscript.sh restart nginx
    
Create a custom server block

If the predefined server blocks 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 server block 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 server block 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
    
Last modification February 9, 2023