11.14/11.15 Apache和PHP结合

时间:2022-04-27
本文章向大家介绍11.14/11.15 Apache和PHP结合,主要内容包括配置httpd支持php目录概要、编辑httpd的配置文件、第二处:更改Require all denied、第三处:因为要支持php,所以要增加一行php相关的配置、第四部分,增加一个索引页、检查Apache是否支持解析php5、检查Apache是否支持解析php7、Apache配置文件、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

配置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/local/apache2.4/bin/apachectl start //启动服务 netstat -lntp curl localhost vim /usr/local/apache2.4/htodcs/test.php //增加如下内容

<?php echo 123; ?>

  • curl localhost/test.php

编辑httpd的配置文件

  • 在安装好Apache、mysql、httpd,但是还未结合在一起,虽然apche调用了php作为它的模块,但是还不知道是否能解析php,所以现在还是需要编辑Apache的配置文件
  • httpd主配置文件,在/usr/local/apache2.4/conf/httpd.conf下
    • 编辑四个地方
      • 定义ServerName,去除 # 号
        • 在/usr/local/apache2.4/bin/apachectl restart 乍一看是报错信息,其实只是一个警告信息,并不是错误,若想不看到这个提示,可以在 /usr/local/apache2.4/conf/httpd.conf 取消掉
        • 方法:在/usr/local/apache2.4/conf/httpd.conf 中搜索 /ServerName ,然后将 # ServerName www.example.com:80 前的# 去除掉,保存退出即可
      • 把Require all denied 改成
        • 若是不更改,那么访问会是 403

第一处:取消警告信息

  1. 在运行/usr/local/apache2.4/bin/apachectl start 会跳出警告信息
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf        //ctrtl+z退出下

[1]+  已停止               vim /usr/local/apache2.4/conf/httpd.conf
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl restart        //会看到警告信息
AH00557: httpd: apr_sockaddr_info_get() failed for hanfeng
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
[root@hf-01 ~]# fg        //回到刚暂停的任务中——>vim /usr/local/apache2.4/conf/httpd.conf      

在文件中搜索 /ServerName
然后将 # ServerName www.example.com:80 前的# 去除掉保存退出即可
  1. 在去除# 号之后,运行apache,会提示79行错误
    • 原因:是因为在最后调用了两个PHP,加载了两个PHP,导致冲突
    • 解决方法:只需要注释掉一个PHP即可
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl restart
httpd not running, trying to start
/usr/local/apache2.4/bin/apachectl: 行 79:  2348 段错误               $HTTPD -k $ARGV
[root@hf-01 ~]# 
  1. 在注释掉一个PHP,就会发现不提示警告信息了
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl start
[root@hf-01 ~]# 
  1. 查看httpd进程
[root@hf-01 ~]# ps aux |grep httpd
root      2453  0.0  0.8 254072  8820 ?        Ss   23:52   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon    2454  0.0  0.8 540900  8972 ?        Sl   23:52   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon    2455  0.0  0.8 540900  8972 ?        Sl   23:52   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon    2456  0.0  0.8 540900  8972 ?        Sl   23:52   0:00 /usr/local/apache2.4/bin/httpd -k start
root      2541  0.0  0.0 112676   980 pts/0    R+   23:54   0:00 grep --color=auto httpd
[root@hf-01 ~]# 
  1. 因为在打开了ServerName,就不在提示警告信息了

第二处:更改Require all denied

  1. 首先,我们可以用浏览器访问自己虚拟机的IP,会发现无法访问
    • 则会提示检查代理服务器和防火墙
  2. 在遇到这种问题,排查方法
  3. 先查看IP是否是通的,用物理机去ping 虚拟机的 IP
  1. 若IP是通的,再去判断80端口是否是通的——>用物理机去ping80端口
  1. 因为windows 并没有打开telnet 客户端,所以会提示并没有这个命令
  2. 打开telnet客户端——>注意:不要打开Telnet服务端,否则会监听23端口,不安全
  • 打开物理机的控制面板
  • 然后选择程序
  • 再选择打开或关闭Windows功能
  • 选择Telnet客户端,并打开

7. 若是可以运行 telnet 命令,那出来的则是一个结果,显示80并不通

  • 因为在linux虚拟机上,并没有打开80端口
  1. 在虚拟机上,使用iptables -nvL 命令,会查看到80端口并没有打开
