Configuring Apache2 Virtual Hosts on Debian Linux 7.0
For cases where there are multiple domain names but only one host, you can use Apache 2 virtual hosts to solve the problem of multiple domain names not mapping.
For cases where there are multiple domain names but only one host, you can use Apache 2 virtual hosts to solve the problem of multiple domain names not mapping. For me, virtual hosts not only solve the multiple-domain-name issue, but also have a domain name forwarding feature. Normally, if the host serves as a mail server, after DNS resolution, "imap.xxx.com" or "pop3.xxx.com" can also access the host (or with other settings, this can be avoided). With Apache 2 virtual hosts, this situation can be effectively avoided.
Environment Debian Linux 7.0 amd64 Domain names: abc.com, def.com, xyz.com The above abc.com and def.com have already been pointed to the server's IP.
Goals
Use abc.com and def.com to access different directories on the server, e.g., www.abc.com accesses /var/www/abc, www.def.com accesses /var/www/def.
Accessing xyz.com will redirect to abc.com.
Steps
Install Apache 2.
# apt-get update && apt-get install apache2
- Modify the configuration file
(1) Under /etc/apache2/sites-enabled, copy the default 000-default to create configuration files for three domain names. As other netizens said, for convenience, the configuration files are named after the domain names. After this, the directory contains four files: 000-default, abc.com, def.com, xyz.com.
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</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 ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
(2) Edit the configuration files for abc.com, def.com, and xyz.com respectively. On the second line, that is, just below "ServerAdmin webmaster@localhost", add the following:
ServerName www.XXX.com
XXX represents your domain name, e.g. abc, def, or xyz
(3) Modify abc.com: in the 4th line DocumentRoot /var/www and the 9th line <Directory /var/www/>, respectively change the corresponding content to /var/www/abc and /var/www/abc/. Make similar modifications to def.com.
(4) Modify xyz.com: at the end of the file, before </VirtualHost>, add the line Redirect / http://www.abc.com.
(5) Go to /etc/apache2/sites-available and create symlinks for the three files above, e.g.: ln -s ../sites-enabled/abc.com ./abc.com
At this point, the virtual host configuration is complete. Add content to the corresponding directories and restart the apache2 service to achieve the above purpose.
评论Comments
加载中…Loading…
留下评论Leave a comment