httpd连接php 和 httpd定义虚拟主机

配置httpd<-> php

  • 配置httpd支持php
  • 配置httpd支持php并且 配置虚拟主机

配置httpd支持php

  • httpd 祝配置文件 /usr/local/apache2.4/conf/httpd.conf
  • vim /usr/local/apache2.4/conf/httpd.conf #修改4个地方
ServerName
Require all denied
AddType application/x-httpd-php .php
Directoryindex index.html index.php
  • /usr/local/apache2.4/bin/apachectl -t #测试语法
  • /usr/loca/apache2.4/bin/apachectl start #启动服务
  • netstat -lntp
  • curl localhost 或者 windows访问主机
  • vim /usr/local/apache2.4/htodcs/test.php #测试php加载成功后读取的文件
<?php
echo "test-php";
?>
  • curl localhost/test.php

httpd配置虚拟主机

  • 一台服务器中可以有多个网站提供被客户访问,每个网站对应一个虚拟主机
  • 配置httpd.conf  注释"Include conf/extra/httpd-vhosts.conf " #开启虚拟机功能
  • 开启vhost_alias_module 模块,开启虚拟机别名设置
  • 虚拟主机配置文件:/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
  • vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com #邮箱可以去除
    DocumentRoot "/data/www/test01/" #定义访问目录
    ServerName www.test01.com  #定义域名
    ServerAlias test01.com test01-test.com #定义域名别名,可以定义多个别名
    ErrorLog "logs/test01.dummy-host.example.com-error_log" #日志文件
    CustomLog "logs/test01.dummy-host.example.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/data/www/test02/"
    ServerName www.test02.com
    ServerAlias test02.com test02-test.com
    ErrorLog "logs/test02.dummy-host2.example.com-error_log"
    CustomLog "logs/test02.dummy-host2.example.com-access_log" common
</VirtualHost>
  • mkdir /data/www/test01
  • mkdir /data/www/test02

test01/index.php

 <?php
echo "teststart-test01.com";
?>

test02/index.php

 <?php
echo "teststart-test02.com";
?>
  • /usr/local/apache2.4/bin/apachectl -t #检查语法
  • /usr/local/apache2.4/bin/apachectl graceful #重新加载配置文件

  • curl -x192.168.31.20 www.test01.com #访问的是 test01的虚拟主机

  • curl -x192.168.31.20 www.test02.com #访问的是 test02的虚拟主机
  • curl -x192.168.31.20 #访问的是 test01的虚拟主机
  • curl -x192.168.31.20 www.test03.com #域名test03 不存在虚拟主机中,未定义,故会去访问默认的虚拟主机即第一个定义的”test01”

  • 在windows访问:需要设置hosts文件C:\Windows\System32\drivers\etc\hosts

    192.168.31.20   test01.com #增加这行
    

    windows就会被指向 test01.com 网站的内容,其他同理

  • 如果客户端访问出现 403 forbidden 错误,可以查看文件 /usr/local/apache2.4/conf/httpd.conf 文件中


<Directory />
    AllowOverride none
    Require all denied     

</Directory>
  • 以上禁止了访问根目录 所以有两个解决办法去改变这个权限,

1:在这个文件此处直接改成granted

2、在httpd-vhosts.conf 文件 相应的虚拟机定义中取定义目录权限,这样比上面定义要安全,比如:


#此处是在<VirtualHost *:80> </VirtualHost> 中定义

<Directory "/data/www/test01">
           #Options Indexes FollowSymLinks  #此处是当访问一个服务器的时候不写路径,并且文件夹中没有index类型文件的时候,会返回此服务器的目录结构
           Options index FollowSymLinks      #这个选项就不会返回目录结构 ,也会提示 403 forbidden
           AllowOverride None           #  设置"None" 时,会忽略".htaccess"文件,设置为"All"的时候 访问服务器会去  .htaccess 中执行命令,算是命令重定向了
           Order deny,allow
           Allow from all
           Require all granted
    </Directory>

访问httpd 虚拟服务其过程

客户端(windows)

test01.com -> hosts 找到IP ->访问 ip的httpd服务器 ->然后根据被访问的域名(test01.com),指向该虚拟主机的服务器,并返回结果

posted on 2017-12-20 08:19  游荡的鱼  阅读(257)  评论(0编辑  收藏  举报

导航