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

頭像是我偶像發表於2017-05-25

一、搭建過程

請參考百度經驗:

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伺服器了!

相關文章