Archive for April, 2008

640 Part IV: Using PHP for Sysadmin Tasks (Web hosting control panel)

Friday, April 25th, 2008

640 Part IV: Using PHP for Sysadmin Tasks Here is a sample output: Creating user account: mrfrog with password 12345 shell=/bin/true Creating r2d2.example.com configuration Create directories /bin/mkdir -m 0755 -p /www/r2d2.example.com /bin/chown -R mrfrog:mrfrog /www/r2d2.example.com /bin/chmod -R 0755 /www/r2d2.example.com /bin/mkdir -m 0755 -p /www/r2d2.example.com/htdocs /bin/chown -R mrfrog:mrfrog /www/r2d2.example.com/htdocs /bin/chmod -R 0755 /www/r2d2.example.com/htdocs /bin/mkdir -m 0755 -p /www/r2d2.example.com/logs /bin/chown -R mrfrog:mrfrog /www/r2d2.example.com/logs /bin/chmod -R 0755 /www/r2d2.example.com/logs Checking syntax: /usr/local/apache/conf/vhosts/r2d2.example.com Syntax OK Appending Include /usr/local/apache/conf/vhosts/r2d2.example.com in /usr/local/apache/conf/httpd.conf # # # Following line loads configuration # for the r2d2.example.com virtual host Include /usr/local/apache/conf/vhosts/r2d2.example.com Creating symbolic link using /bin/ln -s /www/r2d2.example.com /home/mrfrog/r2d2.example.com /bin/cp -r vhosts/standard/htdocs/* /www/r2d2.example.com/htdocs Sending mail to using vhosts/std_vhost.mail Summary In this chapter, you learned how to develop a command-line PHP script that helps you manage Apache virtual hosts on your Linux system. Using this script creating, changing, and removing virtual servers becomes almost easy. Remember to visit www.apache.orgfor documentation on Apache and more directives for use with virtual hosts.

Ecommerce web host - Chapter 17: Apache Virtual Host Maker 639 deny

Thursday, April 24th, 2008

Chapter 17: Apache Virtual Host Maker 639 deny from all The httpd.conf file is appended with the following lines: # # # Following line loads configuration # for the r2d2.exampleexample.com virtual host Include /usr/local/apache/conf/vhosts/r2d2.exampleexample.com When the Apache server restarts, it loads the virtual host configuration for r2d2.exampleexample.com host from /usr/local/apache/conf/vhosts/r2d2.exampleexample.com. This ensures that your httpd.conf file is not cluttered with numerous virtual host configurations, as the Include directive enables you to keep the virtual host configuration outside the main httpd.conf file. You must have a NameVirtualHost directive specified in httpd.conf before any name virtual hosts configurations are added. For example: NameVirtualHost * Include /usr/local/apache/conf/vhosts/ r2d2.exampleexample.com Include /usr/local/apache/conf/vhosts/ diablo.exampleexample.com To better understand the tasks that are performed by makesite, you can set DEBUG to TRUE in makesite.conf to see what gets done. For example: ./makesite –add –user mrfrog –vhost r2d2.exampleexample.com -p 12345 - notify_email=kabir

638 Part IV: Using PHP for Sysadmin Tasks (Free web host)

Thursday, April 24th, 2008

638 Part IV: Using PHP for Sysadmin Tasks Testing makesite Once you have installed and configured makesite in a directory, you can run it as root from the script directory. For example: ./makesite –add –user mrfrog -p 12345 -v r2d2.exampleexample.com –type gold test Here, the makesite script is asked to create a virtual host configuration for a host called r2d2.exampleexample.com using the account type of gold. The script also indicates that the host will be owned by the user mrfrog, with the password 12345. Here is the sample output: Creating user account: mrfrog with password 12345 shell=/bin/tcsh Creating r2d2.exampleexample.com configuration Checking syntax: /usr/local/apache/conf/vhosts/r2d2.examplexample.com Syntax OK Appending Include /usr/local/apache/conf/vhosts/r2d2.exampleexample.com in /usr/local/apache/conf/httpd.conf Restarting Apache: /usr/local/apache/bin/httpd -k restart Writing test page: /www/r2d2.exampleexample.com/htdocs/test.txt Testing: requesting http://r2d2.exampleexample.com/test.txt …successful. Creating symbolic link using /bin/ln -s /www/r2d2.exampleexample.com ~mrfrog/r2d2.exampleexample.com The actual virtual host configuration used by Apache is stored in the conf/vhosts/r2d2.exampleexample.com file, which looks like the following: # # Automated virtual host configuration for r2d2.exampleexample.com # # Account Type: standard # ServerName r2d2.exampleexample.com DocumentRoot /www/r2d2.exampleexample.com/htdocs ErrorLog /www/r2d2.exampleexample.com/logs/errors.log CustomLog /www/r2d2.exampleexample.com/logs/access.log common

