11.14 apache和PHP结合(上)
1 使httpd可以解析php:
告警信息:
[[email protected] conf]# /usr/local/apache2.4/bin/apachectl restart
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::d46b:4589:4da1:2f34. Set the 'ServerName' directive globally to suppress this message
提示没有servername
解决:
[[email protected] conf]# vim /usr/local/apache2.4/conf/httpd.conf
…
# as error documents. e.g. [email protected]
#
ServerAdmin [email protected]
#
# 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 去掉注释,打开servername配置
#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
…
重新运行报错:
[[email protected] conf]# /usr/local/apache2.4/bin/apachectl restart
httpd not running, trying to start
/usr/local/apache2.4/bin/apachectl: 行 79: 27017 段错误 $HTTPD -k $ARGV
解决:
[[email protected] conf]# vim /usr/local/apache2.4/conf/httpd.conf
…
LoadModule php5_module modules/libphp5.so
#LoadModule php7_module modules/libphp7.so
…
[[email protected] conf]# /usr/local/apache2.4/bin/apachectl start
由于配置文件中同时运行两个libphp模块导致错误,注释掉其中一个模块即可;
不需要改动第79行;
1.1 编辑httpd配置文件:
第一处修改:
浏览器访问31.129:
修改前:
判断:
1 ping测试是否连通
2 测试服务器80端口是否打开
解决:
[[email protected] conf]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
第二处修改:
有时It works会显示成403 forbiden:
解决:
将denied改为granted:
[[email protected] conf]# vim /usr/local/apache2.4/conf/httpd.conf
…
# <Directory> blocks below.
#
<Directory />
AllowOverride none
Require all granted
</Directory>
…
能正常访问192.168.31.129的原因:
[[email protected] conf]# vim /usr/local/apache2.4/conf/httpd.conf
…
DocumentRoot "/usr/local/apache2.4/htdocs" 指定了访问目录
<Directory "/usr/local/apache2.4/htdocs">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
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 此处granted改为denied则无法正常访问
…
将granted改为denied后访问31.129:
检查apache配置文件语法:
[[email protected] htdocs]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
语法无误
[[email protected] htdocs]# /usr/local/apache2.4/bin/apachectl -t
AH00526: Syntax error on line 203 of /usr/local/apache2.4/conf/httpd.conf:
Argument for 'Require all' must be 'granted' or 'denied'
语法有错误
重新加载配置文件:
[[email protected] htdocs]# /usr/local/apache2.4/bin/apachectl graceful
此时不会重启服务
若修改的配置文件中有语法错误则重启后服务宕机
修改正在运行的apache的配置文件,再使用上面的命令加载配置时即使配置文件有问题也只会加载不成功,不会杀死apache相关进程
第三处修改:
防止打开虚拟主机配置文件时显示403 forbiden:
解决:
配置文件中此处保持为granted
…
# 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>
…
第四处修改:
为支持PHP,需要增加一行与php相关的配置:
[[email protected] htdocs]# vim /usr/local/apache2.4/conf/httpd.conf
…
#
# 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 不加载该行则php无法正常解析
#
# AddHandler allows you to map certain file extensions to "handlers":
…
第五处修改:
增加一个索引页(使用户仅输入域名或ip即可访问该页):
[[email protected] htdocs]# vim /usr/local/apache2.4/conf/httpd.conf
…
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
#
# The following lines prevent .htaccess and .htpasswd files from being
…
每次想让修改后的配置生效都要重新加载配置文件
测试apache是否支持解析php:
1 编辑测试文件
[[email protected] htdocs]# vim /usr/local/apache2.4/htdocs/1.php
<?php
phpinfo();
?>
增加一个网页文件不需要重启php
结果:
此处证明php已经支持解析
若在配置文件中去掉AddType application/x-httpd-php .php则用户访问到的是没有经过php解析的源代码:
php无法解析检查步骤:
php模块是否加载
[[email protected] htdocs]# /usr/local/apache2.4/bin/apachectl –M
未加载:
检查是否存在该模块的.so 文件
[[email protected] htdocs]# ls /usr/local/apache2.4/modules/libphp5.so
/usr/local/apache2.4/modules/libphp5.so
有则检查apache配置文件是否被注释或存在
LoadModule php5_module modules/libphp5.so
#LoadModule php7_module modules/libphp7.so
存在则检查以下配置是否存在并生效
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
#AddType application/x-httpd-php .php
快捷键ctrl+r:
按下快捷键后输入要执行的历史命令中的部分字符就可以显示完整历史命令,回车后即可执行
11.16 apache默认虚拟主机(上)
默认虚拟主机:一个服务下跑了多个网站(域名),每个网站的域名即配置文件中定义的servername(www.example.com),用户使用任何域名或ip都可以访问www.example.com
例子:一台服务器上用一个httpd服务同时运行了baidu和google的网站,即一个网站多个域名,此时每个域名对应一个虚拟主机
hosts文件:
路径:C:\Windows\System32\drivers\etc\hosts
可以在该文件中定义一个ip,一个域名,让该域名指向该ip
在hosts中指定ip与域名对应关系:
192.168.31.129 www.aaa.com www.123.com
DNS未生效时可以这样使用
问题:
服务器httpd配置文件没有定义www.aaa.com或www.123.com(配置文件中只有www.example.com),此时是如何实现访问的?
解决:
www.example.com是Apache的默认虚拟主机,任何域名只要指向该ip(31.129),都会访问到该站点
问题:
若在主配置文件中定义servername,则该配置文件只能定义一个servername,不好管理,想在配置文件中定义多个servername
解决:
打开虚拟主机配置文件:
在httpd.conf中搜索extra,定位到:
#Include conf/extra/httpd-info.conf
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf 该文件为虚拟主机配置文件
# Local access to the Apache HTTP Server Manual
去掉该行的#,启用该配置
去掉该行后则原来主配置文件中的servername失效(原来配置文件中仅能定义一个servername,不便于管理)
打开2级配置文件:
[[email protected] ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
…
<VirtualHost *:80> 该配置文件中每个VirtualHost代表一个主机即一个网站
DocumentRoot "/data/wwwroot/abc.com"
ServerName abc.com
ServerAlias www.abc.com www.123.comi
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.example.com 测试www.example.com是否生效
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" common
</VirtualHost>若不打开则任何域名指向过来都只能访问一个主机(域名)
修改文件内容:
ServerAdmin 定义管理员的邮箱,不是必须
DocumentRoot 定义网站根目录位置(重要)
ServerName 访问该站点的域名,此处仅能写一个
ServerAlias www.abc.com www.123.com 域名的别名,支持写多个
ErrorLog 错误日志
CustomLog
为刚定义的两个站点编写网站文件:
[[email protected] htdocs]# mkdir -p /data/wwwroot/abc.com
[[email protected] htdocs]# mkdir -p /data/wwwroot/111.com
[[email protected] htdocs]# vim /data/wwwroot/abc.com/index.php
[[email protected] htdocs]# vim /data/wwwroot/111.com/index.php
[[email protected] htdocs]# vim /data/wwwroot/abc.com/index.php
检查配置文件是否有错:
[[email protected] htdocs]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
重新加载配置文件:
[[email protected] htdocs]# /usr/local/apache2.4/bin/apachectl graceful
11.17 apache默认虚拟主机(下)
[[email protected] htdocs]# ping www.abc.com
PING abc.com (199.181.132.250) 56(84) bytes of data.
64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=1 ttl=128 time=368
由于没有在hosts文件中将ip与www.abc.com绑定,ping测试时会通过DNS解析到199地址上:
此时需要在hosts文件中将ip和www.abc.com绑定 或者
使用curl命令:
[[email protected] htdocs]# curl -x 192.168.31.129:80 abc.com
abc.com[[email protected] htdocs]#
指定192.168.31.129的80端口,访问这里的abc.com
[[email protected] htdocs]# curl -x 192.168.31.129:80 www.abc.com
abc.com[[email protected] htdocs]#
指定192.168.31.129的80端口,访问这里的www.abc.com
[[email protected] htdocs]# curl -x 192.168.31.129:80 www.abcdd.com
abc.com[[email protected] htdocs]#
/usr/local/apache2.4/conf/extra/httpd-vhosts.conf文件中没有定义www.abcdd.com这个servername,当服务器找不到对应的servername或serveralias时会将这个访问请求发给默认虚拟主机,说明此时servername为abc.com的虚拟主机为服务器的默认虚拟主机
无论访问什么网站,只要解析到该ip上,都会访问这个网站
[[email protected] htdocs]# curl -x 192.168.31.129:80 www.example.com
111.com[[email protected] htdocs]#
此时主配置文件(httpd.conf)中的www.example.com没有生效,httpd-vhosts.conf配置文件中的www.example.com生效
打开httpd-vhosts.conf配置文件后,原配置文件httpd.conf中的DocumentRoot和ServerName都会失效
总结:
一旦打开httpd-vhosts.conf则原配置文件httpd.conf的servername和DocumentRoot都会失效;
httpd-vhosts.conf中可定义多个虚拟主机,每个虚拟主机都对应了自己的servername和DocumentRoot,serveralias可以写多个;
每个virtualhost代表一个站点,其中有一个virtualhost为默认虚拟主机,即无论任何域名解析到该ip上都会访问默认虚拟主机;
原文地址:http://blog.51cto.com/12216458/2154277