一、搭建過程
請參考百度經驗:
二、遇到的問題及解決辦法
2.1 問題一
如果是外網不可以ping能虛擬機,則說明網絡設置有問題,請參考:
在按照百度經驗中第三步,開啓Apache服務器時,出現下列錯誤:
httpd: Could not reliably determine the server’s fully qualified domain name, using localhost.localdomain for ServerName
2.2 問題一的解決方案:
- 用vim編輯器打開 httpd.conf
將裏面的 #ServerName localhost:80 註釋去掉即可。
# If your host doesn't have a registered DNS name, enter its IP address here.
# You will have to access it by its address anyway, and this will make
# redirections work in a sensible way.
#
ServerName localhost:80
#
# UseCanonicalName: Determines how Apache constructs self-referencing
# URLs and the SERVER_NAME and SERVER_PORT variables.
# When set "Off", Apache will use the Hostname and Port supplied
# by the client. When set "On", Apache will use the value of the
再重啓 httpd服務器
service httpd restart
查看是否存在網絡入口文件
如果存在index.php,或者是以.php文件結尾的文件,就可以進入第4步,如果沒有,則需要先創建。
[root@localhost ~]# ls /var/www/html/
index.php phpinfo.php
- 在WEB入口目錄(/var/www/html/)下創建.php文件
[root@localhost ~]# vi /var/www/html/index.php
<?php
echo "Hello world!";
?>
- 然後可以通過虛擬機訪問,輸入如下命令即可:
[root@localhost ~]# curl localhost:80
Hello world!
- 查看IP地址後,可以在本地通過瀏覽器訪問 http://192.168.137.79:80 ,如果頁面顯示 “Hello world!” ,即表示apache已安裝並啓動成功。
如果4和5都不能訪問服務器,則可以繼續往下看。
2.3 問題二:
主機與虛擬機互PING成功,但主機無法訪問虛擬機上啓動的web服務?
問題描述:
- 本機能ping通虛擬機
- 虛擬機也能ping通本機
- 虛擬機能訪問自己的web
- 本機無法訪問虛擬機的web
後來發現是防火牆將80端口屏蔽了的緣故。
檢查是不是服務器的80端口被防火牆堵了,可以通過命令:
telnet {服務器ip}80 來測試。
2.4 問題二的解決方法
將80端口打開,輸入如下命令:
/sbin/iptables -I INPUT -p tcp –dport 80 -j ACCEPT
然後保存:
/etc/rc.d/init.d/iptables save
重啓防火牆
/etc/init.d/iptables restart
查看CentOS防火牆信息:(可以看到開啓了80端口)
[root@localhost ~]# /etc/init.d/iptables status
表格:filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
- 或者可以直接關閉CentOS的防火牆,關閉其服務即可:
/etc/init.d/iptables stop
永久關閉防火牆:
chkconfig –level 35 iptables off
- 最後,打開主機瀏覽器,輸入虛擬機IP地址,就可以訪問虛擬機的WEB服務器了!