天天看點

Apache和PHP結合、Apache預設虛拟主機

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已經正常

<a href="https://s1.51cto.com/oss/201711/09/a2c3409799a176addea5d8aaafb37acb.png-wh_500x0-wm_3-wmp_4-s_841585805.png" target="_blank"></a>

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才能解析

<b>5、</b>/usr/local/apache2.4/bin/apachectl graceful<b></b>

6、

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

&lt;?php

phpinfo();

<a href="https://s4.51cto.com/oss/201711/09/0315499068a0c12b1a69327ee52ca7b5.png-wh_500x0-wm_3-wmp_4-s_2062284237.png" target="_blank"></a>

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

&lt;IfModule dir_module&gt;

    DirectoryIndex index.html index.php  #新增這個

&lt;/IfModule&gt;

重載配置檔案

/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配置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、通路

<a href="https://s1.51cto.com/oss/201711/09/2edfb486d42636828b7a9f22b3799e5d.png-wh_500x0-wm_3-wmp_4-s_2218255659.png" target="_blank"></a>

Apache預設虛拟主機

1、打開vhosts虛拟主機

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

# Virtual hosts

Include conf/extra/httpd-vhosts.conf

3、定義了兩個虛拟主機

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

&lt;VirtualHost *:80&gt;

    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  #标準日志輸出

&lt;/VirtualHost&gt;

    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

&lt;h1&gt;this is abc.com&lt;/h1&gt;

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

&lt;h1&gt;this is 111.com&lt;/h1&gt;

[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拒絕通路:

<a href="https://s1.51cto.com/oss/201711/09/c4b401c14f2bb629ddafdcefb1acb2df.png-wh_500x0-wm_3-wmp_4-s_1770810071.png" target="_blank"></a>

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

&lt;Directory /&gt;

    Options FollowSymLinks

    AllowOverride None

    Order deny,allow

#    Deny from all

&lt;/Directory&gt;

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

9、通路

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

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

10、abc.com為預設的頁面

11、無論通路哪個都為調到這上

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

本文轉自方向對了,就不怕路遠了!51CTO部落格,原文連結:http://blog.51cto.com/jacksoner/1980209 ,如需轉載請自行聯系原作者