How to Run Rails App Server with Systemd and Ansible

Create a systemd service to run your rails app server. 

Ansible tasks to create the service:

---

...snip...

  vars:
    rails_root: "/myapp"
    rails_user: "webuser"

  tasks:
    - name: Setup Rails Web Service
      template:
        dest: /usr/lib/systemd/system/rails-web.service
        src: templates/rails-web.systemd.j2

    - name: Enable Rails Web Service
      systemd:
        name: rails-web
        daemon_reload: yes
        enabled: yes
        masked: no


The ansible template "rails-web.systemd.j2":

[Unit]
Description=Rails Web

[Service]
Type=simple
SyslogIdentifier=rails-web
User={{ rails_user }}
PIDFile={{ rails_root }}/tmp/pids/web.pid
WorkingDirectory={{ rails_root }}
ExecStart=/bin/bash -l -c "{{ rails_root }}/bin/rails s -b 0.0.0.0 -p 3000"
ExecReload=/bin/kill -s USR2 $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target


But you can also do this manually by creating the file `/usr/lib/systemd/system/rails-web.service` and then runninh `systemctl daemon-reload` to let systemd know about your service. Now you can start it up with systemctl start rails-web!

Did you like this post? Let me know by sending me a message. Is there a topic you would like me to cover? Let me know about that too. I look forward to hearing from you!

Let's Connect!