Web site optimization - Chapter 17: Apache Virtual Host Maker 637 The

Wednesday, April 23rd, 2008

Chapter 17: Apache Virtual Host Maker 637 The $PEAR_DIR must point to the directory in which you installed PEAR, or at least the Console/Getopt.php package from PEAR: $APACHE_INFO[path] = /usr/local/apache , The preceding path should point to the top directory of your Apache Web server. For example, if you have installed Apache in /home/httpd, you should change it. The makesite script assumes that your Apache directory structure is as follows: Top Apache Directory: /usr/local/apache Apache Binary Directory: /usr/local/apache/bin Apache Configuration Directory: /usr/local/apache/conf Apache Virtual Host Configuration Directory: /usr/local/apache/conf/vhosts You will have to create the virtual host configuration directory manually, as it is not standard in the Apache installer. In addition, the makesite.conf file has a few other system-dependent configurations, such as server_ip and www_partition, in $SYSTEM_INFO. You should review all the configurations in makesite.conf to ensure that the paths and values reflect your system as closely as possible. The www_partition should point to the top directory of your system, where you want to create the actual virtual host sites. For example, the sample configuration assumes /www as the www_partition, and therefore a virtual host www.example.com is created as follows: /www Site directory: /www/www.example.com Site s document root: /www/www.example.com/htdocs Site s log directory: /www/www.example.com/logs If this is now the directory structure you want to implement, you have to change www_partition to reflect the top directory, and then you have to change each account s vhost_template code to reflect your requirements. For example, the std_vhost.conf file (used for standard account) creates the following: $serverRoot = sprintf( %s/%s , $www, $server); $docRoot = sprintf( %s/%s/htdocs , $www, $server); $logDir = sprintf( %s/%s/logs , $www, $server); These can be changed to reflect your directory structure.

636 Part IV: Using PHP for Sysadmin Tasks (Web server version)

Wednesday, April 23rd, 2008

636 Part IV: Using PHP for Sysadmin Tasks 4. It determines whether the named user account in the command line already exists, using the userExists() function. If the user account does not exists, it creates the account using the createUser() function. 5. Similarly, the script determines whether the user-supplied group name exists, using the groupExists() function. Note that if the user does not supply a group name using the –group option, the username is assumed as the default group name. This is possible because in popular Linux systems such as Red Hat Linux, each user is assigned to his or her own group when the user account is created using a standard account-creation tool like useradd. If your system does not create a group corresponding to the username, you should supply the –group option with an appropriate group name. 6. Once usernames and groups are checked and/or created as needed, the script calls addSite() to create the virtual site configuration along with the actual directory structure of the site on the disk. 7. Once the configuration is created, the script checks to see if the user wants to restart Apache (–restart) or test (–test) the configuration. If either case is true, the Web server is restarted using the restartApache() function. 8. If the test option (–test) was provided in the command line, the testNewSite() function is called to test the new site. The test is performed by adding a test.txt file in the site and retrieving it as an HTTL get request. If the test.txt can be retrieved via an HTTP get request, the site is assumed to be installed and operating properly. 9. If DEFAULT_SYMLINK_USER_TO_WEBSITE is set to TRUE, a symbolic soft link is created from the user s home directory to the new Web site using the createSymLink() function. 10. Next, the addContents() function is called to optionally add any contents using the master_content template for the current account type. 1. Finally, if the –notify_email option was provided with an e-mail address, the sendMail() function is called to send an e-mail message to the given e-mail address using the mail template specified for the current account type. Installing makesite on Your System The complete makesite package is provided in the CDROM/ch17 directory in make- site.tar.gz. Extract this package on your Linux system and modify the configuration files as needed. The makesite.conf file has two path settings that are likely to differ from your system: $PEAR_DIR = /example/intranet/htdocs/pear ;

Chapter 17: Apache Virtual Host Maker 635 $search (Starting a web site)

Tuesday, April 22nd, 2008

