Project

General

Profile

Actions

Systemd » History » Revision 3

« Previous | Revision 3/4 (diff) | Next »
IlyaK, 2016-12-16 11:57


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 systemd-spawn-fcgi.sh 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).

#!/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 "$@" 

Then you need to create a socket and a service file (they should have the same name apart from the extension).

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:

[Unit]
Description=WebApp Listen Socket

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

[Install]
WantedBy=sockets.target

/etc/systemd/system/webapp.service:

[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

Now enable and start them:

systemctl enable /etc/systemd/system/webapp.socket /etc/systemd/system/webapp.service
systemctl start webapp.socket webapp.service

Updated by IlyaK about 8 years ago · 3 revisions