This is an automated script to help you setup new server and deploy Rails application along with database dump.
Ubuntu 16.04 or newer
- Execute within your rails app directory:
git clone [email protected]:Freika/rails_server.git server
- Create file
server/hosts
and set values as shown in an example below. User and database passwords should be encrypted. - Place your postgres database dump at
server/app_name.sql
. Dump must be created with following command:pg_dump --no-owner app_name > app_name.sql -U DB_USERNAME
- Update IP address of your server in
config/deploy/production.rb
and set user value todeploy
. - Install roles from ansible-galaxy:
ansible-galaxy install -r server/requirements.yml
Create file hosts
in root directory of ansible script. Here is an example:
[rails]
# Your server IP address
IP_ADDRESS
[rails:vars]
# Your application name
app_name=APP_NAME
# Your app domain (for nginx configuration)
app_domain=example.com
# Password for user deploy, encrypted in md5
user_password=PASSWORD
# Enable or disable monit installation
monit_enabled=true
# Username for monit
monit_user=MONIT_USERNAME
# Password for monit, plain text
monit_password=MONIT_PASSWORD
# Allowed IP for monit sign in
monit_allowed_ip=0.0.0.0
# Ruby version
ruby_version=2.3.3
# Make it false for fresh deploy
restore_backup=true
# Your app database password (for database.yml)
postgres_password=00000000
backup_to_aws=true
aws_key=AWS_KEY
aws_key_secret=AWS_SECRET_KEY
aws_region=eu-west-1
aws_bucket_name=YOUR_BUCKET_NAME
slack_webhook_url=https://hooks.slack.com/services/SOME_GENERATED_VALUE
# For local testing
;ansible_ssh_user=vagrant
;ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key
IMPORTANT
After first deploy and restore database from backup don't forget to change restore_backup
value to false
. In other case on next applying app.yml
playbook your database will be rewrited with provided in server
directory.
Run
ansible-playbook server/python.yml server/server.yml
cap production deploy:check
ansible-playbook server/app.yml -i server/hosts
This script goes through full server configuration process. For now it does next things (in order of applying):
- Installs Python2 in order to allow Ansible do all the work
- Installs pip, python packet manager
- Creates
deploy
user which is our main user for application deployments - Configures
zsh
andoh-my-zsh
for bothroot
anddeploy
users - Installs
vim
andhtop
- Installs
monit
for server state monitoring and notifications (RAM, HDD, etc) - Installs
nginx
withpassenger
to serve your Rails applications - Installs
Redis
- Installs
Ruby
- Installs
PostgreSQL
- Configures Nginx to server your Rails application
- Creates PostgreSQL database and user for your Rails application
- Uploads database dump and restores it on server
- Setups daily DB backups with uploading to Yandex Disk
Run cap production deploy
to start deployment process.
Just run ansible-playbook server/app.yml -i server/hosts
. You'll also want to update files from Preparation as appropriate.
Install Vagrant, run vagrant up
and then provision it as if it's just your production server.
- Setup Fail2ban