Application Configuration
Git
Download your Rails application to your home directory:
git clone <URL>In the future, simply download the new commits from the remote repository:
git pullRails
Make sure you create config/master.key and copy the file contents from your local machine.
Prepare the application for running in production using the following commands:
RAILS_ENV=production bundle installRAILS_ENV=production rails assets:precompileRAILS_ENV=production rails db:createRAILS_ENV=production rails db:migrateRAILS_ENV=production rails db:seedIn the future, you should skip the database creation and seed steps.
Nginx
Using the template below, create a new Nginx configuration file:
sudo vim /etc/nginx/sites-enabled/<APPLICATION_NAME>server {
server_name <DOMAIN_NAME>;
listen 80;
root /home/<USERNAME>/<APPLICATION_FOLDER>/public;
try_files /maintenance.html $uri $uri/index.html $uri.html @app;
location ~ /.well-known {
allow all;
}
location @app {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
proxy_pass http://localhost:3000;
}
}Restart Nginx:
sudo service nginx restartSystemd
Using the template below, create a new Systemd initialisation file:
sudo vim /etc/systemd/system/<APPLICATION_NAME>.service[Unit]
Description=<APPLICATION_NAME>
Requires=network.target
[Service]
Type=simple
User=<USERNAME>
Group=<USERNAME>
WorkingDirectory=/home/<USERNAME>/<APPLICATION_FOLDER>/
Environment=PATH=/home/<USERNAME>/.rbenv/plugins/ruby-build/bin:/home/<USERNAME>/.rbenv/shims:/home/<USERNAME>/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
ExecStart=/bin/bash -lc 'bundle exec puma -e production -b 0.0.0.0 -p <PORT>'
TimeoutSec=30s
RestartSec=30s
Restart=always
[Install]
WantedBy=multi-user.targetEnable the service:
sudo systemctl daemon-reloadsudo systemctl enable <APPLICATION_NAME>.servicesudo service nginx restartYou can restart and check the status of the service using the following commands:
sudo systemctl restart <APPLICATION_NAME>.servicesudo systemctl status <APPLICATION_NAME>.servicesudo journalctl -xeLast updated