Install Ruby with rbenv
sudo apt install -y git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev
# Install rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'eval "$(~/.rbenv/bin/rbenv init - bash)"' >> ~/.bashrc
source ~/.bashrc
# Install ruby-build
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
# Install Ruby
rbenv install 3.3.0
rbenv global 3.3.0Install Rails and Dependencies
gem install rails bundler
sudo apt install -y nginx postgresql postgresql-contrib libpq-dev nodejs npm
sudo npm install -g yarnDeploy Application
cd /var/www
git clone https://github.com/yourname/myapp.git
cd myapp
bundle install --deployment --without development test
RAILS_ENV=production rails db:create db:migrate
RAILS_ENV=production rails assets:precompilePuma Service
Create /etc/systemd/system/puma.service:
[Unit]
Description=Puma Rails Server
After=network.target
[Service]
User=deploy
WorkingDirectory=/var/www/myapp
ExecStart=/home/deploy/.rbenv/shims/bundle exec puma -C config/puma.rb
Environment=RAILS_ENV=production
Environment=SECRET_KEY_BASE=your-secret-key
Restart=on-failure
[Install]
WantedBy=multi-user.targetNginx Configuration
upstream rails {
server unix:/var/www/myapp/tmp/sockets/puma.sock;
}
server {
listen 80;
server_name example.com;
root /var/www/myapp/public;
location / {
try_files $uri @rails;
}
location @rails {
proxy_pass http://rails;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location ~ ^/(assets|packs) {
expires max;
gzip_static on;
}
}SSL
sudo certbot --nginx -d example.com