美文网首页
CentOS7 安装 httpd 服务

CentOS7 安装 httpd 服务

作者: Wonz | 来源:发表于2020-08-27 10:47 被阅读0次

安装 httpd 服务

输入下面命令安装 httpd 服务,注意要使用带有 root 权限 的用户,我是从普通用户切回超级用户 root 再开始执行命令的:

yum -y install httpd

报错:Cannot find a valid baseurl for repo: base/7/x86_6

上网查找问题,发现有三种原因:

  • 不能上网
  • DNS 配置有问题
  • 无法解析yum源

不能上网解决方法

查看网卡名字,输入命令:

ip addr

查看网卡名字,输入命令,注意不一定是 enp0s3:

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

按 i 表示要插入内容,把 ONBOOT=no,改为 ONBOOT=yes

image

保存退出,按 Esc,再输入下面命令:

:wq

发现还是不行。

DNS 配置问题解决方法

修改 DNS 配置

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

按 i 表示要插入内容,在文件末尾追加 DNS:

DNS1=8.8.8.8
DNS2=4.2.2.2
image

保存退出,按 Esc,再输入下面命令:

:wq

重启网络,输入命令:

ifup enp0s3

再输入安装 httpd 命令

yum -y install httpd

发现开始安装了,等待几分钟,安装成功。

image

访问 IP 地址

启动 apache 服务器:

systemctl start httpd

设置开机自动启动:

systemctl enable httpd

检查 httpd 服务状态:

systemctl status httpd.service
image

看到绿色的 active(running) 表示正常运行,查看 IP 地址:

ip add
image

然后输入下面命令:

curl 127.0.0.1

发现访问不了,这一般是防火墙的问题,我们需要设置一下,允许访问:

service iptables stop

发现提示没有安装 iptables,需要安装一下:

yum install iptables-services 

然后输入下面命令:

systemctl stop iptables && systemctl disable iptables

其实 CentOS 从 7 开始默认用的是 firewalld,这个是基于 iptables 的,虽然有 iptables 的核心,但是 iptables 的服务是没安装的。所以只要停止 firewalld 服务即可:

systemctl stop firewalld.service && systemctl disable firewalld.service

然后再输入:

curl 127.0.0.1
image

发现可以看到 HTML 代码,表示可以访问该 IP 地址。

参考

Linux系统CentOS 7配置httpd服务

CentOS 7 安装报错:Cannot find a valid baseurl for repo: base/7/x86_6

CentOS7关闭/开启防火墙出现 Unit iptables.service failed to load

相关文章

网友评论

      本文标题:CentOS7 安装 httpd 服务

      本文链接:https://www.haomeiwen.com/subject/tcccsktx.html