Home > Article > Backend Development > Apache virtual host configuration and usage guide in ubuntu
How to enable and disable the site
a2ensite 站点名 a2dissite 站点名
Name-based (distinguished by domain name) virtual host
After installing apache, there is a virtual host called default by default. When creating a new virtual host, you can directly copy the configuration file of the default virtual host and modify the configuration parameters of the new virtual host based on it.
#copy /etc/apache2/site-available/default /etc/apache2/site-available/sitename
Test environment
Operating system: Ubuntu Server 12.04 LTS
Test machine address: 10.39.6.59
Test machine domain name: *.example.com
Basic configuration
We all know that if we want to run a single machine When setting multiple domain names or host names on the machine, we will use name-based virtual hosts. So how to set it up? That's what this guide aims to solve. There is the main configuration file apache2.conf for Apache2 in Ubuntu's /etc/apache2/ directory. In this file we can see the following fields:
# Include the virtual host configurations: Include /etc/apache2/sites-enabled/[^.#]*(12.04版本里无[^.#]*)
The meaning of this line indicates that the file contains the file name in the /etc/apache2/sites-enabled/ directory that does not contain the two characters "." or "#" All files. When we listed the files in this directory, we found that there was only one soft link file of 000-default. The actual connection was the default file in the /etc/apache2/sites-available directory. It is not difficult to see that the file name of the file does not contain Does not contain "." or "#". So of course this file must be included in the configuration file apache2.conf. Open the file and find that it is actually a configuration file for a virtual host. However, since the virtual host in the file is *, it is actually a general configuration file. If we want to create a virtual host, then we need to change the file to look like the following:
<VirtualHost *:80> ServerName www.firehare.com ServerAdmin admin@mail.firehare.com DocumentRoot /var/www/ <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/> 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 # Commented out for Ubuntu #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 analyze the setting statements related to the virtual host in the above settings:
`NameVirtualHost :80`: means What we want to do is a name-based virtual host, the listening port is 80.
`a2445d15a37241fe08bac1f84a382b54 and ee672f0beb03b42be69279368a66a410`: indicates the configuration of a virtual host. Note that if the port number is specified in the above field, it must also be specified here
`ServerName www.firehare.com`: Set the domain name of the virtual host, www.firehare.com can be any domain name you registered
`ServerAdmin admin@mail .firehare.com`: Set the email of the network administrator of the virtual host
`DocumentRoot /var/www/`: Set the home directory path of the virtual host
`ErrorLog /var/log/apache2/error.log`: Set The error message of the virtual host
`CustomLog /var/log/apache2/access.log combined`: Set the access information of the virtual host
In this way, we have configured a virtual host www.firehare.com. But since this is the default configuration, after Apache2 restarts, no matter you enter any domain name pointing to this host in the DNS server, you will be directed to the /var/www directory pointed to by the default configuration of www.firehare.com. Unless the domain name is used by other virtual host configurations, for example, we have also configured edunuke.firehare.com to point to this machine and configured the corresponding virtual host. In this case, entering the domain name edunuke.firehare.com will be included in the directory corresponding to the domain name. middle.
Further explanation
In order to explain clearly, let’s add another virtual host site example.com. First, create a file edunuke in the /etc/apache2/sites-available/ directory and edit the file:
<VirtualHost *:80> ServerName edunuke.example.com ServerAdmin edunuke@mail.example.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 settings is the same It’s similar to the above, so I won’t say more about it. Then run the command:
sudo a2ensite edunuke
In this case, the virtual host site edunuke.example.com has been installed. At this time, you can also find an additional soft link to /etc/apache2/sites-available/edunuke in the /etc/apache2/sites-enabled/ directory. The next step is to restart Apache2 to make the virtual host site run:
sudo /etc/init.d/apache2 restart 这里可以使用reload 重新加载
In this way, if you enter edunuke.example.com on the browser, it will be pointed to the /var/www/edunuke directory, and other input will point to the local machine The domain name will point to the /var/www directory in the default configuration. Friends who are familiar with Apache2 will ask why it is so troublesome. Isn’t it possible to put it in a file? Why use two files? It's actually very simple, because if I want to maintain the edunuke site, I only need to run the command:
sudo a2dissite edunuke sudo /etc/init.d/apache2 restart
. This way I can maintain the edunuke site without affecting the normal operation of other sites.
Advanced configuration
上面谈了一下简单的虚拟主机配置方法。这个基本上能满足我们大部分的需要。但如果要是安装 Zope+Plone 的话,上面的这点设置是远远不够的,由于 Zope+Plone 结构所采用的端口并非是80端口,所以我们还得做端口重定向。为了能够做这个,我们得激活 Rewrite 和 Proxy 两个模块。激活模块很简单,同站点配置目录一样,在 Apache2 中也有两个模块配置目录:mods-available 和 mods-enabled。在 mods-available 目录中的是所有可用的模块,而在 mods-enabled 目录中的则是已被安装到 Apache2 中的模块。由于在 mods-available 目录中已经有了 Rewrite 和 Proxy 模块的配置引导文件,所以只需要简单地将其安装到 Apache2 中即可。使用命令:
sudo a2enmod rewrite sudo a2enmod proxy
然后,添加虚拟主机站点 plone.example.com,同 edunuke 站点创建相似在/etc/apache2/sites-available/ 目录中建立一个文件 plone。显然这个文件名中是没有 "." 或 "#" 这两个字符的了。然后编辑该文件:
<VirtualHost plone.example.com:80> ServerName plone.example.com ServerAdmin plone@mail.example.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>
这样就安装好了 plone.example.com 虚拟主机站点,可以在浏览器中地址栏中输入 http://plone.example.com 就可以重定向到 Zope+Plone 站点去了。