想你了的他他 发表于 2018-12-11 12:20:41

httpd中使用php模块解析php网页

在httpd配置文件中添加php解析
  修改apache的配置文件,让apache可以使用php进行解析
httpd的配置文件在/usr/local/httpd/conf/httpd.conf路径
在httpd配置文件中修改如下几个配置参数,在httpd.conf配置文件中查找一下关键字符配置

# vim conf/httpd.conf
~
这里安装了两个php模块,需要注释掉一个php模块。因为httpd不能同时加载两个php模块
LoadModule php5_module modules/libphp5.so      
#LoadModule php7_module modules/libphp7.so
将ServerName行的注释去掉,这样可以使httpd能够正常启动和访问默认页
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
ServerName www.example.com:80
修改默认的目录策略,denied表示拒绝访问网页目录,granted表示允许访问网页目录,防止打开虚拟主机配置会显示网页报错状态码403
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
#blocks below.
#

 AllowOverride none
 Require all granted

网页根目录的配置路径及权限配置,Require allgranted这里配置和上面的目录访问有关联的作用,表示网页目录是否能够有权限访问
DocumentRoot "/usr/local/httpd/htdocs"

 Options Indexes FollowSymLinks
   # AllowOverride controls what directives may be placed in .htaccess files.
   # It can be "All", "None", or any combination of the keywords:
   #   AllowOverride FileInfo AuthConfig Limit
 AllowOverride None
   # Controls who can get stuff from this server.
 Require all granted

添加php的解析主页,添加php的解析主页,在访问时才能对php页面正确识别

 DirectoryIndex index.html index.php

添加php解析的配置项,httpd中必须添加解析配置项才能让httpd的php解析请求发送到php模块进行解析
   # If the AddEncoding directives above are commented-out, then you
   # probably should define those extensions to indicate media types:
   #
 AddType application/x-compress .Z
 AddType application/x-gzip .gz .tgz
 AddType application/x-httpd-php .php
  检查httpd配置文件是否正确,测试配置内容,这里注意的是一个使用习惯,在修改配置文件后首先检查配置文件中是否有错误,如果配置文件中配置有误而不检查直接重新启动服务,会造成服务启动时的报错服务启动终止,导致服务对外停止提供访问,这样会造成短时间内服务无法访问的情况,需要检查配置文件后并重启服务,使用graceful对服务重新加载配置即可

# /usr/local/httpd/bin/apachectl -t
Syntax OK
# /usr/local/httpd/bin/apachectl graceful
# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1085/sshd  
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1388/master
tcp6       0      0 :::3306                 :::*                  LISTEN      8759/mysqld
tcp6       0      0 :::80                   :::*                  LISTEN      873/httpd  
tcp6       0      0 :::22                   :::*                  LISTEN      1085/sshd  
tcp6       0      0 ::1:25
httpd的php解析,在httpd的网页目录下写一个php测试的文件内容
  # cat htdocs/index.php


php页面正确解析访问结果示例:

httpd加载了php模块,并返回网页报头信息,响应的服务端解析程序是PHP/5.6.37

# curl -I 192.168.1.233/index.php
HTTP/1.1 200 OK
Date: Sun, 29 Jul 2018 09:22:59 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.37
X-Powered-By: PHP/5.6.37
Content-Type: text/html; charset=UTF-8
  如果写了php测试页但无法显示php的测试页面,需要从下面几个方面检查一遍:
php无法解析,首先检查是否有php模块,是否在httpd配置文件中有php的加载配置参数,是否写入解析php的配置参数,测试时是否指定首页的索引解析页

# /usr/local/httpd/bin/apachectl -M
Loaded Modules:
core_module (static)
so_module (static)
-----------------------------------略-------------------------
alias_module (shared)
php5_module (shared)
# cat conf/httpd.conf |grep php.*so
LoadModule php5_module modules/libphp5.so
# cat conf/httpd.conf |grep AddType
   # AddType allows you to add to or override the MIME configuration
   #AddType application/x-gzip .tgz
 AddType application/x-compress .Z
 AddType application/x-gzip .gz .tgz
 AddType application/x-httpd-php .php
   
# cat conf/httpd.conf |grep index.php
 DirectoryIndex index.html index.php
  httpd虚拟主机配置
关于虚拟主机:
一台服务器可以运行多个网站,一个主机多个域名指定不同的网站,每个网站都是一个虚拟主机
  修改windows的解析文件,这里只作为测试使用,修改的文件路径在C:\Windows\System32\drivers\etc\  ,用笔记本打开HOSTS文件,写法格式为ip  域名  域名 ;可以指定一个ip有多个域名
在客户端访问没有配置过的默认虚拟主机的httpd,解析任何的域名的ip如果指向该服务器的ip地址会直接访问到该httpd的默认主机上
  配置httpd的虚拟主机,打开虚拟主机配置文件,修改conf/httpd.conf配置文件,去掉注释或添加:

# Virtual hosts
Include conf/extra/httpd-vhosts.conf
  打开虚拟主机配置后,原来的主配置文件中定义的网站配置会失效停止访问,生效的是虚拟主机配置文件中的设置。虚拟主机配置项如下示例:

# cat conf/extra/httpd-vhosts.conf
# Virtual Hosts
#
# Required modules: mod_log_config
-------------------------------省略---------------------------------------

 DocumentRoot "/usr/local/httpd/docs/abcd.com"    
页: [1]
查看完整版本: httpd中使用php模块解析php网页