[root@hf-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  347 32935 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
  266 26673 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
  266 26673 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
  266 26673 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
  262 26469 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
等等,只截取了一部分,会发现80端口并没有打开
  1. 若想要windows机器去访问它,还需要加上一个操作
  • 单独一条命令,临时加上80端口——> iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@hf-01 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@hf-01 ~]# 
  1. 这时再用物理机去访问虚拟机IP,则会显示 It works!
  1. 之前不能正常访问,就是由于防火墙的规则,没有加上80端口,不能正常访问
  2. 这时打开windows来查看80端口,这样的界面表示80端口通了
  1. 在物理机中按 ctrl+] 退出来,然后在按 quit 字符退出
  1. 这时删除之前加的那条规则,再来物理机查看虚拟机的80端口,会发现不通了
[root@hf-01 ~]# iptables -D INPUT -p tcp --dport 80 -j ACCEPT
[root@hf-01 ~]# 
  1. 这时在把规则添加上,会发现又可以正常访问了
  2. 有时浏览器去访问虚拟机的IP,会显示 403Forbidden 错误界面
  • 这是因为在/usr/local/apache2.4/conf/httpd.conf配置文件中,有一个Require all denied
    • 然后搜索 /denied ,
  1. 打开/usr/local/apache2.4/conf/httpd.conf配置文件
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf

然后搜索 /denied ,会看到
<Directory />
    AllowOverride none
    Require all denied
</Directory>
把 denied 改成 granted 
  1. 之前可以在浏览器正常访问虚拟机ip,是因为
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf

是因为DocumentRoot指定了Directory,即如下:
DocumentRoot "/usr/local/apache2.4/htdocs"        
<Directory "/usr/local/apache2.4/htdocs">


还有后面的
Require all granted

若是将这里的Require all granted改成Require all denied ,那肯定无法浏览
  1. 在更改完配置文件,需要重新加载配置
  • /usr/local/apache2.4/bin/apachectl -t //检查你修改的配置文件是否存在错误
    • -t参数,会检查你修改的配置文件是否存在语法错误
      • 若语法不对,则会报错
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@hf-01 ~]# 
  • 重新加载配置文件
    • graceful 只会重新加载配置文件,而不会重启你的服务
      • 就是说即使你的配置文件有问题,它最多加载不成功,而不会把你的进程杀掉
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@hf-01 ~]# 
  1. 在配置文件中的,两处位置都应该改成granted,这是为了防止你在打开虚拟主机配置文件的时候,显示403Forbidden(403是它的状态码,正常情况下,状态码都是200)

第三处:因为要支持php,所以要增加一行php相关的配置

  • 如果不增加这一行,那么php就无法解析
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf

搜索 /AddType,然后
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz 

在这两行下面,增加php一行
    AddType application/x-httpd-php .php

第四部分,增加一个索引页

  • 索引页作用:在打开一个网站,为什么不输入index.php就可以直接访问呢
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf

搜索 /Index ,找到
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

在index.html后加 index.php
显示为
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
  • 然后检查配置文件是否有错误
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@hf-01 ~]# 
  • 然后重新加载配置文件
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@hf-01 ~]# 

检查Apache是否支持解析php5

  1. 验证
    • 写一个测试php脚本
      • 在访问的时候,访问的是htdocs下的文件
    • phpinfo,可以把php的相关信息打印出来
[root@hf-01 ~]# vi /usr/local/apache2.4/htdocs/1.php

在文件中写入

<?php
phpinfo();
?>

然后保存退出
  1. 然后在浏览器访问虚拟机ip地址并添加页面地址(IP/index.php),然后会看到显示出内容——>增加一个文件是不需要重启php的
