> CentOS > CentOS教程 >

CentOS7 最小化安装后,安装配置并启动httpd的步骤

记录供查阅

1 安装httpd软件包

 yum install httpd

 

针对CentOS7,httpd有专门的版本,此版本提供了针对systemd的单元文件,如下所示

 

-bash-4.2# rpm -ql httpd | grep service
/usr/lib/systemd/system/htcacheclean.service
/usr/lib/systemd/system/httpd.service

2 启动httpd服务并把此服务单元加入当前运行级

systemctl start httpd.service

systemctl enable httpd

此时,这个配置文件被链接到了/etc/systemd/system/multi-user.target.wants/httpd.service,如下图

-bash-4.2# ll /etc/systemd/system/multi-user.target.wants/
total 0
lrwxrwxrwx. 1 root root 38 Sep 16 19:08 auditd.service -> /usr/lib/systemd/system/auditd.service
lrwxrwxrwx. 1 root root 44 Sep 16 19:08 avahi-daemon.service -> /usr/lib/systemd/system/avahi-daemon.service
lrwxrwxrwx. 1 root root 37 Sep 16 19:08 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx. 1 root root 37 Sep 17 02:38 httpd.service -> /usr/lib/systemd/system/httpd.service
lrwxrwxrwx. 1 root root 42 Sep 16 19:08 irqbalance.service -> /usr/lib/systemd/system/irqbalance.service
lrwxrwxrwx. 1 root root 37 Sep 16 19:08 kdump.service -> /usr/lib/systemd/system/kdump.service
lrwxrwxrwx. 1 root root 46 Sep 16 19:08 NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx. 1 root root 39 Sep 16 19:08 postfix.service -> /usr/lib/systemd/system/postfix.service
lrwxrwxrwx. 1 root root 40 Sep 16 19:08 remote-fs.target -> /usr/lib/systemd/system/remote-fs.target
lrwxrwxrwx. 1 root root 39 Sep 16 19:08 rsyslog.service -> /usr/lib/systemd/system/rsyslog.service
lrwxrwxrwx. 1 root root 36 Sep 16 19:08 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx. 1 root root 37 Sep 16 19:08 tuned.service -> /usr/lib/systemd/system/tuned.service

3 打开firewalld防火墙的80端口

firewall-cmd --add-service=http                    (即时打开)

firewall-cmd --permanent --add-service=http  (写入配置文件)

此时,httpd这个服务添加到了/etc/firewalld/zones/public.xml这个zone配置文件中,所以firewalld才能够据此放行。此文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="dhcpv6-client"/>
  <service name="http"/>
  <service name="ssh"/>
  <service name="https"/>
</zone>

 

注意不要将此处firewalld管理的service与systemd中的sevice配置单元混淆,两者没有任何关系。

 

http服务是安装firewalld时自动安装的,这个服务的配置文件为 /usr/lib/firewalld/services/http.xml,我们来看看这个文件。

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>WWW (HTTP)</short>
  <description>HTTP is the protocol used to serve Web pages. If you plan to make your Web server publicly available, enable this option. This option is not required for viewing pages locally or developing Web pages.</description>
  <port protocol="tcp" port="80"/>
</service>

据此知道http服务对应的就是tcp协议的80端口。firewalld根据zone配置文件中的服务名http,依次在/etc/firwalld/services/目录,/usr/lib/firewalld/services/目录查找名为http.xml的文件,找到即停止继续查找,所以位于/etc/firwalld/services/目录的配置文件优先级更高。

 

4 与CentOS6.5的区别

总体思路是一致的,都是防火墙放行,把服务加入运行级配置文件。只是CentOS7中防火墙和运行级管理程序均发生彻底改变了。firewalld代替iptables,systemd代替SystemV init,所以需要重新熟悉相关命令。个人感觉新的程序更加人性化,操作也很容易。 配置文件也都采用了标准的xml格式,放弃了容易出错的老式配置文件。

(责任编辑:IT)