wewe22 发表于 2015-1-12 10:03:27

apache服务与虚拟主机

apache是典型优秀的Web服务器,可以运行在linux,unix,windows等环境。其具有开源,跨平台,支持各种语言,模块化设置,稳定与安全等特点。    以红冒企业版RHEL5为例,搭建与配置apache服务。
    配置好IP,网关,掩码等基本信息。
    为了避免端口和程序发生冲突,删除以RPM包安装的相关软件。可根据实际情况删除。
rpm -e httpd httpd-manual webalizer subversion mod_python mod_ssl mod_perl system-config-httpd php php-cli php-ldap php-common mysql dovecot --nodeps
    在删除过程中,如果出现error: package mysql is not installed报错,则在命令上将这个选项去掉再删即可。
    将httpd解压到/usr/src目录下,并进入其目录。
tar zxf httpd-2.2.17.tar.gz -C /usr/src/
cd /usr/src/httpd-2.2.17/
    开始编译安装。指定安装在/usr/local/httpd目录下,启用动态加载模块,启用网页地址重写,启用字符集支持,启用CGI脚本程序支持。
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install
安装完成后,可以通过ls /usr/local/httpd查看结果。
优化执行路径
ln -s /usr/local/httpd/bin/* /usr/local/bin/
启动服务
/usr/local/httpd/bin/apachectl start
netstat -anpt | grep httpd
配置并启动httpd服务。
网站根目录位于/usr/local/httpd/htdocs中,需要将Web的网页复制到该目录中。例如打开默认的测试页面:
    日志文件位于/usr/local/httpd/log目录下,有access_log和error_log。
    httpd.conf配置文件,位于/usr/local/httpd/conf/httpd.conf。
vim /usr/local/httpd/conf/httpd.conf

    一些常用到的配置项:
ServerRoot "/usr/local/httpd"   //服务器的根目录
Listen 80//监听端口
DocumentRoot "/usr/local/httpd/htdocs"    //网页存放位置
DirectoryIndex index.html            //默认索引页,可以多个,以空格分开。
//区域配置项:仅针对该区域有效,不干涉全局。
//可以是
    Options FollowSymLinks      //控制选项,允许使用符号链接
    AllowOverride None                //不允许隐含控制文件中的覆盖配置
    Order deny,allow                  //访问控制策略的应用顺序
    Deny from all                        //禁止任何人访问该区域
                            //该区域结束
    通常情况下Web对所有的客户端都开放,所以应该使用Allow from all 允许所有人访问策略。但有时某些区域只拒绝/允许一部分主机访问,例如后台或者AWStats系统等。例如修改为:

    ......
    Order deny,allow
    Deny from all            //拒绝访问的主机,例如所有all,可以使网段和一个ip
   #Order allow,deny
   #Allow from all            //允许访问的主机,例如所有all

保存退出,编辑/usr/local/httpd/htdocs/test/test.html
cat >>/usr/local/httpd/htdocs/test/test.html<<end
test
end
重启服务
/usr/local/httpd/bin/apachectl stop
/usr/local/httpd/bin/apachectl start
测试为拒绝访问!!
    构建虚拟WEB主机,基于域名,基于IP,基于端口。
    在此测试前,应该搭建好DNS服务器。略
    创建两个不同网站,用作测试。
mkdir /var/www/html/testcom
mkdir /var/www/html/webcom
echo "www.test.com" > /var/www/html/testcom/index.html
echo "www.web.com" > /var/www/html/webcom/index.html
方法一:三种虚拟主机都可以直接在httpd配置文件上修改,
修改监听地址,例如211.1.1.1:80
虚拟主机区域,例如......
目录权限等,例如......
方法二:直接修改比较少,这样会改动太多主配置文件,通常是用独立配置文件来配置虚拟主机,,然后在httpd.conf文件通过lnclude加载这些配置。这有利于减少httpd.conf文件的改动。
1,基于域名虚拟主机:
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
NameVirtualHost 192.168.78.129:80
      //区域配置文件目录
    Order allow,deny//允许所有人访问
    Allow from all

      //虚拟主机的IP地址
    DocumentRoot "/var/www/html/testcom"    //网站地址
    ServerName www.test.com            //服务名称
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common



    DocumentRoot "/var/www/html/webcom"
    ServerName www.web.com
    ErrorLog "logs/dummy-host2.example.com-error_log"
    CustomLog "logs/dummy-host2.example.com-access_log" common

保存退出。
修改主配置文件,添加独立配置文件。
vim /usr/local/httpd/conf/httpd.conf
....//略
Include conf/extra/httpd-vhosts.conf
保存退出,并重启httpd服务。
/usr/local/httpd/bin/apachectl restart
2,基于IP地址的虚拟主机
(PS:实验可以使用同一网段,减少DNS配置和网段调整等工作。添加多一张虚拟网卡或编辑虚拟网卡di地址:ifconfig eth0:0 192.168.78.130/24)
同样使用独立配置文件的方法。
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
            //区域配置文件目录
    Order allow,deny                         //允许所有人访问
    Allow from all

                  //虚拟主机的IP地址
    DocumentRoot "/var/www/html/testcom"    //网站目录
    ServerName www.test.com                     //服务名称
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common



    DocumentRoot "/var/www/html/webcom"
    ServerName www.web.com
    ErrorLog "logs/dummy-host2.example.com-error_log"
    CustomLog "logs/dummy-host2.example.com-access_log" common

保存退出,同样需要在主配置文件加载配置文件。

3,基于端口的虚拟主机
    这种虚拟主机,一般需要指定端口才可以访问。一般访问网页默认都是80(http)或443(https)。因此在配置虚拟主机时需要指定端口。
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
      //区域配置文件目录
    Order allow,deny                      //允许所有人访问
    Allow from all

                  //虚拟主机的IP地址
    DocumentRoot "/var/www/html/testcom"    //网站目录
    ServerName www.test.com                     //服务名称
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common


                //虚拟主机的IP地址加端口!!
    DocumentRoot "/var/www/html/webcom"
    ServerName www.web.com
    ErrorLog "logs/dummy-host2.example.com-error_log"
    CustomLog "logs/dummy-host2.example.com-access_log" common

保存退出,同样需要在主配置文件上加载独立配置文件。
在主配置文件需要添加监听端口!!
vim /usr/local/httpd/conf/httpd.conf
Listen 1234
重启服务
则可以看到结果。

</end
页: [1]
查看完整版本: apache服务与虚拟主机