Recently seems to have a lot of Ubuntu brothers in Gaim asked about Apache2 virtual host settings, today is just a bit of time, so I tried to write this HowTo, for your reference.
= = Test Environment = =
* Operating system: Ubuntu 5.10
* Test machine Address: 10.39.6.59
* Test machine domain name: *.firehare.com
= = Basic Configuration = =
We all know that if we want to set more than one domain name or hostname on a single machine, we need to use the name-based virtual host. So how do you set it up? This is the HowTo to solve the problem. In the/etc/apache2/directory of Ubuntu, there is a Apache2 master configuration file apache2.conf. In this file we can see this line of content:
Code: |
include/etc/apache2/sites-enabled/[^.#]*
|
This line means that the file contains all the files in the/etc/apache2/sites-enabled/directory that contain the characters "." or "#". When we list the files of the directory, we find that there is only one 000-default soft link file, the actual connection is the default file in the/etc/apache2/sites-available directory, it is easy to see that the file name does not contain "." or "#". So this file is, of course, to be included in the config file apache2.conf. Open the file and discover that it is actually a configuration file for a virtual host, but because the virtual host in the file is *, it is actually a generic configuration file.
sudo gedit/etc/apache2/sites-available/default
If we want to set up a virtual host, then we need to change the file to look like the following:
Namevirtualhost *
<virtualhost *>
ServerAdmin Webmaster@localhost
documentroot/var/www/#这个地方改成你想要的目录
<directory/>
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory/var/www/> #这个地方改成和上面相同 (I tried not to change the same words service is not normal, do not know why)
Options Indexes followsymlinks MultiViews
AllowOverride None
Order Allow,deny
Allow from all
# This directive allows us to have apache2 ' s default start Page
# in/apache2-default/, but still have/go to the right place
#RedirectMatch ^/$/apache2-default/
</Directory>
scriptalias/cgi-bin//usr/lib/cgi-bin/
<directory "/usr/lib/cgi-bin" >
AllowOverride None
Options execcgi-multiviews +symlinksifownermatch
Order Allow,deny
Allow from all
</Directory>
Errorlog/var/log/apache2/error.log
# Possible values Include:debug, info, notice, warn, error, crit,
# Alert, Emerg.
LogLevel warn
Customlog/var/log/apache2/access.log combined
Serversignature on
alias/doc/"/usr/share/doc/"
<directory "/usr/share/doc/" >
Options Indexes multiviews FollowSymLinks
AllowOverride None
Order Deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0:: 1/128
</Directory>
</VirtualHost>
Let's take a look at the settings in the above setting that are related to the virtual host:
* Namevirtualhost 10.39.6.59: What we want to do is a virtual host based on the name, and its IP address is 10.39.6.59
* <virtualhost 10.39.6.59> and </virtualhost>: Represents the configuration of a virtual host in which
* ServerName www.firehare.com: Set the domain name of the virtual host
* ServerAdmin ubuntu.firehare@gmail.com: Set up mail for this virtual host network administrator
* documentroot/var/www/: Set the home directory path for this virtual host
* Errorlog/var/log/apache2/error.log: Set this virtual host error message
* Customlog/var/log/apache2/access.log Combined: Set access information for this virtual host
This allows us to configure a virtual host www.firehare.com. However, since this is the default configuration, after Apache2 reboot, any domain name that you enter into the DNS server that points to the host will be directed to the/var/www directory that the default configuration points to www.firehare.com. Unless the domain name is used by other virtual host configuration, for example, we also configured the edunuke.firehare.com point to the local machine, and configured the corresponding virtual host, so, the input domain name edunuke.firehare.com will be the directory of the domain name.
= = further explanation = =
To clarify that we add a virtual host site edunuke.firehare.com, first to the/etc/apache2/sites-available/directory to create a file Edunuke. Of course, there are no "." or "#" characters in this file name. Then edit the file:
Code: |
<virtualhost 10.39.6.59> ServerName edunuke.firehare.com ServerAdmin ubuntu.firehare@firehare.com DocumentRoot "/var/www/edunuke/" ErrorLog "/var/log/apache2/edunuke_errors.log" Customlog "/var/log/apache2/edunuke_accesses.log" common </VirtualHost>
|
The specific meaning of the set is similar to the above, which I will not say more. And then run the command again:
{{{
sudo a2ensite Edunuke
}}}
In this case, the virtual host site edunuke.firehare.com has been installed. You can also find one more soft link to/etc/apache2/sites-available/edunuke in the/etc/apache2/sites-enabled/directory. The next step is to restart the Apache2 to run the virtual host site:
Code: |
Sudo/etc/init.d/apache2 restart
|
If you enter edunuke.firehare.com on the browser, you will be directed to the/var/www/edunuke directory, and any other domain name that points to this computer will be referred to the/VAR/WWW directory in the default configuration. Familiar with Apache2 's friends will ask why such trouble, put in a file is not also OK. Why do you use two files? It's really simple, because if I want to maintain the Edunuke site, I just run the command:
Code: |
sudo a2dissite Edunuke Sudo/etc/init.d/apache2 restart
|
, this can maintain Edunuke this site, but also does not affect the normal operation of other sites.
= = Advanced Configuration = =
It talks about a simple virtual host configuration method. This basically meets most of our needs. However, if the installation of Zope+plone, the above setting is not enough, because the zope+plone structure of the port is not 80 port, so we have to do port redirection. To be able to do this, we have to activate the Rewrite and Proxy two modules. The activation module is simple, like the site configuration directory, and there are two module configuration directories in Apache2: Mods-available and mods-enabled. In the Mods-available directory are all available modules, and in the mods-enabled directory are the modules that have been installed into Apache2. Since the configuration boot file for the Rewrite and Proxy modules is already available in the Mods-available directory, simply install it into the Apache2. To use the command:
Code: |
sudo a2enmod rewrite sudo a2enmod proxy
|
Then, add a virtual host site plone.firehare.com, similar to Edunuke site creation to create a file Plone in the/etc/apache2/sites-available/directory. Obviously, there is no "." or "#" in this file name of the two characters. Then edit the file:
Code: |
<virtualhost 10.39.6.59> ServerName plone.firehare.com ServerAdmin ubuntu.firehare@firehare.com ErrorLog "/var/log/apache2/plone_errors.log" Customlog "/var/log/apache2/plone_accesses.log" common
Rewriteengine on Rewriterule ^/(. *) http://127.0.0.1:8081/virtualhostbase/http/plone.firehare.com:80/plone/virtualhostroot/$1 [L,P ]
<proxy *> Order Deny,allow Deny from all Allow from all </Proxy>
</VirtualHost>
|
This will install the Plone.firehare.com virtual host site, you can enter the address bar in the browser http://plone.firehare.com can be redirected to the Zope+plone site