Difference between revisions of "Pubapps Systemd Use"

From UFRC
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category: PubApps]]
 
back to [[Web_Application_Hosting]]
 
back to [[Web_Application_Hosting]]
 
=Introduction=
 
=Introduction=
If one or more of your web applications hosted on pubapps infrastructure are architected to run on the webserver you need to have two copies of the app running on every pubweb[1,2] server to account for the HA (High Availability) configuration of the pubapps infrastructure where a secondary pubweb webserver will take over if there is an issue with the main webserver. To make sure your web applications are running after an HA failover or start when a server in pubapps infrastructure is restarted we encourage you to use a systemd script, which can automate the start/stop/restart process. This help article should help you create and use such a script.
+
If your web applications hosted on the pubapps infrastructure is architected to run as a backend process that is then reverse proxied via the pubweb* webservers you will need a way to start the application when a server in the pubapps infrastructure (pubcontainer* node, individual VM, or pubweb* webservers for lightweight backends) is restarted. We encourage you to consider using a systemd in user mode, which can automate the start/stop/restart process. This help article should help you create and use such a user systemd script for a web application.
  
 
=Configuration=
 
=Configuration=
Line 11: Line 12:
  
 
=Script=
 
=Script=
Example systemd script
+
 
 +
==Python==
 +
Example generic python web application systemd script
 
<pre>
 
<pre>
 
[Unit]
 
[Unit]
Line 29: Line 32:
 
WantedBy=multi-user.target
 
WantedBy=multi-user.target
  
 +
</pre>
 +
 +
==R Shiny==
 +
<pre>
 +
[Unit]
 +
Description=MyShinyApp
 +
After=network.target
 +
StartLimitIntervalSec=0
 +
 +
[Service]
 +
Type=simple
 +
Restart=always
 +
RestartSec=5
 +
WorkingDirectory=/pubapps/myproject/shinyapp/prod/app
 +
ExecStart=nohup R --no-echo --no-restore --file=run.R > /pubapps/myproject/shinyapp/prod/logs/app.log 2>&1
 +
EnvironmentFile=/pubapps/myproject/shinyapp/prod/env.sh
 +
 +
[Install]
 +
WantedBy=multi-user.target
 
</pre>
 
</pre>
  
 
=Use=
 
=Use=
 
+
==Login linger mode==
 
* Enable user linger mode to avoid having your application be shut down when you log out:
 
* Enable user linger mode to avoid having your application be shut down when you log out:
 
  loginctl enable-linger $USER
 
  loginctl enable-linger $USER
* Manage the application with
+
==Manage the application==
 
* Initial load of the configuration to enable the service
 
* Initial load of the configuration to enable the service
 
  systemctl --user enable projprod
 
  systemctl --user enable projprod
Line 48: Line 70:
 
* Check if the service is running
 
* Check if the service is running
 
  systemctl --user status projprod
 
  systemctl --user status projprod
* Start the application on pubweb[1,2] servers.
 
  
 +
===HA Setup on pubweb servers===
 +
We recommend that systemd served apps are started on the pubcontainer? server that was listed in the support ticket.
 +
 +
We have two pubapps web/proxy servers providing a HA (High Availability) setup for serving your web application(s).  If the main webserver goes down a secondary pubweb webserver will automatically take over.
 +
 +
If your application is running directly on the webserver then you will need to have a copy of the app running on both pubweb[1,2] servers to account for the HA (High Availability) configuration.
 +
 +
=Additional Information=
 
For additional information search web for 'systemd user  mode'. E.g. [https://wiki.archlinux.org/title/Systemd/User https://wiki.archlinux.org/title/Systemd/User]
 
For additional information search web for 'systemd user  mode'. E.g. [https://wiki.archlinux.org/title/Systemd/User https://wiki.archlinux.org/title/Systemd/User]

Latest revision as of 16:34, 5 August 2024

back to Web_Application_Hosting

Introduction

If your web applications hosted on the pubapps infrastructure is architected to run as a backend process that is then reverse proxied via the pubweb* webservers you will need a way to start the application when a server in the pubapps infrastructure (pubcontainer* node, individual VM, or pubweb* webservers for lightweight backends) is restarted. We encourage you to consider using a systemd in user mode, which can automate the start/stop/restart process. This help article should help you create and use such a user systemd script for a web application.

Configuration

Systemd has two modes of operation - system-wide, which requires the service scripts to be in a system location and operated by root and a user mode. You will be using the user mode.

  • Create a user systemd directory under your /pubapps/PROJECT/ $HOME directory in pubapps
mkdir -p .config/systemd/user/
  • Add a service script for the application, e.g. ~/.config/systemd/user/projprod.service or projdev.service

Script

Python

Example generic python web application systemd script

[Unit]
Description=MyProject (Production)
After=network.target

[Service]
Type=simple

WorkingDirectory=/pubapps/myinstance/myproject/prod/
ExecStart=/pubapps/myinstance/myproject/prod/conda/bin/python3 run/runserver.py
TimeoutSec=30
RestartSec=15s
Restart=always

[Install]
WantedBy=multi-user.target

R Shiny

[Unit]
Description=MyShinyApp
After=network.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=5
WorkingDirectory=/pubapps/myproject/shinyapp/prod/app
ExecStart=nohup R --no-echo --no-restore --file=run.R > /pubapps/myproject/shinyapp/prod/logs/app.log 2>&1
EnvironmentFile=/pubapps/myproject/shinyapp/prod/env.sh

[Install]
WantedBy=multi-user.target

Use

Login linger mode

  • Enable user linger mode to avoid having your application be shut down when you log out:
loginctl enable-linger $USER

Manage the application

  • Initial load of the configuration to enable the service
systemctl --user enable projprod
  • Reload configuration (after every change of the systemd service script)
systemctl --user daemon-reload
  • Start service
systemctl --user start projprod
  • Stop service
systemctl --user stop projprod
  • Restart service
systemctl --user restart projprod
  • Check if the service is running
systemctl --user status projprod

HA Setup on pubweb servers

We recommend that systemd served apps are started on the pubcontainer? server that was listed in the support ticket.

We have two pubapps web/proxy servers providing a HA (High Availability) setup for serving your web application(s). If the main webserver goes down a secondary pubweb webserver will automatically take over.

If your application is running directly on the webserver then you will need to have a copy of the app running on both pubweb[1,2] servers to account for the HA (High Availability) configuration.

Additional Information

For additional information search web for 'systemd user mode'. E.g. https://wiki.archlinux.org/title/Systemd/User