DevOps for Ruby Part 2 - Using Nginx to Host Rails Applications
In DevOps for Ruby Part 1, we set up a new VPS with all of the software needed to serve Ruby on Rails applications. In this post we will be setting up two Rails application with separate PostgreSQL databases.
Objectives
The goal of this tutorial is to:
- Create the necessary folders to host our web applications
- Use Git to sync our applications from Github onto the VPS.
- Create PostgreSQL databases for our applications and modify the
database.yml
file. - Set up Nginx to serve our application.
Initial setup
First, we must create a /var/www
folder. We can do that by ssh
-ing into our VPS. We can create the folder using:
mkdir
makes the directory and the -p
flag adds intermediate folders if they don’t exist.
GitHub setup
In order to avoid typing passwords all the time, we must generate SSH keys and tie them to our Github account. You can follow this guide to get started.
Next, lets fetch our Rails application using the git clone
command:
Verify that you have two folders within /var/www/
for your two Rails apps.
Database Setup
We are going to create a separate user for each application as it is good security practice to limit how much access a user has. Let’s start off by logging into the PostgreSQL shell
We can create users and databases for each of our applications using this syntax
Configuring your database.yml
Each Rails application comes with a database.yml
file like I described in Setup PostgreSQL for Rails on a Mac. Let’s modify this file to connect to our new PostgreSQL database.
Do the same for your secondary Rails app.
Configure Nginx
We now have to setup Nginx to allow us to serve our two applications using one server. If you followed Part 1 of this tutorial, you can find your nginx.conf in the /opt/
. Let’s open it up using vim:
Keep in mind that this configuration is suited to my two applications. Ensure that you change the paths and names to fit your application. I will describe the changes below.
The two Passenger settings that have to suit your machine are:
passenger_root
- Specifies where Passenger is
passenger_ruby
- Tells Passenger what version of Ruby to use.
We created two server settings for each application. Here is what each setting means:
As long as these domains are in your possession and you have set up the DNS to point to your VPS, you can now visit the domains and see your application.
The only caveat left is whenever you update your application, you will need to let Passenger know to reload the application. You can alleviate this in one of two ways:
- Create/Modify the
tmp/restart.txt
in your Rails application folder. - Restart Nginx manually
We are officially done with the manual work! We will be going over ways to automate this setup using tools like Capistrano and Chef in DevOps Part 3.
No comments yet. Share on Mastodon and see your comment or write a post on your blog if you support Webmentions
No reposts yet. Share on Mastodon and see your repost or write a post on your blog if you support Webmentions
No likes yet. Share on Mastodon and see your like or write a post on your blog if you support Webmentions
No bookmarks yet. Share on Mastodon and see your bookmark or write a post on your blog if you support Webmentions
Powered by Webmentions