Chapter 17: Apache Virtual Host Maker 635 $search = array(); $replace = array(); foreach ($info as $key => $value) { array_push($search, /<% . strtoupper($key) . %>/ ); array_push($replace, $value); } foreach ($request as $key => $value) { array_push($search, /<% . strtoupper($key) . %>/ ); array_push($replace, $value); } $body = preg_replace($search, $replace, $body); $subject = preg_replace($search, $replace, $subject); $headerStr = implode( , $headers); mail($to, $subject, $body, $headerStr); return TRUE; } ?> This makesite script builds on the command-line examples discussed throughout this portion of the book. The makesite script uses the two types of command-line arguments, short and long. The short arguments that are allowed are defined in the $CMD_SHORT_OPTIONS string, and the long ones are defined in $CMD_LONG_OPTIONS. These strings are needed for the Console_Getopt::getopt() function, which is available from the Console/Getopt.php class in the PEAR package. The makesite script works as follows: 1. It retrieves all the command-line arguments using the getCommandLine Options() function and stores them in the $cmd array. 2. If add is included as a command-line argument then the script performs the actual task of adding the new virtual Web site. 3. It calls the makeAddRequest() method to create an array called $request with user-supplied information such as virtual host name (supplied using the –vhost host name or the -v hostname), account type (-t account_type or –type account_type), username (–user username), password (-p password or –pass password), and so on.

Apache web server tutorial - 634 Part IV: Using PHP for Sysadmin Tasks

Tuesday, April 22nd, 2008

634 Part IV: Using PHP for Sysadmin Tasks Listing 17-5 (Continued) $headers = array(); foreach ($lines as $str) { $index++; if (preg_match( /From:s*(.+)/i , $str, $match)) { array_push($headers, From: $match[1] rn ); } else if (preg_match( /Subject:s*(.+)/i , $str, $match)) { $subject = $match[1]; } else if (preg_match( /^CC:s*(.+)/i , $str, $match)) { array_push($headers, Cc: $match[1] rn ); } else if (preg_match( /Bcc:s*(.+)/i , $str, $match)) { array_push($headers, Bcc: $match[1] rn ); } else if (preg_match( /Content-Type:s*(.+)/i , $str, $match)) { if (preg_match( /html/ , $match[1])) { array_push($headers, Content-Type: text/htmlrn ); } else { array_push($headers, Content-Type: text/plainn ); } $contentTypeSet = TRUE; } else if (preg_match( /MIME-Version:s*(.+)/i , $str, $match)) { array_push($headers, MIME-Version: $match[1] rn ); } else { array_push($message, $str); } } if (! $contentTypeSet) array_push($headers, Content-Type: text/plainrn ); $body = implode( , $message);

Chapter 17: Apache Virtual Host Maker 633 (Space web hosting) $cmd[$argName]

Monday, April 21st, 2008

Chapter 17: Apache Virtual Host Maker 633 $cmd[$argName] = ($argValue != ) ? $argValue : TRUE; } return (count($cmd) > 0) ? $cmd : null; } function addContents($site = null, $template = null) { $file = sprintf( %s/%s , $GLOBALS[TEMPLATE_DIR], $template); if (!file_exists($file)) { echo Virtual host content template $file does not exists!n ; return null; } // Load template require_once($file); $results = copyContentsToSite($site); return $results; } function sendMail($info = null, $request = null, $template = null) { $mailTemplate = sprintf( %s/%s , $GLOBALS[TEMPLATE_DIR], $template); echo Sending mail to $to using $mailTemplaten ; $to = $request[notify_email]; if (! file_exists($mailTemplate)) { echo Error: mail template $mailTemplate not found! n ; return FALSE; } $lines = file($mailTemplate); $contentTypeSet = FALSE; $message = array(); Continued

632 Part IV: Using (Make web site) PHP for Sysadmin Tasks

Monday, April 21st, 2008

632 Part IV: Using PHP for Sysadmin Tasks Listing 17-5 (Continued) -p password –pass password the password for the user account -v hostname –vhost hostname the virtual hostname -t account_type –type account_type sets type of account -r –restart restarts Apache after configuration test OK -t –test runs test to access http://hostname HELP; } function getCommandLineOptions($options) { $type = gettype($options); if (gettype($options) != array ) { // Error in command line echo $options->message n ; return null; } $cmd = array(); foreach ($options[0] as $argArray) { $argName = preg_replace( /[^w]/ , , $argArray[0]); $argValue = $argArray[1]; if ($argValue[0] == - ) { echo $argName cannot have $argValuen ; return array(); }

Chapter 17: Apache Virtual Host Maker 631 // (Net web server)

Sunday, April 20th, 2008

Chapter 17: Apache Virtual Host Maker 631 // Copy in to SITE_INFO $GLOBALS[SITE_INFO][user] = $request[user]; $GLOBALS[SITE_INFO][group] = $request[group]; $GLOBALS[SITE_INFO][vhost] = $request[vhost]; // Check to see if given vhost is a hostname if (!checkVhostName($request[vhost])) { echo Error: virtual hostname $request[vhost] is not valid.n ; return null; } if (empty($request[group])) { $request[group] = $request[user]; } return $request; } function getValue($cmd = null, $short, $long) { return (isset($cmd[$short])) ? $cmd[$short] : $cmd[$long]; } function syntax() { $script = basename($GLOBALS[ argv ][0]); echo<<