C HAPTER 4 S ETTING U P V IRTUAL H OSTING Difﬁculty Newcomer Skills • You should have a basic understanding of the namespace syntax in Zope URLs. • You should be familiar with the Apache conﬁguration ﬁle. Problem/Task One of the most common tasks in the Zope world is to hide Zope behind the Apache web server in order to make use of all the nice features Apache provides, especially SSL encryption. Solution Apache and other Web servers are commonly connected to Zope via rewrite rules speciﬁed in virtual hosts. Zope needs to interpret these requests correctly and provide meaningful output. You might think this is easy because you just have to point to the right URL of the Zope server. But this is only half the story. What about a URL that points to another object? To handle that situation, you need to tell Zope what the true virtual hosting address is. In Zope 3 this is accomplished using a special namespace called vh, which speciﬁes the public address. Before you can start setting up a virtual hosting environment on your server, you need to do the following: 1 Zope 3 Developer’s Book 2 1. Make sure Zope 3 is running at http://localhost:8080/site/ or more gener- ically at http://destination url:port/path-to-site/. 2. Make sure Apache is running at http://www.example.com:80/ or more gener- ically at http://public url:port/ Zope 3 uses its URL namespace capability to allow virtual hosting, so that no spe- cial component or coding practice is required, which means virtual hosting is always available. Generally, namespaces are speciﬁed using ++namespace++ as one element of the URL. For the vh namespace, for example, you have ++vh++Public-URL++. The ++ at the end of the URL is speciﬁc to the vh namespace. It signals the end of the public URL. The namespace approach has the advantage that you can never lock yourself out due to misconﬁguration. Some Zope 2 virtual hosting solutions have this problem and cause unnecessary headaches. In Zope 2 you also have to add an additional object. Zope 3 does not use any service or utility for this task, which makes virtual hosting support a very core functionality. However, from an Apache 1.3 point of view, the setup of Zope 3 is very similar to that of Zope 2. In the httpd.conf ﬁle—usually found somewhere in /etc or /etc/httpd—you insert the following lines: 1 LoadModule proxy_module /path/to/apache/1.3/libproxy.so 2 3 Listen 80 4 5 NameVirtualHost *:80 6 7 <VirtualHost *:80> 8 SSLDisable 9 ServerName www.example.com 10 RewriteEngine On 11 RewriteRule ^/site(/?.*) \ 12 http://localhost:8080/site/++vh++http:www.example.com:80/site/++$1 \ 13 [P,L] 14 CustomLog /var/log/apache/example.com/access.log combined 15 ErrorLog /var/log/apache/example.com/error.log 16 </VirtualHost> In the preceding code block, note the following: Line 1: You load the module that allows rewriting and redirecting of the URL. Line 3: You set up the Apache server for the default port 80. Line 5: You declare all incoming requests on port 80 as virtual hosting sites. Lines 7–16: These are all speciﬁc conﬁguration instructions for the virtual host at port 80. C HAPTER 4 S ETTING U P V IRTUAL H OSTING 3 Line 8: This speciﬁes to not use SSL encryption for communication. You are allowing only normal HTTP connections. Note that you need this line only if SSL is enabled (that is, if mod ssl is installed). If SSL is not installed, having this line will actually cause a conﬁguration error during startup. Line 9: The virtual host is known as www.example.com to the outside world. Line 10: You turn on the Rewrite Engine, basically telling Apache that this virtual host will rewrite and redirect requests. Line 11–13: The code in these lines should really appear on one line. It de- ﬁnes the actual rewrite rule, which says “If you ﬁnd the URL after the hostname and port to begin with /site, then rewrite this URL to http://localhost: 8080/site/++vh++http:www.example.com:80/site/++ plus whatever was be- hind /site.” For example, www.example.com:80/site/hello.html is rewrit- ten to http://localhost:8080/site/++vh++http:www.example.com:80/site/ ++/hello.html. Note that the part after ++vh++ must strictly be of the form protocol : host : port / path . Even if the port is 80 (the default), you have to specify it. Line 14: You deﬁne the location of the access log. This assumes that you have write access to the logﬁle directory, and you need to make sure the directory exists before you start Apache. Line 15: You deﬁne the location of the error log. Note: The preceding conﬁguration steps have also been veriﬁed for Apache 2.0. At this point you are done setting up Apache. It’s easy, isn’t it? All you need to do now is restart Apache so that the changes in conﬁguration will take eﬀect. Nothing special needs to be conﬁgured on the Zope 3 side. Zope is actually totally unaware of the virtual hosting setup. Note that you do not have to map the URL www.example.com/site to localhost:8080/site; you can choose any location on the Zope server you like. You can now combine the preceding setup with all sorts of other Apache conﬁg- urations as well (for example, SSL). You just use port 443 instead of 80 and enable SSL. Note: One of the current problems in Zope 3 is that the XML navigation tree in the management interface does not work with virtual hosting because of the way it treats a URL.