标签 famp 下的文章

经过我多次试验终于把虚拟服务器弄好了,可以实现一个IP地址访问多个网站了。

昨天刚用famp在FreeBSD虚拟机上装好了WEB服务器,但同时我又遇到了一个问题,就是我的虚拟机服务器上只能访问一个网站,因为只有一个IP地址,而且没有DNS服务器。然后我就想能不能通过一个IP访问不同的网站,经过我上网收索结果证明是可以的。就是用虚拟服务器,但是我们有DNS服务器,所以就只能用IP地址访问网站,经过我将几种方法的对比我觉得我只能通过不同的来访问不同的网站。

下面我就将我如何实现在不同的端口上运行不同的站点的方法介绍给大家:

(阅读本文章之前建议先阅读以下这篇文章:http://help.114la.com/Apache2/vhosts/examples.html

以下是我的虚拟机中FreeBSD服务器装的软件:

Apache:httpd-2.2.11.tar.bz2
Nginx:nginx-0.6.36.tar.gz
Mysql:mysql-5.1.34.tar.gz
PHP:php-5.2.9.tar.bz2
Pureftpd:pure-ftpd-1.0.21.tar.bz2
ZendOptimizer:ZendOptimizer-3.3.X
eaccelerator:eaccelerator-0.9.5.3
GD:gd-2.0.35

我所用到的方法就是基于IP的虚拟主机

在我的实现过程中出现过很多问题,下面我就将我出现的问题说一说:

在我看到的所有方法中都是在httpd.conf(/etc/local/apache/conf)中借助在NameVirtualHost指令中定义端口的方法来达到这个目的。

即在httpd.conf文件中加入

Listen 80
Listen 8080

NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080

<VirtualHost 172.20.30.40:80>
ServerName www.example.com
DocumentRoot /www/domain-80
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
ServerName www.example.com
DocumentRoot /www/domain-8080
</VirtualHost>

<VirtualHost 172.20.30.40:80>
ServerName www.example.org
DocumentRoot /www/otherdomain-80
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
ServerName www.example.org
DocumentRoot /www/otherdomain-8080
</VirtualHost>

但是经过我多次试验发现这样实现不了我的目的

于是我一点一点的把httpd.conf文件里面的内容看了一遍(因为这是配置文件嘛),看到最后一行我发现了Include conf/vhost.conf ,我一看vhost.conf不就是和httpd.conf在同一个目录下的一个文件嘛。

于是我打开vhost.conf,结果这是就是虚拟主机的配置文件,内容如下:

#————————- Mark: main           User: aidong ——————-
<VirtualHost *:80>
Serveralias aidong.org www.aidong.org
ServerAdmin admin@aidong.org
DocumentRoot /home/ftp/aidong_main/wwwroot
ErrorDocument 404 //
DirectoryIndex index.html index.htm index.php

ErrorLog /var/log/apache_log_main
php_admin_value open_basedir “/home/ftp/aidong_main/:/tmp/:/var/tmp/”
<Directory />
Allow from all
</Directory>
</VirtualHost>

#————————- Mark: sherry         User: sherry ——————-
<VirtualHost *:8080>
Serveralias
ServerAdmin admin@aidong.org
DocumentRoot /home/ftp/sherry_sherry/wwwroot
ErrorDocument 404 //
DirectoryIndex index.html index.htm index.php

ErrorLog /var/log/apache_log_sherry
php_admin_value open_basedir “/home/ftp/sherry_sherry/:/tmp/:/var/tmp/”
<Directory />
Allow from all
</Directory>
</VirtualHost>

分析上面一段配置文件就可以知道里面的就是IP地址,80就是端口号,于是我就想在网上找的教程里面让更改的配置文件是不是在这里,然后我就把改成了我的IP地址,把第二个端口改成8080,再根据网上找的教程内容在httpd.conf文件里加入了

Listen 80

Listen 8080

NameVirtualHost *:80

NameVirtualHost *:8080

(上面*即是你的IP地址)

保存退出然后重启apache

# /apache-restart

现在就可以通过IP地址加端口号访问不同的网站了即ip地址:端口号

其实如果你的IP地址是动态的话以上的*就不用改了,这样的话你就可以用动态IP访问你的网站了

利用这个方法你可以添加更多的网站(只要端口号够用)

注:此方法适用于用famp装的服务器,自己手动配置的服务器没有使用过。