Project

General

Profile

Systemd » History » Revision 2

Revision 1 (stbuehler, 2015-03-28 10:21) → Revision 2/4 (stbuehler, 2015-03-28 10:22)

h1. spawning with systemd instead of spawn-fcgi 

 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>