Capistrano
On this page
- Change Database User Password
- Define the Environment Variable
- Database Configuration
- Capistrano Related Gems
- Capify Project
- Configure Capfile
- Configure Deploy
- Define Custom Tasks
- Establish SSH Connection
- Configure Allowed Hosts
- Run Deploy Command
- Create Inventory File
- Run Puma Playbook
- Map IP to domain
- Run SSL Playbook
- Verify Running App
Change Database User Password
To change the database password, run the ansible playbook provided in Change Database Password.
Define the Environment Variable
The new-password
is your new password used in the previous step.
Database Configuration
The config/database.yml file will pickup the password from this environment variable:
The database user is deploy. The Packer built the image with the deploy database user has create database permission. When Capistrano runs as deploy user, it will be able to run rails db:create
. Do not change the username for production. You can change the default password for the deploy database user as explained above.
Capistrano Related Gems
Add Capistrano related gems to the development group in the Gemfile:
I am not using the capistrano3-puma gem because it is not compatible with Puma 6.4.2. The gem has issues due to breaking changes related to daemonizing the Puma process. Instead, we will be using a Puma playbook for running and restarting Puma.
The config/deploy.rb in the demo Rails project has Puma restart capistrano task that will automatically restart on deploy.
Capify Project
Run bundle install
. You can now capify your Rails project by running:
Configure Capfile
The Capfile contents:
Configure Deploy
Add these lines to config/deploy.rb:
- Replace 54.188.245.219 with your Rails server static IP address.
- Change
capt
for application to your application name. - Change the repo_url to your project repo url.
- Replace
your-app-name
in deploy_to to your Rails app name.
Do not change /home/deploy/apps/
. The /home/deploy/apps
directory is used because the prebuilt Packer image has already created it with the proper permissions for the deploy user. This allows Capistrano to create all the necessary directories under your Rails app name folder. Otherwise Capistrano deploy will fail due to directory permission issues.
Define Custom Tasks
Copy the following to the end of config/deploy.rb:
The first time deploy creates the production database. We also print the environment variables to verify that they are set correctly on the server by Capistrano. Otherwise, you will run into database connection issues due to missing database password. We also upload the master.key and database.yml that will be retained across deployments. Capistrano will automatically run migrations when code is deployed.
Establish SSH Connection
Copy ~/.ssh/id_ed25519.pub key file on your development machine to the Rails server ~/.ssh/authorized_keys.
Copy the output and save it on the server:
Now you can test the SSH connection on the development machine by running:
Replace the IP address with your EC2 static IP address. By default the Packer image uses the port 2222 for SSH connection. If this works, the key is setup correctly and Capistrano will be able to deploy your Rails app.
Configure Allowed Hosts
In config/environments/production.rb, add:
Replace clickplan.net
with your domain name.
Run Deploy Command
On the development machine, run:
Create Inventory File
In the hivegrid.dev project, create inventory.ini
inside ansible folder:
- Replace the IP address with the static IP of your Rails EC2 instance.
- Replace the path to pem file that was downloaded using keyDownload.js.
Run Puma Playbook
Run puma playbook in the ansible/playbooks/deploy directory. Replace the capt
with the name of your app in the command line. In the ansible directory, run:
Map IP to domain
This is covered in Mapping IP to Domain
Run SSL Playbook
Run ssl playbook in the ansible/playbooks/deploy directory.
Verify Running App
Browse to the health check endpoint: https://clickplan.net/up
. It should display a green background.
The restart-puma playbook in the ansible/playbooks/deploy directory is handy if you make any changes to Puma config.