1. 程式人生 > >httpd中使用php模塊解析php網頁

httpd中使用php模塊解析php網頁

下使用 關聯 servers 配置參數 筆記本 .gz uri hat gist

在httpd配置文件中添加php解析

修改apache的配置文件,讓apache可以使用php進行解析
httpd的配置文件在/usr/local/httpd/conf/httpd.conf路徑
在httpd配置文件中修改如下幾個配置參數,在httpd.conf配置文件中查找一下關鍵字符配置

[root@localhost httpd]# vim conf/httpd.conf
~

這裏安裝了兩個php模塊,需要註釋掉一個php模塊。因為httpd不能同時加載兩個php模塊
LoadModule php5_module modules/libphp5.so ? ? ?
#LoadModule php7_module modules/libphp7.so
將ServerName行的註釋去掉,這樣可以使httpd能夠正常啟動和訪問默認頁
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn‘t have a registered DNS name, enter its IP address here.
ServerName www.example.com:80

修改默認的目錄策略,denied表示拒絕訪問網頁目錄,granted表示允許訪問網頁目錄,防止打開虛擬主機配置會顯示網頁報錯狀態碼403
# Deny access to the entirety of your server‘s filesystem. You must
# explicitly permit access to web content directories in other 
# <Directory> blocks below.
#
<Directory />
 ?  AllowOverride none
 ?  Require all granted
</Directory>

網頁根目錄的配置路徑及權限配置,Require all  granted這裏配置和上面的目錄訪問有關聯的作用,表示網頁目錄是否能夠有權限訪問
DocumentRoot "/usr/local/httpd/htdocs"
<Directory "/usr/local/httpd/htdocs">
 ?  Options Indexes FollowSymLinks
 ? ?# AllowOverride controls what directives may be placed in .htaccess files.
 ? ?# It can be "All", "None", or any combination of the keywords:
 ? ?# ? AllowOverride FileInfo AuthConfig Limit
 ?  AllowOverride None
 ? ?# Controls who can get stuff from this server.
 ?  Require all granted
</Directory>

添加php的解析主頁,添加php的解析主頁,在訪問時才能對php頁面正確識別
<IfModule dir_module>
 ?  DirectoryIndex index.html index.php
</IfModule>

添加php解析的配置項,httpd中必須添加解析配置項才能讓httpd的php解析請求發送到php模塊進行解析
 ? ?# If the AddEncoding directives above are commented-out, then you
 ? ?# probably should define those extensions to indicate media types:
 ? ?#
 ?  AddType application/x-compress .Z
 ?  AddType application/x-gzip .gz .tgz
 ?  AddType application/x-httpd-php .php

檢查httpd配置文件是否正確,測試配置內容,這裏註意的是一個使用習慣,在修改配置文件後首先檢查配置文件中是否有錯誤,如果配置文件中配置有誤而不檢查直接重新啟動服務,會造成服務啟動時的報錯服務啟動終止,導致服務對外停止提供訪問,這樣會造成短時間內服務無法訪問的情況,需要檢查配置文件後並重啟服務,使用graceful對服務重新加載配置即可

[root@localhost httpd]# /usr/local/httpd/bin/apachectl -t
Syntax OK
[root@localhost httpd]# /usr/local/httpd/bin/apachectl graceful
[root@localhost httpd]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address ? ? ? ? ? Foreign Address ? ? ? ? State ? ? ? PID/Program name ? ?
tcp ? ? ? ?0 ? ? ?0 0.0.0.0:22 ? ? ? ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ? ?1085/sshd ? 
tcp ? ? ? ?0 ? ? ?0 127.0.0.1:25 ? ? ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ? ?1388/master 
tcp6 ? ? ? 0 ? ? ?0 :::3306 ? ? ? ? ? ? ? ? :::* ? ? ? ? ? ? ? ? ?  LISTEN ? ? ?8759/mysqld 
tcp6 ? ? ? 0 ? ? ?0 :::80 ? ? ? ? ? ? ? ? ? :::* ? ? ? ? ? ? ? ? ?  LISTEN ? ? ?873/httpd ? 
tcp6 ? ? ? 0 ? ? ?0 :::22 ? ? ? ? ? ? ? ? ? :::* ? ? ? ? ? ? ? ? ?  LISTEN ? ? ?1085/sshd ? 
tcp6 ? ? ? 0 ? ? ?0 ::1:25 

httpd的php解析,在httpd的網頁目錄下寫一個php測試的文件內容

[root@localhost httpd]# cat htdocs/index.php

