LAMP 架构——配置 Apache(httpd) 结合 php

Apache 主配置文件为:/usr/local/apache2/conf/httpd.conf

配置 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 // 启动服务
具体步骤如下:

配置之前启动 Apache

[root@dl-001 conf]# /usr/local/apache2.4/bin/apachectl restart
httpd: Could not reliably determine the server's fully qualified domain name, using fe80::50e7:255d:8ff7:2d3d for ServerName
httpd not running, trying to start

解决办法:

步骤 1

[root@dl-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf
 99 LoadModule alias_module modules/mod_alias.so
100 LoadModule rewrite_module modules/mod_rewrite.so
101 LoadModule php5_module        modules/libphp5.so
102 #LoadModule php7_module        modules/libphp7.so      //因为下载的两个版本的php所以需要注释一个。
.......................................
.....................................
140 # ServerName gives the name and port that the server uses to identify itself.
141 # This can often be determined automatically, but we recommend you specify
142 # it explicitly to prevent problems during startup.
143 #
144 # If your host doesn't have a registered DNS name, enter its IP address here.
145 #
146 ServerName www.example.com:80        //去除#(该行默认是注释的)
[root@dl-001 conf]# /usr/local/apache2.4/bin/apachectl restart    //再次启动apache
[root@dl-001 conf]# ps -aux|grep httpd        //查看进程(启动成功)
root      5657  0.0  1.2 315668 12156 ?        Ss   18:19   0:00 /usr/local/apache2.4/bin/httpd -k restart
daemon    5669  0.0  0.6 315668  6504 ?        S    18:23   0:00 /usr/local/apache2.4/bin/httpd -k restart
daemon    5670  0.0  0.6 315668  6504 ?        S    18:23   0:00 /usr/local/apache2.4/bin/httpd -k restart
daemon    5671  0.0  0.6 315668  6504 ?        S    18:23   0:00 /usr/local/apache2.4/bin/httpd -k restart
daemon    5672  0.0  0.6 315668  6504 ?        S    18:23   0:00 /usr/local/apache2.4/bin/httpd -k restart
daemon    5673  0.0  0.6 315668  6504 ?        S    18:23   0:00 /usr/local/apache2.4/bin/httpd -k restart
root      5675  0.0  0.0 112680   976 pts/1    R+   18:23   0:00 grep --color=auto httpd

步骤 2

虚拟机: [root@dl-001 conf]# iptables -I INPUT -p tcp - -dport 80 -j ACCEPT
// 临时添加监听 80 端口的规则

物理机:启用 win7 中 telnet 命令 开始–> 控制面板控制面板–> 程序–> 启动或关闭 Windows 功能–> 选择上 telnet 客户端

ping  192.168.114.128       //查看ip是否是通的
ping 192.168.114.128 80     //查看端口是否是通的

说明:可以使用浏览器直接访问本地虚拟机 IP。


步骤 3(注意版本)

[root@dl-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf
<Directory />
         AllowOverride none
    Require all granted        //改为granted 2.4.27版本的Apache
</Directory>


<Directory />
   Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Allow from all        //Deny改为Allow  2.4.29版本的Apache 
</Directory>

步骤 4

//检测配置是否存在语法错误
[root@dl-001 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK

//重新加载配置文件
[root@dl-001 ~]# /usr/local/apache2.4/bin/apachectl graceful

注:该命令不会使服务重启,只是加载配置文件的内容。


步骤 5

    [root@dl-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php    //增加此行。如果不添加PHP无法解析


#
<IfModule dir_module>
    DirectoryIndex index.html 1.php    //添加1.php
</IfModule>

#