11 # SITES = [(sitename, application_list)]
12 # application_list = [application_name, (application_name, command, ...)]
13 # Here, sitename is the name of the site. This folder name should exist below ROOT_DIR and
14 # is also used below SOCKET_DIR. The site name is also translated to a user and
15 # group name by replacing dots by dashes and prepending USER_PREFIX and
18 # application_list specifies the applications to start for this site. These can
19 # be generic (when only application_name is given), in which case the command
20 # is looked up in APPLICATIONS using the application_name. For a site-specific
21 # application, command is the command that should be run. It will be prefixed
22 # with the site's root dir, if is not an absolute path.
26 ('stderr.nl', ['php']),
27 # ('stdin.nl', ['php']),
28 ('stdout.nl', ['php']),
29 # ('evolution-events.nl', ['php']), #, ('xerxes', 'applications/xerxes/manage.py runfcgi'), ('wipi', 'applications/wipi/wipi.fcgi')]),
30 # ('stdio.flexvps.nl', ['php']),
31 # ('foresightsecurity.nl', ['php']),
34 # Generic applications that can be run for any site
35 # Maps application_name to application_command. application_command will be
36 # prefixed with the site's root dir, if it is not an absolute path.
37 APPLICATIONS={"php": "/usr/bin/php-cgi"}
39 # Kill these procs before starting new ones. Only processes of these names that
40 # are run by the sites in SITES are killed. This is a bit hackish, we should
41 # really be using pidfiles...
42 KILL_PROCS=['php-cgi', 'manage.py']
44 ## ABSOLUTE path to the spawn-fcgi binary
45 SPAWNFCGI="/usr/bin/spawn-fcgi"
47 ## Dir in which to create the UNIX sockets to listen on
48 SOCKET_DIR="%s/var/fcgi" % (ROOT_DIR)
50 ## number of PHP children to spawn
53 ## maximum number of requests a single PHP process can serve before it is restarted
54 PHP_FCGI_MAX_REQUESTS=1000
56 # The user to run as, will be prefixed to the sitename
58 # The group to run as.
59 SCRIPT_GROUP="httpd-users"
60 # The group that should be able to use the sockets created
61 HTTPD_GROUP="www-data"
63 # Will be postfixed to the site's root and exported in the PHPRC variable.
66 #### END OF CONFIG ####
68 for (site, apps) in SITES:
69 site_name = re.sub('\.', '-', site)
71 ## switch to the following user / group
72 user_id = "%s%s" % (USER_PREFIX, site_name)
75 site_dir = os.path.join(ROOT_DIR, site)
76 socket_dir = os.path.join(SOCKET_DIR, site_name)
79 raise Exception("Site dir does not exist: %s" % (site_dir))
82 # Kill existing processes first
83 for procname in KILL_PROCS:
84 os.system('killall --user %s %s' % (user_id, procname))
87 if os.path.exists(socket_dir):
88 shutil.rmtree(socket_dir)
90 # Create dir for sockets. Make owning group root and set group write
91 # permissions, so the mask field in the acl will not block out anything.
92 os.makedirs(socket_dir)
93 os.chown(socket_dir, pwd.getpwnam(user_id)[2], grp.getgrnam(HTTPD_GROUP)[2])
94 #os.chmod(socket_dir, stat.S_IRWXU)
97 # Unpack app tuple or lookup app command in APPLICATIONS
98 if isinstance(app, tuple):
100 (app_name, app_command) = app
102 raise Exception("Wrong number of elements in site tuple: %s", app)
105 app_command = APPLICATIONS[app_name]
107 # Prefix with site dir if not an absolute path
108 if not os.path.isabs(app_command):
109 app_command = os.path.join(site_dir, app_command)
111 # Create socket filename
112 socket = os.path.join(socket_dir, app_name)
115 # TODO: Wrap this in env to clear up the environment
116 spawnfcgi = '%s -s "%s" -u "%s" -g "%s"' % (SPAWNFCGI, socket, user_id, SCRIPT_GROUP)
117 fcgiapp = ' -- %s' % (app_command)
119 if app_name == 'php':
120 os.environ['PHP_FCGI_MAX_REQUESTS'] = str(PHP_FCGI_MAX_REQUESTS)
121 phprc = os.path.join(site_dir, PHPRC_DIR, 'php.ini')
122 if os.path.exists(phprc):
123 #os.environ['PHPRC'] = phprc
124 fcgiapp += ' -c %s' % (phprc)
125 spawnfcgi += ' -C %s' % (PHP_FCGI_CHILDREN)
128 print spawnfcgi + fcgiapp
129 os.system(spawnfcgi + fcgiapp)
131 # Ensure www-data can write to the socket :-S
132 # Spawn-fcgi explicitely chmods the socket after creation, very
134 os.chmod(socket, stat.S_IRWXU | stat.S_IRWXG)