Once usernames and groups are checked and/or created as needed, the script calls addSiteto create the virtual site configuration along with the actual directory structure of the site on
Trang 14 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 groupoption, the username is assumed
as the default group name This is possible because in popular Linux sys-tems 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 groupoption 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 per-formed 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_WEBSITEis 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 con-tents using the master_content template for the current account type
11 Finally, if the notify_emailoption 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 configu-ration 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’ ;
Trang 2The $PEAR_DIRmust point to the directory in which you installed PEAR, or at least the Console/Getopt.phppackage 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 configura-tions, 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.exam-ple.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
Trang 3Testing 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
#
<VirtualHost 192.168.0.11>
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
Trang 4<Directory />
<Files “*.conf”>
deny from all
</Files>
</Directory>
</VirtualHost>
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.example-example.com
This ensures that your httpd.conf file is not cluttered with numerous virtual host configurations, as the Includedirective enables you to keep the virtual host con-figuration 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 TRUEin makesite.conf to see what gets done For example:
./makesite add user mrfrog vhost r2d2.exampleexample.com -p 12345 notify_email=kabir
Trang 5Here 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.org for documentation on Apache and more directives for use with virtual hosts.