Skip to content

Instantly share code, notes, and snippets.

@proveeder
Last active January 6, 2025 16:51
Show Gist options
  • Select an option

  • Save proveeder/71fc34d50147f6a0b0d064ea74dd9cc5 to your computer and use it in GitHub Desktop.

Select an option

Save proveeder/71fc34d50147f6a0b0d064ea74dd9cc5 to your computer and use it in GitHub Desktop.
Rails capistrano deploy

This file describes an overall manual deploy instructions of the rails + capistrano application to the VPS.

Connect to server

ssh root@<ip>

Configure user

adduser deployer
visudo # add deployer under root line with same preveligies

Configure ssh

nano /etc/ssh/sshd_config # uncomment Port 20 and change port to 4321, save and exit
sudo systemctl restart ssh.service

Re-enter to check connetction and update

exit
ssh deployer@<ip> -p 4321
sudo apt update -y
sudo apt upgrade -y
sudo apt install build-essential libssl-dev zlib1g-dev libpq-dev # general dependencies
sudo apt autoremove -y

rbenv

curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash
~/.rbenv/bin/rbenv init

then add

"eval "$(/home/deployer/.rbenv/bin/rbenv init - bash)""

to ~/.bashrc

Restart shell, check rbenv command

install ruby version

rbenv install <version>
rbenv global <version>

nvm

sudo snap install node --classic

Yarn

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn
yarn config set ignore-engines true # in case node engine version errors while deployment will occurr

Postgres

install db from postgres by following official installation docs)

Create user and make it sudo user

sudo -u postgres createuser --interactive --pwprompt

Install nginx + passenger

gem install passenger
passenger-install-nginx-module

and follow all recomendations (creating swap area of 1 gb can be not enough, it would be better to take a bit more (+100mb as example)) exit (to switch back to deployer).

sudo micro /opt/nginx/conf/nginx.conf

and change config as follows:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    passenger_root /home/deployer/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/passenger-6.0.16;
    passenger_ruby /home/deployer/.rbenv/versions/3.1.2/bin/ruby;

    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen       80;
        server_name  185.220.205.236;
        root         /home/deployer/qna/current/public;
        passenger_enabled on;

        location ^~     /assets/ {
            gzip_static on;
            expires max;
            add_header Cache-Control public;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

check config

sudo /opt/nginx/sbin/nginx -t

redis

sudo apt install redis-server
sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.default
sudo service redis-server restart
sudo service redis-server status

sphinx

sudo apt install sphinxsearch -y

nginx conf paste this into /lib/systemd/system/nginx.service

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

and reload server. After that you will be able to do:

sudo systemctl status nginx
sudo systemctl start nginx
sudo systemctl stop nginx

Ignore pid errors.

Capistrano

cap <server> deploy:check
cap <server> deploy

In case db not found error occurs: go to last release, enter

RAILS_ENV=production /bin/rails db:create
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment