Summary In this chapter, you learned how to develop command-line PHP scripts that can be run via a cron job to automate user reminders, to monitor system resources, and to protect your POP3 user mailboxes from SPAM and other unwanted e-mail. 606 Part IV: Using PHP for Sysadmin Tasks 21 549669 ch16.qxd 4/4/03 9:27 AM Page 606 Chapter 17 Apache Virtual Host Maker IN THIS CHAPTER ◆ Learning the basics of an Apache Virtual Host ◆ Developing an Apache Virtual Host Maker ◆ Configuring Apache for virtual hosts ◆ Deploying virtual hosts using an Apache Virtual Host Maker APACHE IS THE MOST POPULAR Web server in the world. Approximately 60 percent of the Web servers available on the Internet are running Apache or Apache-like (e.g., Zeus) Web servers. Not only is it Open Source software, but Apache is capable of hosting multiple Web sites on a single physical Web server system. These virtual hosts make Apache a great platform to deploy in organizations of any size. Internet service providers and large organizations often prefer Apache because they can deploy tens to hundreds of Web sites on a single system. In this chapter, you will develop a command-line PHP script to help you manage Apache virtual hosts for a Linux system. The script should work on other UNIX-like platforms as well. The following section outlines the basics of Apache virtual hosts. Understanding an Apache Virtual Host To understand how Apache virtual hosts work, first assume that you have a Linux server running the latest stable version of Apache. Furthermore, assume that your server’s IP address is 192.168.1.100 and that Apache is installed in the /usr/local/ httpd directory. 607 22 549669 ch17.qxd 4/4/03 9:27 AM Page 607 To create a virtual host called www.mynewsite.com manually, you have to edit the httpd.conf file (usually stored in the /usr/local/httpd/conf/ directory) by adding lines such as the following: NameVirtualHosts * <VirtualHost 192.168.1.100> ServerName www.mynewsite.com DocumentRoot /www/mynewsite/htdocs ErrorLog /www/mynewsite/logs/errors.log CustomLog /www/mynewsite/logs/access.log common </VirtualHost> The first line tells Apache that the name-based virtual host feature should be enabled — and should appear only once in the config file. This means that a single IP address can represent multiple Web server names, and that Apache should find a name in a specific HTTP (HOST) header from Web browser requests. The configuration enclosed in the <VirtualHost > container defines www.mynewsite.com as a virtual host. Each of these lines represents an Apache directive. For example, the ServerName directive defines the name of the virtual host. The DocumentRoot directive defines the path to the Web site’s document tree. The ErrorLog and CustomLog directives define the paths to the error and access log file, respectively. You can add many directives within a virtual host container to define the virtual hosts as needed. To add a second virtual host called www.mysecondsite.com, dupli- cate the <VirtualHost > container for the new site with any appropriate site- specific changes, as shown in the following example: NameVirtualHosts * # defines www.mynewsite.com <VirtualHost 192.168.1.100> ServerName www.mynewsite.com DocumentRoot /www/mynewsite/htdocs ErrorLog /www/mynewsite/logs/errors.log CustomLog /www/mynewsite/logs/access.log common </VirtualHost> # defines www.mysecondsite.com <VirtualHost 192.168.1.100> ServerName www. mysecondsite.com DocumentRoot /www/ mysecondsite /htdocs ErrorLog /www/ mysecondsite /logs/errors.log CustomLog /www/ mysecondsite /logs/access.log common </VirtualHost> 608 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 608 Visit www.apache.org for documentation on Apache and more directives for use with virtual hosts. After a new configuration is added to httpd.conf, the Web server needs to be restarted so that it can reload the new configuration. This is done using the follow- ing command: /usr/local/httpd/bin/apachectl restart The location of the script to control the Apache server is dependent upon the version of Linux you are running. Once restarted, the virtual Web sites become available via http://www. mynewsite.com/ and http://www.mysecondsite.com/ as long as the domains have the appropriate DNS records defined on the server. Name server configuration management is covered in Chapter 17, so this discussion will assume that the DNS configuration points virtual host names to their respective Web server hosts. Defining Configuration Tasks In most cases, adding a new virtual host to a system involves the following tasks: 1. Creating a DNS address (A) record for the new host in the name server configuration for that domain. This chapter assumes that you have the appropriate DNS configuration. Chapter 18 shows you how to manage DNS configuration using another tool. 2. Creating a virtual host configuration in the httpd.conf file. This can be complex or very simple depending on your needs. 3. Creating appropriate directory structure and permission settings so that the virtual host configuration you created in httpd.conf has a physical document tree in your Web server’s disk. Chapter 17: Apache Virtual Host Maker 609 22 549669 ch17.qxd 4/4/03 9:27 AM Page 609 4. Creating a user account to enable someone to manage Web contents via FTP or a shell. 5. Installing default access control (.htaccess) or other site-specific files, such as missing file handler (404 error handler) pages. 6. Copying default Web contents (if any) to the new site. 7. Optionally, creating a MySQL database account for the Web site. 8. Restarting the Web server to make the site accessible via the Web. 9. Testing the new site using a Web browser to ensure that it is accessible. As you can see, this is quite a list. If these tasks are undertaken manually, there are many places where an administrator could make a mistake, and spend hours to retrace steps and fix problems. Therefore, we want to build a tool (called makesite) that will enable administra- tors to perform most of these operations using a single command line such as the following: ./makesite -add \ user username \ pass password \ vhost www.example.com \ type gold \ restart \ test \ notify_email username@example.com This application is built as a command-line tool instead of a Web form for security reasons. With the correct permissions (namely 700), this script can only be run by a privileged user. If constructed as a Web form, the applica- tion would be far less secure due to it being much easier to access remotely. Despite taking various precautions to secure critical Web pages, making them accessible via the standard Web server is just a bad idea. The preceding command line will do the following: 1. Create a user account, called username, with a password. 2. Create an Apache virtual host configuration for www.example.com. 610 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 610 . as a Web form, the applica- tion would be far less secure due to it being much easier to access remotely. Despite taking various precautions to secure critical Web pages, making them accessible. and to protect your POP3 user mailboxes from SPAM and other unwanted e-mail. 606 Part IV: Using PHP for Sysadmin Tasks 21 549669 ch16.qxd 4/4/03 9:27 AM Page 606 Chapter 17 Apache Virtual Host. tens to hundreds of Web sites on a single system. In this chapter, you will develop a command-line PHP script to help you manage Apache virtual hosts for a Linux system. The script should work on