httpd中使用php模块解析php网页php 解析url
在httpd配置文件中添加php解析
修改apache的配置文件,让apache可以使用php进行解析 httpd的配置文件在/usr/local/httpd/conf/httpd.conf路径 在httpd配置文件中修改如下几个配置参数,在httpd.conf配置文件中查找一下关键字符配置
[root@localhost httpd]# vim conf/httpd.conf ~ 这里安装了两个php模块,需要注释掉一个php模块。因为httpd不能同时加载两个php模块 LoadModule php5_module modules/ #LoadModule php7_module modules/ 将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 # <Directory> blocks below. # <Directory /> AllowOverride none Require all granted </Directory> 网页根目录的配置路径及权限配置,Require all granted这里配置和上面的目录访问有关联的作用,表示网页目录是否能够有权限访问 DocumentRoot "/usr/local/httpd/htdocs" <Directory "/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 </Directory> 添加php的解析主页,添加php的解析主页,在访问时才能对php页面正确识别 <IfModule dir_module> DirectoryIndex index.html index.php </IfModule> 添加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对服务重新加载配置即可
[root@localhost httpd]# /usr/local/httpd/bin/apachectl -t Syntax OK [root@localhost httpd]# /usr/local/httpd/bin/apachectl graceful [root@localhost httpd]# 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:25httpd的php解析,在httpd的网页目录下写一个php测试的文件内容
[root@localhost httpd]# cat htdocs/index.php
<?php phpinfo(); ?>php页面正确解析访问结果示例:
httpd加载了php模块,并返回网页报头信息,响应的服务端解析程序是PHP/5.6.37
[root@localhost httpd]# 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的配置参数,测试时是否指定首页的索引解析页
[root@localhost httpd]# /usr/local/httpd/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) -----------------------------------略------------------------- alias_module (shared) php5_module (shared) [root@localhost httpd]# cat conf/httpd.conf |grep php.*so LoadModule php5_module modules/ [root@localhost httpd]# 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 [root@localhost httpd]# cat conf/httpd.conf |grep index.php DirectoryIndex index.html index.phphttpd虚拟主机配置 关于虚拟主机: 一台服务器可以运行多个网站,一个主机多个域名指定不同的网站,每个网站都是一个虚拟主机
修改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打开虚拟主机配置后,原来的主配置文件中定义的网站配置会失效停止访问,生效的是虚拟主机配置文件中的设置。虚拟主机配置项如下示例:
[root@localhost httpd]# cat conf/extra/httpd-vhosts.conf # Virtual Hosts # # Required modules: mod_log_config -------------------------------省略--------------------------------------- <VirtualHost *:80> DocumentRoot "/usr/local/httpd/docs/" <---网站网页加载路径 ServerName <---解析的虚拟主机名 ServerAlias www. <---虚拟主机域名 ErrorLog "logs/-error_log" <---虚拟主机错误日志 CustomLog "logs/-access_log" common <---虚拟主机访问日志 </VirtualHost> <VirtualHost *:80> DocumentRoot "/usr/local/httpd/docs/123.com" ServerName 123.com ServerAlias ErrorLog "logs/123.com-error_log" CustomLog "logs/123.com-access_log" common </VirtualHost>创建虚拟主机的网页存储目录,并使用apachectl检查一下配置文件,然后重新加载httpd配置
[root@localhost httpd]# mkdir -p /usr/local/httpd/docs/ [root@localhost httpd]# mkdir -p /usr/local/httpd/docs/123.com [root@localhost httpd]# /usr/local/httpd/bin/apachectl -t Syntax OK [root@localhost httpd]# /usr/local/httpd/bin/apachectl graceful在windows端和linux端简要的认证一下虚拟主机访问结果:
linux下使用curl命令访问web网页的内容,结果如下:
[root@localhost ]# curl -x192.168.1.233:80 [root@localhost ]# curl -x192.168.1.233:80 123.com 123.com