美文网首页
Linux(Centos7)安装apache服务器

Linux(Centos7)安装apache服务器

作者: Impact | 来源:发表于2017-07-07 21:38 被阅读0次

    Centos安装apache服务器

    概述

    Apache HTTP Server ("httpd"),一款开源的HTTP服务器,官网地址:Apache HTTP Server Project。因需要提供文件的下载功能,简单搭建了这个服务器,基本满足需求。

    安装前

    系统信息:

    [root@localhost ~]# uname -a
    Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
    

    检测是否安装过,我机器上已经安装过,如下:

    [root@localhost /]# rpm -qa | grep httpd
    httpd-tools-2.4.6-45.el7.centos.4.x86_64
    httpd-2.4.6-45.el7.centos.4.x86_64
    httpd-devel-2.4.6-45.el7.centos.4.x86_64
    

    安装过程:

    yum 安装 httpd

    yum install httpd-devel.x86_64

    提示完成后,修改配置文件:

    修改配置

    vim /etc/httpd/conf/httpd.conf

    ServerName 192.168.52.131:8989
    
    Listen 8989
    
    DocumentRoot "/home/doc"
    
    <Directory "/home/doc">
        AllowOverride None
    
        Require all granted
    </Directory>
    
    <Directory "/home/doc/com">
    
        Options Indexes FollowSymLinks
    
        AllowOverride None
    
        Require all granted
    </Directory>
    
    

    其中 ServerName 要写成访问的域名或IP地址,再加上端口; DocumentRoot 是访问的根目录,需要对文件做不同的分类放置的话,需要单独配置,需要将 denied 改成 granted ;如果不需要 conf.d 中的配置,可以将 IncludeOptional conf.d/*.conf 注释掉;还有就是端口不要冲突。

    启动服务

    systemctl restart httpd.service

    [root@localhost conf]# systemctl restart httpd.service 
    Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.
    
    

    可能在启动时会有问题,根据提示 journalctl -xe 查看错误信息,可能是配置的问题。

    如果在启动后,依然无法访问,那么就有可能是防火墙的问题。

    防火墙配置

    添加允许 http 访问:

    firewall-cmd --add-service=http

    [root@localhost home]# firewall-cmd --add-service=http
    FirewallD is not running
    
    [root@localhost home]# systemctl start firewalld
    Failed to start firewalld.service: Unit firewalld.service is masked.
    
    [root@localhost home]# systemctl unmask firewalld.service 
    Removed symlink /etc/systemd/system/firewalld.service.
    
    [root@localhost home]# firewall-cmd --add-service=http
    success
    

    端口开放

    将需要的端口开放:

    firewall-cmd --permanent --add-port=8989/tcp

    [root@localhost home]# firewall-cmd --permanent --add-port=8989/tcp
    success
    

    重启服务

    配置好之后,再重启防火墙:

    firewall-cmd --reload
    

    成功之后,在重启 httpd 服务并添加开机自启动:

    [root@localhost home]# systemctl restart httpd.service 
    [root@localhost home]# systemctl enable httpd.service 
    Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
    

    在浏览器地址栏输入域名或IP地址加上端口号,可以访问配置的根目录则表示成功。

    总结

    关键步骤

    1. 安装:

    yum install httpd-devel.x86_64

    1. 配置:

    vim /etc/httpd/conf/httpd.conf

    1. 启动/重启:

    systemctl start/restart httpd.service

    1. 防火墙允许http访问:

    firewall-cmd --add-service=http

    1. 防火墙端口开放

    firewall-cmd --permanent --add-port=8989/tcp

    1. 防火墙重启

    firewall-cmd --reload

    问题

    1. 配置文件httpd.conf的配置项需要仔细,包括端口、文件目录等,配置错误会导致无法成功;
    2. 防火墙的策略需要检测;
    3. 系统相关,例如我用的Centos 7和6的配置就有所区别。

    相关文章

      网友评论

          本文标题:Linux(Centos7)安装apache服务器

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