将 ubuntu 更新到 13.10 后,我很难设置虚拟主机! 这是我试过的:
启动终端并输入:
sudo a2enmod vhost_alias
如果您没有收到任何错误消息并且您的返回结果如下所示,那么您的方向是正确的。
Enabling module vhost_alias.
Run '/etc/init.d/apache2 restart' to activate new configuration!
接下来要做的是通过键入转到 sites-available 目录
cd /etc/apache2/sites-available/
好的,现在我们在 apaches 目录中,虚拟主机的所有定义文件都在这里。我们想复制一个默认模板,隐秘地命名为 default
sudo cp default our-test-site
这将创建一个名为 our-test-site 的默认模板的副本(您当然应该用您想要的任何内容替换它)。让我们编辑它,输入
sudo gedit our-test-site
这将在编辑器中打开文件,下面是默认 vhost 文件的内容(如果您进行了一些自定义,则与通常的 YMMV 一样)
ServerAdmin webmaster@localhost
DocumentRoot /var/www
Options FollowSymLinks
AllowOverride None
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
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
Alias /doc/ "/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
我们需要添加一行并编辑两行。
在 DocumentRoot 指令上方(第 4 行之前)添加 ServerName our-test-site.local。
在第 4 行编辑 DocumentRoot/var/www 路径并将其设置为/path-to-the-test-site-WITHOUT-trailing-slash。它应该看起来像这样
DocumentRoot /path-to-the-test-site-WITHOUT-trailing-slash
如果您没有注意到我的微妙提示,路径末尾不应有尾部斜杠。
编辑第 9 行的路径并将其设置为/path-to-the-test-site-WITH-trailing-slash/。它应该看起来像这样
DocumentRoot /path-to-the-test-site-WITHOUT-trailing-slash
如果您没有注意到我的微妙提示,路径末尾应该有一个斜杠。
到此为止,几乎完成,虚拟主机文件已设置。通过键入启用它
sudo a2ensite our-test-site
响应应该是这样的
Enabling site our-test-site.
Run '/etc/init.d/apache2 reload' to activate new configuration!
此时虚拟主机设置已经完成,剩下的就是告诉服务器 our-test-site.local 应该重新定位到 127.0.0.1。我们通过输入
sudo gedit /etc/hosts
并在本地主机(第 1 行)之后添加 127.0.0.1 our-test-site.local。
整个hosts文件应该是这样的
127.0.0.1 localhost
127.0.0.1 our-test-site.local
127.0.1.1 ubuntu-vm
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
保存,关闭编辑器,最后输入
sudo /etc/init.d/apache2 restart
或
sudo apache2ctl restart
好了,您的虚拟主机已设置,打开浏览器并输入 http://our-test-site.local并享受。
更新:如果您在访问 localhost 的内容时遇到问题,您应该将 ServerName localhost 添加到您的默认虚拟主机中(如上文针对新虚拟主机所述)。然后禁用和启用站点,并重新启动apache
sudo a2dissite default
sudo a2ensite default
sudo /etc/init.d/apache2 restart
更新 2:在您的新虚拟主机文件中,您应该更改您的
AllowOverride None
到
AllowOverride All
对于您的前两个目录节点(/和带有您站点路径的那个)。这将允许所有 .htaccess 文件正常工作并允许重定向。
当然不要忘记
sudo a2dissite our-test-site
sudo a2ensite our-test-site
sudo /etc/init.d/apache2 restart
最佳答案
问题在于,对于 Apache 2.4/ubuntu 13.10 及更高版本,站点可用文件类似于 name.conf
而不是
sudo cp default our-test-siteof
我是这样做的
sudo cp default.conf our-test-site.conf
等等。
关于apache - 如何为 Apache 2.4/ubuntu 13.10 及以上版本设置虚拟主机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20969833/