如何为本地开发设置虚拟主机

mvi*_*ekc 10 virtualhost 11.04 apache2

Ubuntu 11.04 安装了 apache2 并安装了所有相关软件包。我已经尝试了大部分博客,并让 google 和其他论坛成为我最好的朋友,但我无法解决这个问题。

我需要在我的本地系统上设置一个命名的虚拟主机以进行开发。

我在其中创建了目录“vivek”/var/www并复制了默认的 index.html 并编辑了一些元素。

我添加的文件vivek.com/etc/apache2/sites-available,如下所示:

# Ensure that Apache listens on port 80
Listen 80

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.vivek.com
DocumentRoot /var/www/vivek

# Other directives here
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/vivek/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

</VirtualHost>



<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
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

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
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>
Run Code Online (Sandbox Code Playgroud)

即我添加了以下几行

# Ensure that Apache listens on port 80
Listen 80

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.vivek.com
DocumentRoot /var/www/vivek

# Other directives here
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/vivek/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

到“sites-available”文件夹中已经存在的默认文件(在编辑之前备份了默认文件)

在 /etc/hosts 中的主机文件中添加了这个

127.0.0.1 localhost
127.0.1.1 vivek-PC
127.0.0.1 www.vivek.com
Run Code Online (Sandbox Code Playgroud)

执行以下操作没有错误:

root@vivek-PC:~# a2ensite vivek.com
Enabling site vivek.com.
Run '/etc/init.d/apache2 reload' to activate new configuration!
root@vivek-PC:~# /etc/init.d/apache2 reload
* Reloading web server config apache2
Run Code Online (Sandbox Code Playgroud)

当我输入时www.vivek.com,它给了我默认的 index.html,/var/www但没有出现在已编辑的文件夹 /var/www/vivek 中。

后来,我编辑了 index.html ,/var/www但我仍然得到相同的 index.html (编辑前的默认值)。所有的 index.htmls 都已被编辑,但 Apache 似乎有一些隐藏的,当我请求时它会不断出现www.vivek.com

具有讽刺意味的是,在我重新启动后 - Apache 运行良好,但我的网站 -www.vivek.com未能显示(即使隐藏的 index.html 上帝知道在哪里!!).. 现在我的浏览器显示“无法连接”

请帮忙。一周以来我一直在尝试设置它,但没有成功。

Lek*_*eyn 6

后来,我从 /var/www 编辑了 index.html,但我仍然得到相同的 index.html(编辑前的默认值)。所有的 index.htmls 都已被编辑,但 Apache 似乎有一些隐藏的,当我请求 www.vivek.com 时会不断出现

通过阅读本文,我猜您正在查看缓存文件。不要按F5或点击刷新按钮,而是通过按Ctrl+跳过刷新缓存F5

或者,使用命令行程序curl(默认情况下未安装)。用法示例:

$ curl -i http://localhost/
HTTP/1.1 200 OK
Date: Sat, 02 Jul 2011 00:42:01 GMT
Server: Apache/2.2.17 (Ubuntu)
Last-Modified: Fri, 01 Jul 2011 04:12:49 GMT
ETag: "4507-b1-4a6fa3b114149"
Accept-Ranges: bytes
Content-Length: 177
Vary: Accept-Encoding
Content-Type: text/html

<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
Run Code Online (Sandbox Code Playgroud)

一个注意事项:我在干净的 apache 安装中执行了以下操作:

  • 将您的第一个配置文件添加到 /etc/apache2/sites-available/vivek.com
  • /etc/apache2/sites-available/default 保持原状
  • sudo a2ensite vivek.com
  • sudo /etc/init.d/apache reload

我收到和你一样的消息。但是,服务器无法启动。当停止服务器 usingsudo /etc/init.d/apache2 stop并再次启动它时sudo /etc/init.d/apache2 start,它根本拒绝启动。查看错误日志/var/log/apache2/error.log发现了一些错误:

[Sat Jul 02 00:48:09 2011] [notice] Graceful restart requested, doing restart
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Sat Jul 02 00:48:09 2011] [warn] NameVirtualHost *:80 has no VirtualHosts
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Run Code Online (Sandbox Code Playgroud)

所以端口 80 似乎正在使用中。但是如果 Apache 还没有启动,我可以通过运行sudo netstat -tpln. 我查看了配置并得出结论,Listen 80应该从您的配置文件中删除该行/etc/apache2/sites-available/vivek.com。之后,我可以再次启动服务器并使用curl,我确认服务器正确响应请求。

KISS,您的第二个 vhost 块是多余的,因为它被/etc/apache2/sites-available/default. 下一个配置文件是/etc/apache2/sites-available/vivek.com

<VirtualHost *:80>
    ServerName www.vivek.com
    DocumentRoot /var/www/vivek

    # Other directives here
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/vivek/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)