<?php
phpinfo();
?>

php頁面正確解析訪問結果示例:

httpd加載了php模塊,並返回網頁報頭信息,響應的服務端解析程序是PHP/5.6.37

[root@localhost httpd]# curl -I 192.168.1.233/index.php
HTTP/1.1 200 OK
Date: Sun, 29 Jul 2018 09:22:59 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.37
X-Powered-By: PHP/5.6.37
Content-Type: text/html; charset=UTF-8

如果寫了php測試頁但無法顯示php的測試頁面,需要從下面幾個方面檢查一遍:
php無法解析,首先檢查是否有php模塊,是否在httpd配置文件中有php的加載配置參數,是否寫入解析php的配置參數,測試時是否指定首頁的索引解析頁

 [root@localhost httpd]# /usr/local/httpd/bin/apachectl -M
Loaded Modules:
 core_module (static)
 so_module (static)
-----------------------------------略-------------------------
 alias_module (shared)
 php5_module (shared)
[root@localhost httpd]# cat conf/httpd.conf |grep php.*so
LoadModule php5_module modules/libphp5.so

[root@localhost httpd]# cat conf/httpd.conf |grep AddType
 ? ?# AddType allows you to add to or override the MIME configuration
 ? ?#AddType application/x-gzip .tgz
 ?  AddType application/x-compress .Z
 ?  AddType application/x-gzip .gz .tgz
 ?  AddType application/x-httpd-php .php
 ? ?

[root@localhost httpd]# cat conf/httpd.conf |grep index.php
 ?  DirectoryIndex index.html index.php

httpd虛擬主機配置
關於虛擬主機:
一臺服務器可以運行多個網站,一個主機多個域名指定不同的網站,每個網站都是一個虛擬主機

修改windows的解析文件,這裏只作為測試使用,修改的文件路徑在C:\Windows\System32\drivers\etc\ ?,用筆記本打開HOSTS文件,寫法格式為ip ?域名 ?域名 ;可以指定一個ip有多個域名
在客戶端訪問沒有配置過的默認虛擬主機的httpd,解析任何的域名的ip如果指向該服務器的ip地址會直接訪問到該httpd的默認主機上


配置httpd的虛擬主機,打開虛擬主機配置文件,修改conf/httpd.conf配置文件,去掉註釋或添加:

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

打開虛擬主機配置後,原來的主配置文件中定義的網站配置會失效停止訪問,生效的是虛擬主機配置文件中的設置。虛擬主機配置項如下示例:

[root@localhost httpd]# cat conf/extra/httpd-vhosts.conf 
# Virtual Hosts
#
# Required modules: mod_log_config
-------------------------------省略---------------------------------------
<VirtualHost *:80>
 ?  DocumentRoot "/usr/local/httpd/docs/abcd.com" ? ? <---網站網頁加載路徑
 ?  ServerName abcd.com ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <---解析的虛擬主機名
 ?  ServerAlias www.abcd.com ? ? ? ? ? ? ? ? ? ? ? ?  <---虛擬主機域名
 ?  ErrorLog "logs/abcd.com-error_log" ? ? ? ? ? ? ?  <---虛擬主機錯誤日誌
 ?  CustomLog "logs/abcd.com-access_log" common ? ? ? <---虛擬主機訪問日誌
</VirtualHost>
<VirtualHost *:80>
 ?  DocumentRoot "/usr/local/httpd/docs/123.com"
 ?  ServerName 123.com
 ?  ServerAlias www.123.com
 ?  ErrorLog "logs/123.com-error_log"
 ?  CustomLog "logs/123.com-access_log" common
</VirtualHost>

創建虛擬主機的網頁存儲目錄,並使用apachectl檢查一下配置文件,然後重新加載httpd配置

[root@localhost httpd]# mkdir -p /usr/local/httpd/docs/abcd.com
[root@localhost httpd]# mkdir -p /usr/local/httpd/docs/123.com
[root@localhost httpd]# /usr/local/httpd/bin/apachectl -t
Syntax OK
[root@localhost httpd]# /usr/local/httpd/bin/apachectl graceful

在windows端和linux端簡要的認證一下虛擬主機訪問結果:
技術分享圖片

linux下使用curl命令訪問web網頁的內容,結果如下:

[root@localhost abcd.com]# curl -x192.168.1.233:80 abcd.com
abcd.com
[root@localhost abcd.com]# curl -x192.168.1.233:80 123.com
123.com

httpd中使用php模塊解析php網頁