假如我现在的服务器ip是1.1.1.1,两个域名www.a.com和www.b.com。 www.a.com绑定到/var/www/a下.www.b.com绑定到/var/www/b下。用基于域名的方式配置虚拟主机。
1、将 http://www.a.com 与 http://www.b.com 的DNS解析到你的服务器IP上。 2、删除apache的默认主机配置文件。你也不希望创建2个虚拟主机后人家还能直接访问/var/www/ 吧 ; 进入 /etc/apache2/sites-enabled/ ; 删除 000-default 文件。 3、在 /etc/apache2/sites-enabled/ 目录,创建2个文件。文件名用 a.conf 和 b.conf 。
在 a.conf 里填入以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <VirtualHost *:80> ServerName www.a.com ServerAdmin webmaster@localhost DocumentRoot /var/www/a/ <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/a/> 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 LogLevel warn CustomLog ${APACHE_LOG_DIR} /access.log combined 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>
在 b.conf 里填入以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 <VirtualHost *:80> ServerName www.b.com ServerAdmin webmaster@localhost DocumentRoot /var/www/b/ <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/b/> 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 LogLevel warn CustomLog ${APACHE_LOG_DIR} /access.log combined 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>
4、在 /etc/apache2/sites_enabled/ 里面建立ln链接
1 ln -s /etc/apache2/sites-available/a.conf /etc/apache2/sites-enabled/b.conf
1 ln -s /etc/apache2/sites-available/b.conf /etc/apache2/sites-enabled/b.conf
5、重启apache
1 sudo /etc/init.d/apache2 restart
关于添加新端口的方法 在相关虚拟主机配置文件的最上方添加以下代码可以使用指定端口
1 sudo vi /etc/apache2/ports.conf
其中81为要使用的端口
添加二级子域名 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <VirtualHost *:80> ServerName www.ttwinbug.com DocumentRoot "/home/impress/var/html/www" ErrorLog "/var/log/default_asia.log" </VirtualHost> <VirtualHost *:80> ServerName css3html5.ttwinbug.com DocumentRoot "/home/impress/var/html/ch3" ErrorLog "/var/log/cs3.log" </VirtualHost> <Directory "/home/impress/var/html/www" > Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory>
由于新版的apache2设定发生很大变化, 导致虚拟机设置的时候出现403错误及 以下提示
1 Forbidden You don't have permission to access / on this server.
此时在配置文件中,如下修改即可
1 2 3 4 <Directory /wwwroot/demo> Options FollowSymLinks Indexes Require all granted </Directory>