Listing 17-2 (Continued) <Directory /> <Files “*.conf”> deny from all </Files> </Directory> </VirtualHost> STD_VHOST_CONF; $output[‘config’] = $vhostConfig; $output[‘makedir’] = array( ‘SERVER_ROOT’ => $serverRoot, ‘DOCUMENT_ROOT’ => $docRoot, ‘LOG_DIR’ => $logDir); return $output; } ?> The makesite script loads this file if the account type is specified as standard. The standard template can be selected by either explicitly selecting type standard or not specifying any account type, as the standard type is set as the default in make- site.conf (DEFAULT_ACCOUNT_TYPE). Of course, you can specify any account type as the default. When the std_vhost.conf file is loaded, the makeVirtualHost() function is called from makesite, which must return an Apache virtual server con- figuration enclosed in a <VirtualHost > container. In this sample configuration, a virtual host configuration is returned. That defines the server name, using the ServerName directive; the Web document root, using the DocumentRoot directive; and the error and access logs, using the ErrorLog and CustomLog directives, respectively. It also specifies that any files with .conf extensions are not allowed for Web browsing. You can create highly cus- tomizable configurations using PHP in the makeVirtualHost() function. The sam- ple configuration is simply a basic example. To learn more about Apache 2 configurations, visit http://httpd. apache.org to review online documentation. 616 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 616 Creating the contents configuration file Listing 17-3 shows the contents configuration file, std_contents.conf, which is stored in the vhosts subdirectory pointed to by $TEMPLATE_DIR in makesite.conf. This configuration file is loaded once the new user account, the virtual host con- figuration, and the necessary directory structure have been created. The purpose of this configuration file is to enable you to install contents. Listing 17-3: vhosts/std_contents.conf <?php // Master contents for standard account function copyContentsToSite($site = null) { $MASTER_CONTENTS_DIR = “vhosts/standard/htdocs/*”; $CP_BIN = $GLOBALS[SYSTEM_INFO][cp_bin]; $CP_OPT = $GLOBALS[SYSTEM_INFO][cp_opt]; $CHOWN_BIN = $GLOBALS[SYSTEM_INFO][chown_bin]; $CHMOD_BIN = $GLOBALS[SYSTEM_INFO][chmod_bin]; $user = $site[user]; $group = $site[group]; $docRoot = $site[DOCUMENT_ROOT]; $cmd = “$CP_BIN $CP_OPT $MASTER_CONTENTS_DIR $docRoot”; echo “$cmd\n”; exec($cmd, $output, $status); $cmd = “$CHOWN_BIN -R $user:$group $docRoot”; exec($cmd, $output, $status); $cmd = “$CHMOD_BIN -R 755 $docRoot”; exec($cmd, $output, $status); return TRUE; } ?> Chapter 17: Apache Virtual Host Maker 617 22 549669 ch17.qxd 4/4/03 9:27 AM Page 617 In the sample version, once the configuration file is loaded, the copyContentsToSite() function is run by the makesite script. This function per- forms a copy operation that copies all files in vhosts/standard/htdocs/* (includ- ing subdirectories) to the newly created Web site’s document root directory. Then it sets the directory ownership and file permissions for the entire document root so that files can be both accessible by the owner of the account and read by the Apache server. Of course, you can do much more using this configuration file. For example, you can install any specific applications you want to offer users of this account type. Creating the e-mail template Listing 17-4 shows the e-mail template, which is also stored in the vhosts directory pointed to by the $TEMPLATE_DIR variable in makesite.conf. This is a simple text file that stores e-mail headers and a message body containing a set of custom tags. These tags are parsed and replaced before mail is sent out. The mail is sent to the email address specified by the notify_email=email_address command-line argument for makesite. Listing 17-4: vhosts/std_vhost.mail From: Your Friendly ISP <admin@examplep.net> Content-Type: text/html Subject: Your <%VHOST%> is now ready [Account Type: <%TYPE%>] Dear Customer, Your web site <%VHOST%> is now ready. You can access it via http://<%VHOST%> Your account information is as follows: Shell account: <%USER%> [GROUP: <%GROUP%>] Password: <%PASSWD%> Your Web site information is as follows: [ ] PHP [ ] CGI [ ] SSI Server Root: <%SERVER_ROOT%> Document Root: <%DOCUMENT_ROOT%> Log dir: <%LOG_DIR%> 618 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 618 Thanks. Account Team, Your ISP Ideally, this e-mail is sent with enough instructions for the new account owner to be able to start using the Web site account. Creating the makesite script Listing 17-5 shows the makesite script. Listing 17-5: makesite #!/usr/bin/php -q <?php require_once(‘makesite.conf’); $CMD_SHORT_OPTIONS = ‘hu:p:v:t:rtn:g:’; $CMD_LONG_OPTIONS = array(‘help’, ‘add’, ‘enable’, ‘disable’, ‘user=’, ‘group=’, ‘pass=’, ‘vhost=’, ‘type=’, ‘restart’, ‘test’, ‘notify_email=’ ); $cmd = getCommandLineOptions(Console_Getopt::getopt($GLOBALS[‘argv’], $CMD_SHORT_OPTIONS, $CMD_LONG_OPTIONS) ); $SITE_INFO = null; Continued Chapter 17: Apache Virtual Host Maker 619 22 549669 ch17.qxd 4/4/03 9:27 AM Page 619 Listing 17-5 (Continued) if (empty($cmd) || (getValue($cmd, ‘v’, ‘vhost’)) == null ) { syntax(); exit; } if (isset($cmd[‘add’])) { $request = makeAddRequest($cmd); if ($request != null) { $type = $request[type]; $account = $GLOBALS[ACCOUNT_TYPE][$type]; // See if user account already exists or not // if new, create if(! userExists($request[user]) && ! createUser($request[user], $request[passwd], $account[shell])) { echo “User $request[user] does not exist\n”; echo “User $request[user] could not be created.\n”; return FALSE; } // See if group already exists or not // If new, create if(! groupExists($request[group])) { echo “Group $request[group] does not exist\n”; return FALSE; } $addOK = addSite($request); // If site was added successfully see if we need to // restart or test if ($addOK && (isset($request[restart]) || isset($request[test]) ) ) { if (!restartApache()) { 620 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 620 . $site[group]; $docRoot = $site[DOCUMENT_ROOT]; $cmd = “$CP_BIN $CP_OPT $MASTER_CONTENTS_DIR $docRoot”; echo “$cmd
”; exec($cmd, $output, $status); $cmd = “$CHOWN_BIN -R $user:$group $docRoot”; exec($cmd,. information is as follows: [ ] PHP [ ] CGI [ ] SSI Server Root: <%SERVER_ROOT%> Document Root: <%DOCUMENT_ROOT%> Log dir: <%LOG_DIR%> 618 Part IV: Using PHP for Sysadmin Tasks 22. vhosts/standard/htdocs/* (includ- ing subdirectories) to the newly created Web site’s document root directory. Then it sets the directory ownership and file permissions for the entire document root