apacheVirtualHost - juedaiyuer/researchNote GitHub Wiki

apache虚拟目录和虚拟主机

访问同一个服务器上的不同目录

虚拟主机

在apache的默认配置下,开启服务后,打开http://localhost,会显示 It works!字样。

我们可以配置hosts,

sudo vim /etc/hosts

127.0.0.1   localhost
127.0.0.1   urey.com

这时如果访问http://urey.com,也会跳转到与http://localhost 相同的页面,即我们虚拟了两个域名,让它们指向了本地的电脑,访问它们,打开的是同一个目录里的内容。

简单说就是同一台服务器可以同时处理超过一个域名(domain)。假设www.example1.net和 www.example2.net两个域名都指向同一服务器,WEB服务器又支持Virtual Hosting,那么www.example1.net和www.example2.net可以访问到同一服务器上不同的WEB空间(网站文件存放目录)。

web文档根目录

# /etc/apache2/sites-enabled/000-default
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

在apache的配置文件http.conf中设置如下信息,可以将该目录变成一个web可以放的目录

Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all

参数说明

  • Indexes 允许目录浏览(即列出此目录下所有文件名称);MultiViews 允许内容协商的多重视图。
  • 比如:客户端输入“http://10.0.0.1/icone/a”这将会显示此目录下所有 a.* 文件,并不是出错信息。All 包含了初MultiViews 之外的所有特性,若无Options 语句,默认为All
  • AllowOverride 定义对于每个目录下的 .htalless 文件中的指含类型,一般禁止使用。
  • 设置缺省的访问权限,当前设置表示允许所有客户机都能访问。比如仅允许192.168.16.0/24 这个段能访问,除了192.168.16.5/24 之外。
  • 使用方法:Oredr allow ,deny;Allow from 192.168.16.0/24;Deny from 192.168.16.5/24

层次性的配置文件

sites-available 真正的配置文件

sites- enabled 指向available文件的符号连接

配置多个虚拟主机,配置文件放在available里面,对于虚拟主机的停用,启动非常方便

apache工具集

  • a2ensite apache2启动时自动读取这个文件的配置
  • a2dissite 撤销软链接

增加虚拟主机

<VirtualHost *自定义端口>
	# 在ServerName后加上你的网站名称
	ServerName www.test.com

	# 如果你想多个网站名称都取得相同的网站,可以加在ServerAlias后加上其他网站别名。
	# 别名间以空格隔开。
	ServerAlias test.com mytest.com

	# 在ServerAdmin后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。
	ServerAdmin [email protected]

	# 在DocumentRoot后加上存放网站内容的目录路径(用户的个人目录)
	DocumentRoot /home/yourpath

	<Directory /home/linyupark/public_html>
	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
	Allow from all
	</Directory>

	ErrorLog /home/linyupark/public_html/error.log
	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn
	CustomLog /home/linyupark/public_html/access.log combined
	ServerSignature On
</VirtualHost>

实践

/etc/apache2$ sudo vim apache2.conf

#<Directory /srv/>
#       Options Indexes FollowSymLinks
#       AllowOverride None
#       Require all granted
#</Directory>

# wordpress set
</Directory /home/juedaiyuer/opensource/wordpress>
	    Options Indexes FollowSymLinks
	    AllowOverride None
	    Require all granted
</Directory>


# /etc/apache2/sites-available
# a2ensite 很方便
sudo vim test.conf

<VirtualHost *:80>
	# This first-listed virtual host is also the default for *:80
	ServerName www.test.com
	ServerAlias example.com
	DocumentRoot "/home/juedaiyuer/mycode/test/phptest"
	<Directory />
	    Options Indexes FollowSymLinks Multiviews
	    AllowOverride All
	    Require all granted
	</Directory>
</VirtualHost>


#修改hosts文件(/etc/hosts)
127.0.0.1	www.test.com

这样我们访问不同的域名,就对应到了不同的主目录下面.运行成功

阿里云ECS设置虚拟主机

问题

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message

#解决方法 sudo vim /etc/apache2/apache2.conf
#Server Name
ServerName 127.0.0.1

#sudo service apache2 restart

Forbidden

You don't have permission to access /info.php on this server.

#解决方法 sudo vim /etc/apache2/apache2.conf
#修改 Require all granted
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
⚠️ **GitHub.com Fallback** ⚠️