How to deploy Ruby on Rails Code on production server with Passenger and Apache?
In this blog, you are going to learn about the deployment of rails application on the production server with its configuration setting in Apache. For this, you need to have Ruby, Rails, MySQL installed in your server system.
Let’s begin with the process of deploying the Rails application.
Step 1: Add the code copy to the server.
For adding code to the server, you need to take a copy of the code on your server by login using sftp.
$ sftp firstname.lastname@example.org
$ put samplecode
Step 2: Installation of project and configuration settings.
Install all libraries of the project and make configuration settings including configuration of database.yml and secrets.yml by using following commands. For this, you need to login to the server using ssh and go to the project folder.
$ ssh email@example.com
$ cd samplecode
Rails app has ruby versions and you have to use that specific version to run the application. So, use the ruby version of the application.
$ rvm use (ruby version)
Install all libraries of the project, set the database and compile assets. Rails have some specific commands to do that.
$ bundle install
$ rake db:create RAILS_ENV=production
$ rake db:migrate RAILS_ENV=production
$ rake assets:precompile RAILS_ENV=production
Rails provide a secret key which you have to mention it into the config/secret.yml file of the application to maintain the security.
$ rake secret
Above command provide a secret key, add it to the config/secrets.yml file of you RoR project.
Step 3: Apache settings
You have to do apache settings to run the application in production mode on apache server. For this first, you have to create samplecode.conf file in etc/apache2/sites-available/ path.
The file content will look like as in Image 1. Where you have to mention the domain of your application like www.myapp.com and server name like myapp.com. With this, you also have to mention the path of a public folder of the application and ruby version of the application.
Image 1: Samplecode.conf File
You need to create the symlink of the file samplecode.conf to the sites-enabled folder. It is created for apache and virtual host settings. This virtual host setting will tell Apache where your application is located. Replace myapp.com with your application name and as well as mention path to ruby with the ruby version of the application into the samplecode.conf file.
If you start the application on some port and not on some domain like www.myapp.com, you need to specify the port number in the conf file in place of port “80” at the top of the file. And then mention that port to the ports.conf file which is located at /etc/apache/ports.conf.
Image 2: Ports.conf File
In the above image, you can see how to mention port, like I have mentioned port 3000 to be heard by apache while serving the page. At last restart the apache server by using the following command. It will run the application on the link like 127.0.0.0:3000 or if it is for a domain then it will run on www.myapp.com or myapp.com.
$ sudo restart apache2 restart
In this way, you can start a rails app on the production server.