Socket activation for FastCGI with systemdPublished on Thu 26 July 2012.
The server hosting this blog runs Parabola GNU/Linux-libre with systemd. The HTTP daemon I use is nginx, it serves several domains with Python software connected to it via FastCGI (using MoinMoin as example, it handled a wiki this article was migrated from). Since nginx doesn’t start FastCGI processes on its own and I don’t want to use another daemon for this, I decided to use systemd socket activation to control the FastCGI process. (I’m not discussing the choice of software described here, most reasons I have to use it are unrelated to this issue and there are probably better choices for your use.)
I’ve configured nginx to use the /srv/www/wiki.mtjm.eu/fastcgi socket.
Mostly as documented in the manual, this unit describes the socket:
[Unit] Description=wiki.mtjm.eu [Socket] ListenStream=/srv/www/wiki.mtjm.eu/fastcgi Accept=false
I put it in /etc/systemd/system/sockets.target.wants/mtjmwiki.socket.
The Accept=false line is the reason for FastCGI: the service (mtjmwiki.service) is started on first request and used for the following requests, unlike with Accept=true that would start it separately for each request. Only the second option is supported with CGI and some older servers (it was typically done via inetd).
The service unit is similarly simple:
[Unit] Description=wiki.mtjm.eu [Service] Type=simple ExecStart=/srv/www/wiki.mtjm.eu/moin/moin.fcgi User=mtjmwiki Group=mtjmwiki StandardInput=socket
I wrote it to /etc/systemd/system/mtjmwiki.service. The /srv/www/wiki.mtjm.eu/moin/moin.fcgi script is /usr/share/moin/server/moin.cgi adapted to use my paths.
The StandardInput=socket line makes the MoinMoin process get the socket used as specified by the FastCGI protocol. (Without this line it would assume that it’s using CGI.)
Starting the socket units and accessing the wiki has shown that this worked.
Submit comments on this post to <email@example.com>.