[toc]php
[root@xavi ~]# vim /usr/local/apache2.4/conf/httpd.conf
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl restart AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::1d7a:ffe8:8235:a2a5. Set the 'ServerName' directive globally to suppress this message httpd not running, trying to start /usr/local/apache2.4/bin/apachectl: 行 79: 2759 段錯誤 (吐核)$HTTPD -k $ARGV
fg返回上一條命令html
修改它的目的是,容許全部請求,不然咱們訪問時回報403錯誤。web
[root@xavi ~]# fg vim /usr/local/apache2.4/conf/httpd.conf
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl restart httpd not running, trying to start /usr/local/apache2.4/bin/apachectl: 行 79: 2827 段錯誤 (吐核)$HTTPD -k $ARGV
回到vim /usr/local/apache2.4/conf/httpd.conf,找到79行,把句首加上#,可是任然報錯,想起文檔最後啓用了兩個PHP,應該禁止一個apache
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl restart httpd not running, trying to start
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl stop [root@xavi ~]# vim /usr/local/apache2.4/conf/httpd.conf [root@xavi ~]# /usr/local/apache2.4/bin/apachectl start
如下說明已經啓動成功vim
[root@xavi ~]# netstat -lnp |grep httpd tcp6 0 0 :::80 :::* LISTEN 3163/httpd
此處暫停,不成功,有可能和以前課程裏的iptables設置有關係。windows
[root@xavi htdocs]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT [root@xavi htdocs]# iptables -nvL Chain INPUT (policy ACCEPT 82 packets, 15752 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 0 0 ACCEPT udp -- virbr0 * 0.0.0.0/0 0.0.0.0/0 udp dpt:53 0 0 ACCEPT tcp -- virbr0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53 0 0 ACCEPT udp -- virbr0 * 0.0.0.0/0 0.0.0.0/0 udp dpt:67 0 0 ACCEPT tcp -- virbr0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:67 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- * virbr0 0.0.0.0/0 192.168.122.0/24 ctstate RELATED,ESTABLISHED 0 0 ACCEPT all -- virbr0 * 192.168.122.0/24 0.0.0.0/0 0 0 ACCEPT all -- virbr0 virbr0 0.0.0.0/0 0.0.0.0/0 0 0 REJECT all -- * virbr0 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable 0 0 REJECT all -- virbr0 * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain OUTPUT (policy ACCEPT 83 packets, 16131 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT udp -- * virbr0 0.0.0.0/0 0.0.0.0/0 udp dpt:68
<Directory /> AllowOverride none Require all denied </Directory> <Directory /> AllowOverride none Require all granted </Directory>
AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType application/x-httpd-php .php
<IfModule dir_module> DirectoryIndex index.html </IfModule> <IfModule dir_module> DirectoryIndex index.html index.php </IfModule>
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK
正確的則顯示爲 「Syntax OK」,不然繼續檢查修改httpd配置文件。瀏覽器
編寫一個測試腳本,以下:服務器
[root@xavi ~]# vim /usr/local/apache2.4/htdocs/test.php //寫入以下內容 <?php phpinfo(); //打印php的全部函數 ?>
[root@xavi ~]# vim /usr/local/apache2.4/conf/httpd.conf [root@xavi ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@xavi ~]# /usr/local/apache2.4/bin/apachectl graceful
只能顯示源代碼了,沒法解析 php7
/usr/local/apache2.4/modules/libphp5.so架構
不少php架構的網站其網址後綴都有index.php
[root@xavi ~]# ls /usr/local/apache2.4/htdocs/ index.html test.php
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@xavi ~]# /usr/local/apache2.4/bin/apachectl graceful
一臺服務器能夠訪問多個網站,每一個網站都是一個虛擬主機 概念:域名(主機名)、DNS、解析域名、hosts 任何一個域名解析到這臺機器,均可以訪問的虛擬主機就是默認虛擬主機
hosts文件是一個沒有擴展名的系統文件,它的主要做用是能加快域名解析,還能夠屏蔽網站等。 windows的hosts
注意這個文件必定是在系統盤,若是你的系統在D盤請自行修改前面的盤符。以下圖所示
在hosts下增長一下代碼
把剛纔編輯的ip和網站都用#號註釋掉,ping網址時顯示的是公網的ip地址,而不是剛纔虛擬試驗機的ip
這裏須要注意的是打開httpd服務:
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl start
一樣也可訪問test.php
[root@xavi ~]# vim /usr/local/apache2.4/conf/httpd.conf
今天再從新看筆記的時候發現到這裏錯了:昨天的報錯就有httpd-vhosts,原來本身把#去除行選錯了,選錯httpd-manual.conf文件了
AH00526: Syntax error on line 36 of /usr/local/apache2.4/conf/extra/httpd-manual.conf:
[root@xavi ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
將其內容編輯爲:
<VirtualHost *:80> //每一個VirtualHost表明一個主機,一個主機表明一個網站 #ServerAdmin webmaster@dummy-host.example.com //定義管理員的郵箱,做用不大能夠刪除 DocumentRoot "/data/wwwroot/abc.com" //指定網站的根目錄在哪 ServerName abcd.com //定義網站名稱,只能寫一個 ServerAlias www.abcd.com www.123.com //設置網站別名,支持多個網站 ErrorLog "logs/abcd.com-error_log" //錯誤日誌,保存和虛擬主機網站同樣的名字方便讀取 CustomLog "logs/abcd.com-error_log" common </VirtualHost>
<VirtualHost *:80> DocumentRoot "/data/wwwroot/abcd.com" ServerName abcd.com ServerAlias www.abcd.com www.123.com ErrorLog "logs/abcd.com-error_log" CustomLog "logs/abcd.com-access_log" common </VirtualHost> <VirtualHost *:80> DocumentRoot "/data/wwwroot/xavi.com" ServerName xavi.com iServerAlias www.example.com ErrorLog "logs/xavi.com-error_log" CustomLog "logs/xavi.com-access_log" common </VirtualHost>
檢查錯誤:
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK
[root@xavi ~]# mkdir /data/wwwroot/ [root@xavi ~]# mkdir /data/wwwroot/abcd.com [root@xavi ~]# mkdir /data/wwwroot/xavi.com
[root@xavi ~]# vim /data/wwwroot/abcd.com/index.php <?php echo "abcd.com"; php?>
也可簡單編輯php代碼,不加結尾
[root@xavi ~]# vim /data/wwwroot/abcd.com/index.php [root@xavi ~]# vim /data/wwwroot/xavi.com/index.php <?php echo "xavi.com"; [root@xavi ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl graceful httpd not running, trying to start
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl start httpd (pid 7427) already running [root@xavi ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@xavi ~]# curl -x192.168.72.130:80 abcd.com <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>403 Forbidden</title> </head><body> <h1>Forbidden</h1> <p>You don't have permission to access / on this server.<br /> </p> </body></html>
想了好久,網上也沒找打答案,想起403錯誤在上一章節提起過,查知道本身在httpd配置測試時該了一個地方,以下圖:
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl stop [root@xavi ~]# vim /usr/local/apache2.4/conf/httpd.conf [root@xavi ~]# /usr/local/apache2.4/bin/apachectl start [root@xavi ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@xavi ~]# /usr/local/apache2.4/bin/apachectl graceful [root@xavi ~]# curl -x192.168.72.130:80 abcd.com this is a test[root@xavi ~]#
[root@xavi ~]# /usr/local/apache2.4/bin/apachectl stop [root@xavi ~]# vim /usr/local/apache2.4/conf/httpd.conf [root@xavi ~]# /usr/local/apache2.4/bin/apachectl start [root@xavi ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@xavi ~]# /usr/local/apache2.4/bin/apachectl graceful [root@xavi ~]# curl -x192.168.72.130:80 abcd.com this is a test[root@xavi ~]#
注意: 虛擬主機生效後,即Virtual hosts生效,原Apache配置文件中的默認主機(www.example.com 192.168.72.130)就會失效