- httpd的主配置文件是/usr/local/apache2.4/conf/httpd.conf
- vim /usr/local/apache2.4/conf/httpd.conf #修改以下4个地方
- (1)搜索www.example.com:80 ,去掉#
ServerName #ServerName
- (2)搜索Require all denied,改为Require all granted
Require all denied
- (3)在AddType application/x-gzip .gz .tgz下面加上如下一行
AddType application/x-httpd-php .php #注意php .php中需有一个空格
-
(4)搜索DirectoryIndex index.html,改为如下:
DirectoryIndex index.html index.php
-
验证apache是否支持解析PHP(测试PHP脚本)
vim /usr/local/apache2.4/htodcs/test.php //增加如下内容
<?php
echo 123;
?>
在浏览器中输入PHP所在虚拟机IP,如果能顺利打打页面则说明apache支持解析PHP
curl localhost/test.php
-
遇到PHP解析不了问题的排查方法:
(1)查看apache是否有加载PHP模块
/usr/local/apache2.4/bin/apachectl -M
(2)查看是否有这个PHP模块
ls /usr/local/apache2.4/modules/libphp5.so
(3)检查配置文件里是否有加载PHP模块的语句
vim /usr/local/apache2.4/conf/httpd.conf
LoadModule php5_module modules/libphp5.so
(4)检查配置文件里是否有AddType..的语句
vim /usr/local/apache2.4/conf/httpd.conf
AddType application/x-httpd-php .php
- 知识点:
/usr/local/apache2.4/bin/apachectl -t #检查配置文件httpd.conf语法是否正确
/usr/local/apache2.4/bin/apachectl graceful #重新加载配置文件(不会重启服务)
/usr/local/apache2.4/bin/apachectl start //启动服务
iptables -I INPUT -p tcp --dport 80 -j ACCEPT #开放80端口
iptables -D INPUT -p tcp --dport 80 -j ACCEPT #关闭80端口
netstat -lntp #查看系统监听的端口
11.16/17 Apache默认虚拟主机 [疑难]
- 一台服务器可以有多个网站,每个网站都是一个虚拟主机.
- 概念:域名(主机名)、DNS、解析域名、hosts.
- 任何一个域名解析到这台机器,都可以访问的虚拟主机就是默认虚拟主机.
- vim /usr/local/apache2.4/conf/httpd.conf #搜索httpd-vhost,去掉#号
- vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf #改为如下:
-
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/abc.com"
ServerName abc.com
ServerAlias www.abc.com www.123.com
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common
</VirtualHost>
- <VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.111.com
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" common
</VirtualHost>
- /usr/local/apache2.4/bin/apachectl –t
-
/usr/local/apache2.4/bin/apachectl graceful
- mkdir -p /data/wwwroot/abc.com /data/wwwroot/www.111.com
echo "abc.com" > /data/wwwroot/abc.com/index.html #网站默认的主页就是index.html
echo "111.com" > /data/wwwroot/111.com/index.html
curl -x127.0.0.1:80 abc.com #这样会去访问abc.com/index.html
curl -x127.0.0.1:80 www.111.com #访问www.111.com
curl -x127.0.0.1:80 www.abc.com