I have configured and deployed rails with nginx and capistrano!

Tram Ho

1: Create user deploy

Suppose you were added by a customer to a server. The first step you will have to be a dedicated user to deploy.

  • Create a file create_user.sh

After creating permissions for the .sh file

  • Decentralization is complete we will run .sh file

Ok so the user deploy has been created with the user name deploy password is 12345678 (you can change the password again) then run:

2 Config server remote

2.1 Install dependencies for ruby ​​and rails

2.2 Installing ruby ​​using rvm (ruby 2.5.3)

2.3 Installing rails and NodeJs (rails 5.2.3)

2.4 Install Mysql

You can specify the version of mysql 5.7 as below and the same with other versions.

2.5 Config nginx

Because I use unicorn, we need an nginx config file to point to unicorn

You create the file nginx_app_rails as follows

And fix that file as follows:

In the file above you pay attention to the following 3:

The server unix line must point to the unicorn.sock file of the code deploy test (the current directory)

This will configure the remote server’s domain and ip address

This will point to the code deploy that was generated when we ran the command

After all we will enable it on:

3 Config server local and add gem to config

3.1 Config unicorn

Add the following gems to the gem file

Create config/unicorn.rb and edit it as follows:

Next, we will configure staging and production

  1. Create a folder with the following format:

And then corrected as follows:

As above file we need to understand some lines as follows:

  • worker_processes: 2 # like this unicorn will run with 2 processes
  • app_directory = “…..” the current unencrypted directory is currently deploying on the generated system when we run up to cap staging deploy
  • listen “# {app_directory} /tmp/sockets/unicorn.sock”, backlog: 128 # Listening to unicorn.sock I don’t really understand this 😄
  • pid “…” points to the unicorn pid when the project runs unicorn will have a unicord pid generated
  • stderr_path and stdout_path where unicorn logs are stored
  • preload_app true # I don’t know what this is, kind of reload the app to get the configs above I guess =))
  • As for the code below, the check section when I fork the github code, check if the activerecord is defined as having not deleted the app’s pid if it exists. and finally check if the connection is successful with the DB ( ActiveRecord::Base.establish_connection )

3.2 Config capistrino

Ok, now that we have finished configuring unicorn, we will now use Capistrano to auto load the rails app’s dependencies.

We use some of the following gems:

and comment the gem 'puma' again. and then bundle

After bundle we run the following command to create the file. bundle exec cap install Will generate the following files:

Ok then they will go config: 1: config Capfile

Done config file config/deploy

Next config for production and staging in config/deploy

But things to notice in the above file are:

Ok so basically the next config is but the processing process.

4 Some other things

1: Create ssh key for user deploy

Done when ssh key we add ssh to github account

2 Create a .env file

NOTE: The ~/.bashrc file must be edited again to receive the .env file

Finally: source .env and source .bashrc to get environment variables

3 clone code from github

4 Run rails commands when clone project returns

5 Deploy

When running this command we will get some errors as follows:

  1. Not authorized for rails_app directory

  1. Database file has not been created

  1. Have not created the configscrets.yml file This file has cproduction: How to create the key screts

Those are some of the errors I encountered, but the fact that when you run uncort, there is an error in the following directory to find the error

  1. Create credentials file When an error is:

Then we need:

NOTE:

Remember to remove the master.key file and credentials before running the command

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo