generaljruby

Deploy a new Rails application

There are different ways to deploy a Rails application.

Use Apache with Passenger

Enable Apache 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.

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 the bundled web server in the Rails project

Start the Rails server by running the command below. Remember to replace the APPNAME placeholder with the name of your project:

$ cd /opt/bitnami/projects/APPNAME
$ bundle exec rails server

This will start the Rails server for your application on port 3000. Find more information about how to access the application using your Web browser.

Last modification December 21, 2022