http://192.168.74.129/1.php
  1. 当看到php正常显示,如上图,那说明php支持支持解析
  2. 如果php不支持解析(以上四步骤错误一处),那么刷新页面显示出来的则是源代码(就是 1.php 文件中的代码)
  3. 如果遇到php无法解析,则去检查Apache的配置文件
    1. 第一项,查看是否加载php5模块,使用/usr/local/apache2.4/bin/apachectl -M命令
      • 如果没有加载这个模块,查看这个模块是否存在。使用 ls /usr/local/apache2.4/modules/libphp5.so查看文件是否存在,若是文件都没有,那么肯定不会加载这个模块

    [root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_event_module (static) authn_file_module (shared) authn_core_module (shared) authz_host_module (shared) authz_groupfile_module (shared) authz_user_module (shared) authz_core_module (shared) access_compat_module (shared) auth_basic_module (shared) reqtimeout_module (shared) filter_module (shared) mime_module (shared) log_config_module (shared) env_module (shared) headers_module (shared) setenvif_module (shared) version_module (shared) unixd_module (shared) status_module (shared) autoindex_module (shared) dir_module (shared) alias_module (shared) php5_module (shared) [root@hf-01 ~]# ls /usr/local/apache2.4/modules/libphp5.so /usr/local/apache2.4/modules/libphp5.so

    1. 第二项,如果有文件,则没有显示php模块,那么就要去看apache的配置文件中有没有加载 libphp5 这一行配置,在文件中搜索/libphp5 ,如果没有这一行配置,那么肯定也是无法解析的

    [root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf 在文件中搜索/libphp5,看是否加载了这一行配置 LoadModule php5_module modules/libphp5.so

    1. 第三项,检查配置文件中是否加载了 AddType application/x-httpd-php .php 这一行
      • 在 .php 前面有一行空格,需要注意下!不能遗忘!!!——>使用 /usr/local/apache2.4/bin/apachectl -t 可以检查出配置写错了

    [root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf 在文件搜索/AddType中关于php这一行配置是否存在 AddType application/x-httpd-php .php

    1. 第四项,检查是否加了 index.php
      • 在访问一个网站的时候,比如访问www.baidu.com的时候,并不用是www.baidu.com/index.php访问,会发现,两个访问出的页面是相同的,就是因为这里有索引页(或者叫做index页),它默认能跳转到这个页面下去,包括直接输入虚拟机ip的时候,也会访问到 It works!这是访问到的是 index.html ,就是因为我们定义了一个index

    [root@hf-01 ~]# ls /usr/local/apache2.4/htdocs/ 1.php index.html [root@hf-01 ~]#

检查Apache是否支持解析php7

  1. 在配置文件中,注释掉php5,打开php7
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf

在文件中搜索 /libphp7,然后注释掉php5打开php7
# LoadModule php5_module        modules/libphp5.so
LoadModule php7_module        modules/libphp7.so
  1. 快捷键 ctrl+r ,可以快速执行命令历史中所用过的一些命令
    • 在快捷键 ctrl+r 之后,然后输入命令的一些字母,就会显示出你使用过的命令
  2. 然后检查配置文件是否存在语法错误,并重新加载配置文件
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t 
Syntax OK
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@hf-01 ~]# 
  1. 会看到更换了php7之后,用物理机访问虚拟机的ip,也会正常显示
  1. 在浏览器打开看到的phpinfo,和/usr/local/php7/bin/php -i |less看到的结果基本是一致的,只不过一个是文本形式,一个是图形化,图形化看起来更加直观,清晰明了
[root@hf-01 ~]# /usr/local/php7/bin/php -i |less
phpinfo()
PHP Version => 7.1.6

System => Linux hf-01 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64
Build Date => Dec 18 2017 22:25:37
Configure Command =>  './configure'  '--prefix=/usr/local/php7' '--with-apxs2=/usr/local/apache2.4/bin/apxs' '--with-config-file-path=/usr/local/php7/etc' '--with-pdo-mysql=/usr/local/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-libxml-dir' '--with-gd' '--with-jpeg-dir' '--with-png-dir' '--with-freetype-dir' '--with-iconv-dir' '--with-zlib-dir' '--with-bz2' '--with-openssl' '--with-mcrypt' '--enable-soap' '--enable-gd-native-ttf' '--enable-mbstring' '--enable-sockets' '--enable-exif'
Server API => Command Line Interface
Virtual Directory Support => enabled
Configuration File (php.ini) Path => /usr/local/php7/etc
Loaded Configuration File => (none)
Scan this dir for additional .ini files => (none)
Additional .ini files parsed => (none)
PHP API => 20160303
PHP Extension => 20160303
Zend Extension => 320160303
Zend Extension Build => API320160303,TS

等等等,只截取了一部分

Apache配置文件

  • 为什么可以在htdocs下面中可以访问到 index.html、1.php
  • 这是因为在/usr/local/apache2.4/conf/httpd.conf配置文件中的 DocumentRoot 参数,定义了网站的根目录在哪里,域名就是定义的ServerName
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf

在文件中搜索/htdocs,其中DocumentRoot 定义了文件根目录在哪里
DocumentRoot "/usr/local/apache2.4/htdocs"
<Directory "/usr/local/apache2.4/htdocs">

搜索/ServerName,域名就是定义的ServerName ,只不过这个ServerName,,可以使用任何一个域名去访问它,比如可以使用IP可以访问,使用example.com可以访问,用www也可以去访问它
ServerName www.example.com:80