Document directory
- Apache Virtual Host Guide
For Apache, it is better to look at the official content, which is comprehensive and new. In fact, it is also true for other technologies. Besides Google, you can find the answer, if it is overwhelmed by junk information, it depends on the official wiki. Chinese characters are available, but Chinese characters are not available. For details about how to set up a VM in Apache, refer to this website. The content is as follows:
Apache Virtual Host Guide directory
[Hide]
- 1. How to enable and disable a site
- 2. Name-based (distinguished by domain names) Virtual Hosts
- 2.1 test environment
- 2.2 basic configuration
- 2.3 Further explanation
- 2.4 Advanced Configuration
- 3. Port-based VM
- 4. IP-based VM
[Edit] How to enable and disable a site
A2ensite site name a2dissite site name
[Edit] Name-based (distinguished by domain name) VM
After apache is installed, there is a default virtual host by default. When creating a VM, you can directly copy the configuration file of the default Vm and modify the configuration parameters of the new VM.
#copy /etc/apache2/site-available/default /etc/apache2/site-available/sitename
[Edit] test environment
- Operating System: Ubuntu Server 12.04 LTS
- Test Machine address: 10.39.6.59
- Test Machine Domain Name: * .example.com
[Edit] basic configuration
We all know that if we want to set multiple domain names or host names on a single machine, we need to use name-based virtual hosts. So how should we set it? This is the problem that this guide is trying to solve. In the/etc/apache2/directory of Ubuntu, there is a master configuration file apache2.conf for apache2. In this file, we can see the following fields:
# Include the virtual host deployments: Include/etc/apache2/sites-enabled/[^. #] * ([^. #] * is not included in version 12.04.)
This line indicates that the file contains all files whose names do not contain the characters "." or "#" in the/etc/apache2/sites-enabled/directory. When we list the files in this 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 not hard to see that the file name does not contain ". "or "#". Therefore, this file must be included in the configuration file apache2.conf. Open this file and find that it is actually a configuration file of the virtual host. However, because the virtual host in this file is *, it is actually a common configuration file. If we want to create a VM, we need to change the file to the following:
NameVirtualHost *:80<VirtualHost www.example.com:80>ServerName www.example.comServerAdmin admin@mail.example.comDocumentRoot /var/www/<Directory />Options FollowSymLinksAllowOverride None</Directory><Directory /var/www/>Options Indexes FollowSymLinks MultiViewsAllowOverride NoneOrder allow,denyAllow from all# This directive allows us to have apache2's default start page# in /apache2-default/, but still have / go to the right place# Commented out for Ubuntu#RedirectMatch ^/$ /apache2-default/</Directory>ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/<Directory "/usr/lib/cgi-bin">AllowOverride NoneOptions ExecCGI -MultiViews +SymLinksIfOwnerMatchOrder allow,denyAllow from all</Directory>ErrorLog /var/log/apache2/error.log# Possible values include: debug, info, notice, warn, error, crit,# alert, emerg.LogLevel warnCustomLog /var/log/apache2/access.log combinedServerSignature OnAlias /doc/ "/usr/share/doc/"<Directory "/usr/share/doc/">Options Indexes MultiViews FollowSymLinksAllowOverride NoneOrder deny,allowDeny from allAllow from 127.0.0.0/255.0.0.0 ::1/128</Directory></VirtualHost>
Next we will analyze the setting statements related to the VM in the above section:
- 'Namevirtualhost: 80': indicates a name-based virtual host, and the listening port is 80.
- '<VirtualHost www.example.com: 80> and </VirtualHost>': indicates the configuration of a virtual host. Note: If the above field specifies the port number, you must also specify
- 'Servername www.example.com ': sets the Domain Name of the VM.
- 'Serveradmin admin@mail.example.com ': sets the mail for this VM Administrator
- 'Documentroot/var/www/': sets the main directory path of the VM.
- 'Errorlog/var/log/apache2/error. log': sets error information for this VM.
- 'Customlog/var/log/apache2/access. log combined': sets the access information of the VM.
In this way, we have configured a virtual host www.firehare.com. However, since this is the default configuration, after Apache2 is restarted, no matter whether you enter any Domain Name Pointing to this host in the DNS server, will be directed to the/var/www directory pointed to by default configuration www.firehare.com. Unless this domain name is used by other VM configurations, for example, we have configured edunuke.firehare.com to point to the local host and configured the corresponding VM, enter the domain name edunuke.firehare.com and the domain name will be placed in the directory.
[Edit] further description
To make it clear, add another virtual host site example.com. First, create an edunuke file in the/etc/apache2/sites-available/directory and edit the file:
<VirtualHost edunuke.example.com:80>ServerName edunuke.example.comServerAdmin edunuke@mail.example.comDocumentRoot "/var/www/edunuke/"ErrorLog "/var/log/apache2/edunuke_errors.log"CustomLog "/var/log/apache2/edunuke_accesses.log" common </VirtualHost>
The specific meanings of the settings are similar to those above. Then run the following command:
sudo a2ensite edunuke
In this case, the virtual host site edunuke.example.com has been installed. You can also find a soft link to/etc/apache2/sites-enabled/in the/etc/apache2/sites-available/edunuke directory. Next, restart Apache2 to run the VM site:
Sudo/etc/init. d/apache2 restart here you can use reload to reload
In this way, if you enter edunuke.example.com in the browser, it will be directed to the/var/www/edunuke directory, the input of other domain names pointing to the local machine will be directed to the/var/www directory in the default configuration. If you are familiar with Apache2, you may ask why it is so troublesome. Is it okay to put it in a file? Why should I use two files? It is actually very simple, because if I want to maintain the edunuke site, I just need to run the command:
sudo a2dissite edunukesudo /etc/init.d/apache2 restart
You can maintain the edunuke site without affecting the normal operation of other sites.
[Edit] Advanced Configuration
I talked about the simple virtual host configuration method. This basically satisfies most of our needs. However, if Zope + Plone is installed, the above settings are far from enough. Because the Zope + Plone structure uses port 80 instead of port 80, We have to perform port redirection. To do this, we have to activate the Rewrite and Proxy modules. The activation module is very simple. Like the site configuration directory, Apache2 also has two modules: mod-available and mod-enabled. All available modules are available in the directory of "mod-available", while the modules that have been installed in Apache2 are in the directory of "mod-enabled. Because the Rewrite and Proxy module configuration boot files are already available in the directory of the mod-available, you just need to install them in Apache2. Run the following command:
sudo a2enmod rewritesudo a2enmod proxy
Then, add the VM site plone.example.com, and create a file plone in the/etc/apache2/sites-available/directory similar to that created by the edunuke site. Obviously, this file name does not contain the "." or "#" characters. Then edit the file:
<VirtualHost plone.example.com:80>ServerName plone.example.comServerAdmin plone@mail.example.comErrorLog "/var/log/apache2/plone_errors.log"CustomLog "/var/log/apache2/plone_accesses.log" commonRewriteEngine onRewriteRule ^/(.*) http://127.0.0.1:8081/VirtualHostBase/http/plone.firehare.com:80/plone/VirtualHostRoot/$1 [L,P]<Proxy *>Order Deny,AllowDeny from allAllow from all</Proxy></VirtualHost>
So that the plone.example.com virtual host site is installed, you can enter the http://plone.example.com in the address bar in the browser to redirect to the Zope + Plone site.
If you need other content, you can view other parts of the website.
Because Apache has been set on my server, default is used in site-available. My task is to add the local directory corresponding to the directory in the URL. You only need to change the default directory and restart the service. The specific setting steps are as follows (because it is too unfamiliar with Apache or something, if there is an error, please submit it and tell me the correct method. correct it ):
Modify the default file in the/etc/apache2/sites-available path, and add the corresponding Directory
Run the following commands in sequence
Sudo a2dissite default
Stop site
Sudo a2ensite default
Enable site
Sudo/etc/init. d/apache2 reload
Reload settings