我如何将 example.com 绑定到 apache 上,以便我在机器上本地运行以用于开发目的?
我尝试将其添加到我的 /etc/hosts 文件中
127.0.0.1 example.com
并重新加载了 apache2,sudo /etc/init.d/apache2 reload
但是没有作用。
我已经添加了虚拟主机example.com
并启用了它a2ensite
编辑:
Apache 错误日志(据我所知没有相关内容)
[Mon Mar 28 15:21:01 2011] [notice] Apache/2.2.16 (Ubuntu) configured -- resuming normal operations
[Mon Mar 28 15:21:03 2011] [notice] Graceful restart requested, doing restart
[Mon Mar 28 15:21:03 2011] [notice] Apache/2.2.16 (Ubuntu) configured -- resuming normal operations
[Mon Mar 28 15:21:23 2011] [error] [client 127.0.0.1] File does not exist: /var/www/favicon.ico
[Mon Mar 28 15:21:23 2011] [error] [client 127.0.0.1] File does not exist: /var/www/favicon.ico
[Mon Mar 28 15:35:34 2011] [error] [client 127.0.0.1] File does not exist: /var/www/phpmyadmin
[Mon Mar 28 15:36:48 2011] [notice] caught SIGTERM, shutting down
[Mon Mar 28 15:36:49 2011] [notice] Apache/2.2.16 (Ubuntu) PHP/5.3.3-1ubuntu9.3 with Suhosin-Patch configured -- resuming normal operations
[Mon Mar 28 15:49:13 2011] [notice] Graceful restart requested, doing restart
[Mon Mar 28 15:49:13 2011] [notice] Apache/2.2.16 (Ubuntu) PHP/5.3.3-1ubuntu9.3 with Suhosin-Patch configured -- resuming normal operations
[Mon Mar 28 15:49:17 2011] [notice] caught SIGTERM, shutting down
[Mon Mar 28 15:49:18 2011] [notice] Apache/2.2.16 (Ubuntu) PHP/5.3.3-1ubuntu9.3 with Suhosin-Patch configured -- resuming normal operations
[Mon Mar 28 15:50:24 2011] [notice] Graceful restart requested, doing restart
[Mon Mar 28 15:50:24 2011] [notice] Apache/2.2.16 (Ubuntu) PHP/5.3.3-1ubuntu9.3 with Suhosin-Patch configured -- resuming normal operations
[Mon Mar 28 16:15:09 2011] [notice] Graceful restart requested, doing restart
Warning: DocumentRoot [/home/ashfame/www/example.com/] does not exist
(2)No such file or directory: apache2: could not open error log file /home/ashfame/www/example.com/error.log.
Unable to open logs
[Mon Mar 28 16:46:18 2011] [warn] pid file /var/run/apache2.pid overwritten -- Unclean shutdown of previous Apache run?
[Mon Mar 28 16:46:18 2011] [notice] Apache/2.2.16 (Ubuntu) PHP/5.3.3-1ubuntu9.3 with Suhosin-Patch configured -- resuming normal operations
[Mon Mar 28 16:46:25 2011] [error] [client 127.0.0.1] File does not exist: /var/www/example.com
[Mon Mar 28 16:46:38 2011] [error] [client 127.0.0.1] File does not exist: /var/www/www
[Mon Mar 28 16:55:48 2011] [error] [client 127.0.0.1] File does not exist: /var/www/phpbb/phpbb
[Mon Mar 28 17:17:40 2011] [notice] caught SIGTERM, shutting down
[Mon Mar 28 17:17:41 2011] [notice] Apache/2.2.16 (Ubuntu) PHP/5.3.3-1ubuntu9.3 with Suhosin-Patch configured -- resuming normal operations
[Mon Mar 28 17:19:29 2011] [error] [client 127.0.0.1] File does not exist: /var/www/example.com
我也尝试这样做,显示主机文件正在工作,但在浏览器中打开wget --spider http://example.com
http://www.iana.org/domains/example/
ashfame@ashfame-desktop:/usr/sbin$ wget --spider http://example.com
Spider mode enabled. Check if remote file exists.
--2011-03-28 17:20:57-- http://example.com/
Resolving example.com... 127.0.0.1
Connecting to example.com|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Remote file exists and could contain further links,
but recursion is disabled -- not retrieving.
虚拟主机配置文件:
# Place any notes or comments you have here
# It will make any customization easier to understand in the weeks to come
<VirtualHost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin [email protected]
ServerName www.example.com
ServerAlias example.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html
DocumentRoot /home/ashfame/www/example.com/
# Custom log file locations
LogLevel warn
ErrorLog /home/ashfame/www/example.com/error.log
CustomLog /home/ashfame/www/example.com/access.log combined
</VirtualHost>
答案1
如果 wget 可以正常工作,则表明您的服务器配置正确。很有可能,出于某种原因,您的浏览器优先从其他来源获取名称信息,而不是从 /etc/hosts 获取。
首先,检查
/etc/nsswitch.conf
并验证“文件”是否在“主机”下首先列出。其次,检查您的浏览器配置,确保您没有使用代理,并且浏览器没有独立于系统方法解析名称的单独方法(或者如果有,则配置为
/etc/hosts
在 DNS 之前使用)。第三,您可以设置一个已知在 DNS 中不存在的不同的 RFC 2606 虚拟主机(例如 mysite.example)(.example 是明确禁止的),这可能会阻止一些造成命名混淆的原因。
答案2
这是我的虚拟主机
<VirtualHost *:80>
ServerName sagar.localhost
ServerAdmin sagar@local
DocumentRoot /home/sagar/www/
<Directory /home/sagar/www>
Options Indexes FollowSymlinks MultiViews
AllowOverride All //for mod_rewrite
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/sagar_error.log
</VirtualHost>
所以基本上这是针对简单的 php 和 html 脚本的,如果您还需要 cgi,则需要添加到选项 AFAIK。所以您需要做的第一件事是创建 $HOME/www/example.com 目录。我认为规范<Directory></Directory>
很重要,但您没有。