您的位置:首页 > 运维架构 > Linux

CentOS下搭建LAMP环境及遇到的问题

2017-05-25 12:05 387 查看

一、搭建过程

请参考百度经验:

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 问题一的解决方案:

用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服务器了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux