Apache和PHP结合

1、先禁用之前的php7的模块

[root@centos7 ~]# cat /usr/local/apache2.4/conf/httpd.conf| grep 'php'

LoadModule php5_module        modules/libphp5.so

#LoadModule php7_module        modules/libphp7.so


2、查看下是否禁用php7模块

[root@centos7 ~]# /usr/local/apache2.4/bin/apachectl -M

 rewrite_module (shared)

 php5_module (shared) #只有一个,说明禁用了

Syntax OK


3、访问下apache是否工作,可以看到It works!说明apache已经正常

Apache和PHP结合、Apache默认虚拟主机_第1张图片


4、添加php服务解析

[root@centos7 ~]# vi /usr/local/apache2.4/conf/httpd.conf

    AddType application/x-compress .Z

    AddType application/x-gzip .gz .tgz

    AddType application/x-httpd-php .php  #添加下这行后,apache才能解析

5、/usr/local/apache2.4/bin/apachectl graceful

6、

[root@centos7 ~]# vi  /usr/local/apache2.4/htdocs/1.php

phpinfo();

Apache和PHP结合、Apache默认虚拟主机_第2张图片

7、vi /usr/local/apache2.4/conf/httpd.conf

    DirectoryIndex index.html index.php  #新增这个

重载配置文件

/usr/local/apache2.4/bin/apachectl graceful

mv   /usr/local/apache2.4/htdocs/1.php /usr/local/apache2.4/htdocs/index.php

8、访问http://192.168.3.74

Apache和PHP结合、Apache默认虚拟主机_第3张图片


apache配置php7来解析

1、[root@centos7 htdocs]# vi /usr/local/apache2.4/conf/httpd.conf

#LoadModule php5_module        modules/libphp5.so

LoadModule php7_module        modules/libphp7.so

2、[root@centos7 htdocs]# /usr/local/apache2.4/bin/apachectl graceful

3、访问
Apache和PHP结合、Apache默认虚拟主机_第4张图片


Apache默认虚拟主机

1、打开vhosts虚拟主机

[root@centos7 htdocs]# vi /usr/local/apache2.4/conf/httpd.conf

# Virtual hosts

Include conf/extra/httpd-vhosts.conf

2、[root@centos7 htdocs]# /usr/local/apache2.4/bin/apachectl graceful

3、定义了两个虚拟主机

[root@centos7 htdocs]# cat  /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 

    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  #标准日志输出


    DocumentRoot "/data/wwwroot/111.com"

    ServerName 111.com

    ServerAlias www.111.com www.example.com

    ErrorLog "logs/111.com-error_log"

    CustomLog "logs/111.com-access_log" common

4、

[root@centos7 htdocs]# mkdir /data/wwwroot/

[root@centos7 htdocs]# mkdir /data/wwwroot/abc.com

[root@centos7 htdocs]# mkdir /data/wwwroot/111.com

5、

[root@centos7 htdocs]# vi /data/wwwroot/abc.com/index.php


this is abc.com

[root@centos7 htdocs]# vi /data/wwwroot/111.com/index.php


this is 111.com

6、

[root@centos7 htdocs]# /usr/local/apache2.4/bin/apachectl graceful

7、访问:[root@centos7 abc.com]# curl -x http://127.0.0.1:80 abc.com

错误:403拒绝访问:

c4b401c14f2bb629ddafdcefb1acb2df.png-wh_

解决: vi /usr/local/apache2.4/conf/httpd.conf

    Options FollowSymLinks

    AllowOverride None

    Order deny,allow

#    Deny from all

8、/usr/local/apache2.4/bin/apachectl graceful

9、访问

[root@centos7 abc.com]# curl -x127.0.0.1:80 www.abc.com

this is abc.com

[root@centos7 abc.com]# curl -x127.0.0.1:80 www.111.com

this is 111.com

10、abc.com为默认的页面

11、无论访问哪个都为调到这上

[root@centos7 abc.com]# curl -x127.0.0.1:80 123123123asdasd

this is abc.com