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


10

已安装apache2的Ubuntu 11.04和所有相关软件包。我已经尝试了大多数博客,并把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>

即我添加了以下几行

# 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>

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

在/ etc / hosts中存在的hosts文件中添加了此内容

127.0.0.1 localhost
127.0.1.1 vivek-PC
127.0.0.1 www.vivek.com

执行以下操作,没有错误:

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

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

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

具有讽刺意味的是,在我重新启动后-Apache正常运行,但是我的站点- www.vivek.com无法显示(即使index.html被隐藏了,上帝也知道了!)。现在我的浏览器显示“无法连接”

请帮忙。自一周以来,我一直在尝试进行设置,但未成功。

Answers:


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>

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

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

我收到和你一样的信息。但是,服务器无法启动。当停止使用sudo /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

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

,你的第二个虚拟主机块是多余的,因为它涵盖了/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>

1

有一个脚本可以简化您的工作-https://github.com/RoverWire/virtualhost

最终,这样做的结果与给出的答案所解释的相同,但仅通过一个命令即可完成。--

sudo virtualhost create mysite.local my_site

它还允许您删除创建的主机

sudo virtualhost delete mysite.local my_site

请注意,“ my_dir”假定它在/ var / www之后开始

因此,如果您的站点文件夹位于/ var / www / my_site中

您应该运行此命令(传递不带/ var / www的目录路径)-

sudo virtualhost create mysite.local my_site

您还可以修改脚本文件并删除默认路径“ / var / www”,以便可以将绝对路径传递到站点目录

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.