Apache和PHP結合,httpd的虛擬主機配置
阿新 • • 發佈:2018-11-25
httpd配置支援php
上次安裝httpd2.4對應的配置檔案:/usr/local/apache2.4/conf/httpd.conf
編輯配置檔案,修改以下4個地方
ServerName
Require all denied
AddType application/x-httpd-php .php
DirectoryIndex index.html index.php
- 編輯配置檔案,去掉ServerName前面的#,啟動httpd
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl start httpd (pid 6328) already running
- 防火牆開啟80埠,測試訪問
# Windows終端: telnet 192.168.77.134 80
正在連線192.168.77.134...無法開啟到主機的連線。 在埠 80: 連線失敗
[[email protected] ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
# 再次telnet可以訪問了
- 如果開啟80埠,訪問出現403錯誤,編輯配置把Require all denied 改為 Require all granted
# 改完配置需要重新載入配置,載入前最好檢測配置是否正確
[ [email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
# 載入配置
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful
- 配置支援PHP,搜AddType,在"AddType application/x-gzip .gz .tgz"這行下面新增"AddType application/x-httpd-php .php"
- 增加索引頁,搜尋DirectoryIndex,找到如下內容,增加了index.php
<IfModule dir_module> DirectoryIndex index.html index.php </IfModule>
- 在目錄/usr/local/apache2.4/htdocs/下編寫php檔案,嘗試訪問,例如1.php,訪問http:ip/1.php
httpd的預設虛擬主機
- 一臺伺服器可以訪問多個網站,每個網站都是一個虛擬主機
- 概念:域名(主機名)、DNS、解析域名、hosts 任何一個域名解析到這臺機器,都可以訪問的虛擬主機就是預設虛擬主機
vim /usr/local/apache2/conf/httpd.conf # 搜尋httpd-vhost,去掉#,使支援虛擬主機
vim /usr/local/apache2/conf/extra/httpd-vhosts.conf # 改為如下
<VirtualHost *:80>
ServerAdmin [email protected] # 管理員郵箱,可以不用配置
DocumentRoot "/tmp/web-default" # 網站資源目錄
ServerName test.com # 域名
ServerAlias www.test.com # 域名別名
ErrorLog "logs/test.com-error_log" # 錯誤日誌
CustomLog "logs/test.com-access_log" common # 訪問日誌
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/tmp/web-1/"
ServerName www.123.com
</VirtualHost>
[[email protected] apache2.4]# bin/apachectl -t
Syntax OK
[[email protected] apache2.4]# bin/apachectl graceful
編寫測試檔案1.php放到/tmp/web-default目錄下,2.php放到/tmp/web-1/下, 訪問測試
[[email protected] apache2.4]# curl -x 192.168.77.134:80 test.com
1.php
[[email protected] apache2.4]# curl -x 192.168.77.134:80 www.test.com
1.php
[[email protected] apache2.4]# curl -x 192.168.77.134:80 www.123.com
2.php
[[email protected] apache2.4]# curl -x 192.168.77.134:80 www.example.com # 訪問預設虛擬主機
1.php