CentOS下搭建LAMP環境及遇到的問題

一、搭建過程

請參考百度經驗:

Centos系統下Lamp環境的快速搭建(超詳細)

二、遇到的問題及解決辦法

2.1 問題一

如果是外網不可以ping能虛擬機,則說明網絡設置有問題,請參考:

VirtualBox安裝CentOS網絡設置(DHCP)

在按照百度經驗中第三步,開啓Apache服務器時,出現下列錯誤:

httpd: Could not reliably determine the server’s fully qualified domain name, using localhost.localdomain for ServerName

2.2 問題一的解決方案:

  1. 用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
  1. 再重啓 httpd服務器

    service httpd restart

  2. 查看是否存在網絡入口文件
    如果存在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!";

?>
  1. 然後可以通過虛擬機訪問,輸入如下命令即可:
[root@localhost ~]# curl localhost:80
Hello world!
  1. 查看IP地址後,可以在本地通過瀏覽器訪問 http://192.168.137.79:80 ,如果頁面顯示 “Hello world!” ,即表示apache已安裝並啓動成功。

如果4和5都不能訪問服務器,則可以繼續往下看。

2.3 問題二:

主機與虛擬機互PING成功,但主機無法訪問虛擬機上啓動的web服務?

問題描述:

  1. 本機能ping通虛擬機
  2. 虛擬機也能ping通本機
  3. 虛擬機能訪問自己的web
  4. 本機無法訪問虛擬機的web

後來發現是防火牆將80端口屏蔽了的緣故。
檢查是不是服務器的80端口被防火牆堵了,可以通過命令:
telnet {服務器ip}80 來測試。

2.4 問題二的解決方法

  1. 將80端口打開,輸入如下命令:

    /sbin/iptables -I INPUT -p tcp –dport 80 -j ACCEPT

  2. 然後保存:

    /etc/rc.d/init.d/iptables save

  3. 重啓防火牆

    /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 
  1. 或者可以直接關閉CentOS的防火牆,關閉其服務即可:

    /etc/init.d/iptables stop


永久關閉防火牆:
chkconfig –level 35 iptables off
  1. 最後,打開主機瀏覽器,輸入虛擬機IP地址,就可以訪問虛擬機的WEB服務器了!
發佈了104 篇原創文章 · 獲贊 108 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章