在LAMP架構中,Apache通過PHP模塊與Mysql建立連接,讀寫數據。那么配置Apache和PHP結合的步驟是怎么操作的呢?
1、修改http.conf文件[root@juispan?~]#?cat?/usr/local/apache2.4/conf/httpd.conf
......
#ServerName
......
AllowOverride?none
Require?all?denied
......
DirectoryIndex?index.html
......
AddType?application/x-compress?.Z
AddType?application/x-gzip?.gz?.tgz
......
[root@juispan?~]#?vi?/usr/local/apache2.4/conf/httpd.conf
......
ServerName???????????##取消了注釋號
......
AllowOverride?none
Require?all?granted????????????????##更改了denied限制
......
DirectoryIndex?index.html?index.php??##增加了“index.php”
......
AddType?application/x-compress?.Z
AddType?application/x-gzip?.gz?.tgz
AddType?application/x-httpd-php?.php??##增加了這行
......
2、檢查與啟動httpd[root@juispan?~]#?/usr/local/apache2.4/bin/apachectl?-t
Syntax?OK
[root@juispan?~]#?/usr/local/apache2.4/bin/apachectl?start
/usr/local/apache2.4/bin/apachectl:?行?79:?57987?段錯誤???????????????$HTTPD?-k?$ARGV
啟動出錯,因為這里有兩個php的模塊,注釋掉一個即可。[root@juispan?~]#?cat?/usr/local/apache2.4/conf/httpd.conf?|?grep?-i?php
#LoadModule?php5_module????????modules/libphp5.so??????##這里用php?7
LoadModule?php7_module????????modules/libphp7.so
DirectoryIndex?index.html?index.php
AddType?application/x-httpd-php?.php
[root@juispan?~]#?/usr/local/apache2.4/bin/apachectl?start
[root@juispan?~]#?/usr/local/apache2.4/bin/apachectl?-M?|?tail?-2
alias_module?(shared)
php7_module?(shared)
使用graceful能直接刷新配置,而不重啟httpd服務。按ctrl+r能搜索命令歷史。(reverse-i-search)`graceful':?/usr/local/apache2.4/bin/apachectl?graceful
3、修改防火墻[root@juispan?~]#?iptables?-I?INPUT?-p?tcp?--dport?80?-j?ACCEPT
[root@juispan?~]#?iptables?-nvL?|?grep?':80'
0?????0?ACCEPT?????tcp??--??*??*???0.0.0.0/0?????0.0.0.0/0?????tcp?dpt:80
0?????0?ACCEPT?????tcp??--??*??*???0.0.0.0/0?????0.0.0.0/0?????tcp?dpt:80?ctstate?NEW
4、配置測試頁[root@juispan?~]#?vi?/usr/local/apache2.4/htdocs/1.php
phpinfo();
?>
5、客戶端打開測試頁面(ip/1.php)
▎如果客戶端解析不出來,可以從以下幾方面進行排查:
1)檢查php模塊是否被加載[root@juispan?~]#?/usr/local/apache2.4/bin/apachectl?-M?|?tail?-1
php7_module?(shared)
2)檢查php模塊文件是否存在[root@juispan?~]#?ls?/usr/local/apache2.4/modules/libphp*
/usr/local/apache2.4/modules/libphp5.so
/usr/local/apache2.4/modules/libphp7.so
3)檢查配置文件是否正確[root@juispan?~]#?cat?/usr/local/apache2.4/conf/httpd.conf?|?grep?'php'
#LoadModule?php5_module????????modules/libphp5.so
LoadModule?php7_module????????modules/libphp7.so
DirectoryIndex?index.html?index.php
AddType?application/x-httpd-php?.php
配置文件中,有三條php相關的命令,需要逐一檢查。
還可以檢查下配置語法是否正確:[root@juispan?~]#?/usr/local/apache2.4/bin/apachectl?-t
Syntax?OK
然后,確認完全無誤后,重新嘗試打開測試頁面。