The commands in the Application Configuration section should be run by the non-root user.
Git
Download your Rails application to your home directory:
In the future, simply download the new commits from the remote repository:
Rails
Make sure you create config/master.key
and copy the file contents from your local machine.
If your CSS is compiled using Webpacker, make sure to use the stylesheet_pack_tag
.
Prepare the application for running in production using the following commands:
RAILS_ENV=production bundle install
RAILS_ENV=production rails assets:precompile
RAILS_ENV=production rails db:create
RAILS_ENV=production rails db:migrate
RAILS_ENV=production rails db:seed
In 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 restart
Systemd
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.target
Enable the service:
sudo systemctl daemon-reload
sudo systemctl enable <APPLICATION_NAME>.service
sudo service nginx restart
You can restart and check the status of the service using the following commands:
sudo systemctl restart <APPLICATION_NAME>.service
sudo systemctl status <APPLICATION_NAME>.service