Project

General

Profile

Systemd » History » Revision 3

Revision 2 (stbuehler, 2015-03-28 10:22) → Revision 3/4 (IlyaK, 2016-12-16 11:57)

h1. spawning with systemd instead of spawn-fcgi 

 In modern Systemd that suports "StandardInput=socket" no "systemd-spawn-fcgi.sh" is required (See https://b.mtjm.eu/fastcgi-systemd-socket-activation.html) 


 Otherwise, get Get "systemd-spawn-fcgi.sh":https://gist.github.com/stbuehler/439a9849747279a1f0a9 and install it in @/usr/local/sbin/@ (don't forget @chmod +x /usr/local/sbin/systemd-spawn-fcgi.sh@): 

 @systemd-spawn-fcgi.sh@ makes sure you get exactly one socket, and moves the bound socket from file descriptor 3 (systemd socket passing standard) to file descriptor 0 (FastCGI standard). 

 <pre> 
 #!/bin/bash 
 
 set -e 
 
 if [ "${LISTEN_PID}" != $$ ]; then 
	 echo >&2 "file descriptors not for us, pid not matching: '${LISTEN_PID}' != '$$'" 
	 exit 255 
 fi 
 
 if [ "${LISTEN_FDS}" != "1" ]; then 
	 echo >&2 "Requires exactly one socket passed to fastcgi, got: '${LISTEN_FDS:-0}'" 
	 exit 255 
 fi 
 
 unset LISTEN_FDS 
 
 # move socket from 3 to 0 
 exec 0<&3 
 exec 3<&- 
 
 # spawn fastcgi backend 
 exec "$@" 
 </pre> 

 Then you need to create a "socket":http://www.freedesktop.org/software/systemd/man/systemd.socket.html and a "service":http://www.freedesktop.org/software/systemd/man/systemd.service.html file (they should have the same name apart from the extension). 

 h2. Example 

 Start a FastCGI application "webapp", started through @/home/webappuser/webapp/runfcgi@ (the same command you'd give spawn-fcgi), bound to socket @/run/webapp.sock@. @www-data@ is the webserver user (which needs to access the FastCGI application). 

 @/etc/systemd/system/webapp.socket@: 
 <pre> 
 [Unit] 
 Description=WebApp Listen Socket 

 [Socket] 
 SocketUser=www-data 
 SocketGroup=www-data 
 SocketMode=0600 
 ListenStream=/run/webapp.sock 

 [Install] 
 WantedBy=sockets.target 
 </pre> 

 @/etc/systemd/system/webapp.service@: 
 <pre> 
 [Unit] 
 Description=WebApp service 
 After=network.target postgresql.service mysql.service 
 Wants=postgresql.service mysql.service 

 [Service] 
 User=webappuser 
 Group=webappgroup 
 StandardOutput=null 
 StandardError=syslog 
 ExecStart=/usr/local/sbin/systemd-spawn-fcgi.sh /home/webappuser/webapp/runfcgi 

 [Install] 
 WantedBy=multi-user.target 
 </pre> 

 Now enable and start them: 
 <pre> 
 systemctl enable /etc/systemd/system/webapp.socket /etc/systemd/system/webapp.service 
 systemctl start webapp.socket webapp.service 
 </pre>