Production Deployment with rails5 -Apache - Passenger-MS SQL

This blog is to deploy rails application at disruptive cloud computing platform. example: scaleway

Changes to make at rails project

Change in config/environments/production.rb

config.assets.compile = true

Change in config/initializers/devise.rb

config.secret_key = ‘34790ca67ba55291b876053e2de5a248059345fd3994ada01bc4f16a4b272dedcffdee6ty1f9a4b3a92773462686c9ee4a0753b02efcc26d14e6ee11cb9fc9c6’

Change in config/secrets.yml

secret_key_base: 32790ca67ba55234b8760538ide5a248059345fd3994ada01bc4f16a4b272dedcffdee6c71f9a4b3a92773462686c9ee4a0753b02efcc26d14e6ee11cb9fc9c6

Configure database.yml with production

default: &default 
 adapter: postgresql 
 encoding: utf8 
 pool: 5 
 username: Username 
 password: Password
 host: localhost
production: <<: *default 
 database: production_db

Login into remote server as root user

Creating the User with root access

adduser mynewuser

First you will be prompted to enter the user password twice. do this step.

Next you’ll be prompted to enter in the user’s information. This step is not required, and pressing enter fills the field with the default information:

Adding user `mynewuser’ …
Adding new group `mynewuser’ (1001) …
Adding new user `mynewuser’ (1001) with group `mynewuser’ …
Creating home directory `/home/mynewuser’ …
Copying files from `/etc/skel’ …
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for mynewuser
Enter the new value, or press ENTER for the default
Full Name []: User
Room Number []:
Work Phone []:
Home Phone []:
Other []:

enter Y then hit enter to continue

Is the information correct? [Y/n] Y

Grant Root privileges to the User

visudo

Find the following code:

# User privilege specification
root ALL=(ALL:ALL) ALL

In this case, we’re granting root privileges to the user mynewuser. Add the following below that code:

mynewuser ALL=(ALL:ALL) ALL

Then exit and save the file with the key commands Ctrl-x, Y, enter.

If you’ve followed the instruction above correctly, then you should now have a user setup by the name of mynewuser which can use sudo to run commands as root!

Login as mynewuser or user you created

Project is placed under the path var/www/myapp/

add it to Gemfile

group :production do
 gem ‘passenger’
end

then bundle

Use this command to start the apache installation:

passenger-install-apache2-module

Passenger will display this text after Apache installs:

The Apache 2 module was successfully installed.
Please edit your Apache configuration file, and add these lines:

To finish the process, open up the Apache config and paste the three required lines into the file:

sudo nano /etc/apache2/apache2.conf

LoadModule passenger_module /home/deploy/.rvm/gems/ruby-2.4.0@myapp/gems/passenger-5.1.11/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
 PassengerRoot /home/deploy/.rvm/gems/ruby-2.4.0@myapp/gems/passenger-5.1.11
PassengerDefaultRuby /home/deploy/.rvm/gems/ruby-2.4.0@myapp/wrappers/ruby
 </IfModule>
ServerName example.com

Save and Exit.

Create a file called myapp.conf inside /etc/apache2/sites-enabled and add below lines

<VirtualHost *:80>
# domain name
 ServerName example.com
# Tell Apache and Passenger where your app’s ‘public’ directory is
 DocumentRoot /var/www/myapp/public
PassengerRuby /home/deploy/.rvm/gems/ruby-2.4.0@myapp/wrappers/ruby
# Relax Apache security settings
<Directory /var/www/myapp/public>
 Allow from all
 Options -MultiViews
# Uncomment this if you’re on Apache >= 2.4:
 #Require all granted
</Directory>
</VirtualHost>

Enabling the sites available / Enabling the New Virtual Host Files

sudo a2dissite 000-default #disabling the default site
sudo a2ensite myapp #enabling the your site
sudo service apache2 restart #restarting the apache

Apache commands

sudo service apache2 reload
sudo service apache2 start
sudo service apache2 stop