Clone the source repository.
Run rails secret
to generate secret key and set SECRET_KEY_BASE
to the value later in ~/.profile
.
Update ~/.profile
with the following configurations
# REQUIRED Production configurations example
export RAILS_ENV=production
export SECRET_KEY_BASE=value from `rails secret`
export RAILS_SERVE_STATIC_FILES=1
The following files require root access to edit them,
/etc/systemd/system/rails.service
/etc/nginx/sites-enabled/rails
Run the following commands (as root) to restart services,
systemctl daemon-reload
systemctl restart rails.service
service nginx restart
[Unit]
+ Description=Project Name
- Description=ExampleApp
Requires=network.target
[Service]
Type=simple
User=rails
Group=rails
+ WorkingDirectory=/home/rails/project-directory/
- WorkingDirectory=/home/rails/example/
ExecStart=/bin/bash -lc 'bundle exec puma'
TimeoutSec=30s
RestartSec=30s
Restart=always
[Install]
WantedBy=multi-user.target
server {
listen 80;
+ root /home/rails/project-directory/public;
- root /home/rails/example/public;
server_name _;
index index.htm index.html;
location ~ /.well-known {
allow all;
}
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
May need to run rvm install "ruby-x.x.x"
if ruby version is different.
We need to update the bundler with bundle update --bundler
if we change
the ruby version.
Then run bundle install
and followed by yarn install --check-files
.
Run rails db:setup && rails db:migrate
bin/webpack
rails assets:precompile
On production, errors are not visible. We need to read the log files to trace errors.
# Rails log
~/project-directory/log/production.log
# Nginx log
/var/log/nginx/error.log
On production, relative path to assets does not work unless they
reside in /public/assets
directory. We need to use asset_path
or asset_url
helpers to generate path or url (with fingerprints)
to the assets.
TQuyw