Ruby on Rails on Ubuntu 22.04: Production Deployment with Nginx & Puma

Introduction Deploying a Ruby on Rails application on Ubuntu 22.04 for production can be a daunting task for beginners and seasoned developers alike....


0

Introduction

Deploying a Ruby on Rails application on Ubuntu 22.04 for production can be a daunting task for beginners and seasoned developers alike. This guide will walk you through the process of deploying your Rails app using Nginx as a web server and Puma as an application server. By the end, you will have a fully functioning Ruby on Rails application running on Ubuntu with a robust setup. The focus here is to ensure that your deployment is not only functional but also optimized for performance and security. We’ll cover everything from setting up your environment to configuring Nginx and Puma for your Ruby on Rails app on Ubuntu. Let’s dive into the steps required to get your Rails application live. This comprehensive guide will help you navigate the nuances of deploying a Ruby on Rails application on Ubuntu.

Prerequisites

Before diving into the deployment process, ensure you have the following prerequisites in place:

  • A server running Ubuntu 22.04
  • A user with sudo privileges
  • Basic knowledge of Ruby on Rails
  • Ruby installed on your system
  • Rails application ready for deployment.

Step 1: Install Required Packages

Begin by updating your system and installing the necessary packages.

sudo apt update && sudo apt install -y git nodejs yarn

This command updates the system package list and installs Git for version control, Node.js for JavaScript runtime, and Yarn for managing JavaScript dependencies. With these tools in place, you’re ready to proceed to the next step.

Step 2: Install RVM and Ruby

RVM (Ruby Version Manager) allows you to manage multiple versions of Ruby easily. Install it alongside Ruby.

gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable --ruby
source ~/.rvm/scripts/rvm
rvm install 3.1.2

These commands import GPG keys, install RVM, source the RVM script, and finally install Ruby version 3.1.2. With Ruby installed, you’re ready to set up your database.

Step 3: Set Up PostgreSQL

PostgreSQL is a powerful database system commonly used with Ruby on Rails applications. Install and configure it next.

sudo apt install -y postgresql postgresql-contrib libpq-dev
sudo -u postgres createuser --interactive

The first command installs PostgreSQL along with necessary libraries, while the second command helps you set up a new PostgreSQL user. With your database ready, you can proceed to configure your web server.

Step 4: Configure Nginx

Nginx will serve as our web server to handle HTTP requests and proxy them to Puma. Install and configure it accordingly.

sudo apt install -y nginx
sudo nano /etc/nginx/sites-available/myapp

This command installs Nginx and opens a configuration file where you’ll set up your site configuration for the Ruby on Rails app. Once Nginx is configured, it’s time to set up the application server.

Step 5: Set Up Puma

Puma is an application server that will run your Rails application. You’ll need to configure it to work with Nginx.

bundle exec puma -C config/puma.rb

Running this command starts Puma with the configuration specified in config/puma.rb, ensuring it listens for requests from Nginx. With Puma running, you are ready to deploy your application.

Step 6: Deploy Your Application

With everything configured, it’s time to deploy your Rails application using Capistrano or manually copying files if preferred.

cap production deploy

This command uses Capistrano to automate deployment tasks, such as pushing code changes to your server seamlessly. If you encounter any issues, refer to the troubleshooting section.

Troubleshooting.

Common Issue 1

If you encounter “Permission Denied” errors when accessing files, ensure that correct file permissions are set for the user running Nginx and Puma. Check logs in /var/log/nginx/error.log for detailed error messages.

Best Practices

  • Regularly update your server packages.
  • Use environment variables for sensitive data.
  • Monitor application performance after deployment.

Conclusion

Deploying a Ruby on Rails application on Ubuntu 22.04 using Nginx and Puma is straightforward when following these steps. By ensuring all prerequisites are met and configurations are correctly set up, you’ll have a secure and scalable environment for your app. Remember to keep your server updated and monitor its performance regularly to maintain a healthy production setup.


Like it? Share with your friends!

0

What's Your Reaction?

hate hate
0
hate
confused confused
0
confused
fail fail
0
fail
fun fun
0
fun
geeky geeky
0
geeky
love love
0
love
lol lol
0
lol
omg omg
0
omg
win win
0
win
Anoop Patel