我不敢相信我以前没有做过,但是我想得到一个明确的答案,所以我已经准备好了。
我有一个Apache的配置文件/etc/apache2/sites-available/mysite,看起来像这样:
/etc/apache2/sites-available/mysite
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /home/sam/public_html <Directory /> Options FollowSymLinks AllowOverride All </Directory> <Directory /home/sam/public_html> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> 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>
因此,这可以~/public_html很好地处理html和php文件。但是我在那里有多个项目,因此想开始使用子域。我想要做的是将文件从~/public_html/myproject/用作根目录myproject.localhost。
~/public_html
~/public_html/myproject/
myproject.localhost
我尝试将以下内容添加到我的apache文件的底部:
<VirtualHost myproject.localhost> DocumentRoot ~/public_html/myproject/ ServerName myproject.localhost ServerAdmin admin@myproject.localhost <Directory ~/public_html/myproject> Options Indexes FollowSymLinks AllowOverride FileInfo Order allow,deny Allow from all </Directory> </VirtualHost>
但apache抱怨:
Restarting web server: apache2[Tue Aug 20 11:06:19 2013] [error] (EAI 2)Name or service not known: Could not resolve host name myproject.localhost -- ignoring! ... waiting [Tue Aug 20 11:06:20 2013] [error] (EAI 2)Name or service not known: Could not resolve host name myproject.localhost -- ignoring!
我知道我犯了一个基本错误,但是我不确定这是什么。
现在是我完整的文件:
<VirtualHost *:80> DocumentRoot /home/sam/public_html/ryua1226-magento/ ServerName mydomain.localhost ServerAdmin admin@mydomain.localhost <Directory /home/sam/public_html/ryua1226-magento> Options Indexes FollowSymLinks AllowOverride FileInfo Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /home/sam/public_html <Directory /> Options FollowSymLinks AllowOverride All </Directory> <Directory /home/sam/public_html> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> 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>
您在<VirtualHost>标签内告诉Apache您想回答的IP和端口,所以这里*表示任何IP,但在端口80上接受此站点的请求。接下来,您需要告诉Apache文档根目录在哪里。~/表示您的默认主目录,因此,如果您DocumentRoot恰好是默认home变量,则它将与您现有的符号一起使用(取决于您以哪个用户身份运行服务器)。然后,您将声明服务器名称。
<VirtualHost>
~/
DocumentRoot
home
您要为其创建主机的每个域名都需要其自己的虚拟主机指令,除非您使用别名。
<VirtualHost *:80> DocumentRoot /home/sam/public_html ServerName myproject.localhost # Other directives here </VirtualHost> <VirtualHost *:80> DocumentRoot /home/sam/public_html/myproject ServerName myotherproject.localhost # Other directives here </VirtualHost>
关于主机 除此以外,您为主机创建的任何特殊名称都必须进入主机文件或DNS服务器中。这样,任何正在寻找服务器的Web浏览器都可以找到它,而无需键入IP。如果您尝试仅使用IP访问服务器,则可能会在设置中将多个主机置于同一IP上,因此,您只会获得第一个响应IP的主机(通常是vhosts列表中的顶部) 。