美文网首页技术篇
Apache HTTP Server安装

Apache HTTP Server安装

作者: 小六的昵称已被使用 | 来源:发表于2019-02-20 19:53 被阅读23次

    title: Apache HTTP Server安装
    categories: Linux
    tags:
    - Apache
    - httpd
    timezone: Asia/Shanghai
    date: 2019-01-12


    环境

    CentOS6/7
    REHL6/7
    
    # 源码方式安装
    [root@host66 bin]# ./httpd -v
    Server version: Apache/2.4.37 (Unix)
    Server built:   Jan  8 2019 09:15:52
    
    # CentOS7 1804(7.5) YUM方式安装(安装光盘)
    [root@localhost /]# httpd -v
    Server version: Apache/2.4.6 (CentOS)
    Server built:   Nov  5 2018 01:47:09
    
    # RHEL6.10 YUM方式安装(安装光盘)
    [root@redhat6 ~]# httpd -v
    Server version: Apache/2.2.15 (Unix)
    Server built:   Feb 19 2018 06:33:11
    
    # REHL 7.3 YUM方式安装(安装光盘)
    [root@localhost ~]# httpd -v
    Server version: Apache/2.4.6 (Red Hat Enterprise Linux)
    Server built:   Aug  3 2016 08:33:27
    

    方法1:源码编译安装

    1.配置本地yum并安装开发工具
    mkdir /mnt/cdrom
    mount /dev/cdrom /mnt/cdrom
    
    cat <<EOF >/etc/yum.repos.d/local.repo
    [local]
    name=local
    baseurl=file:///mnt/cdrom
    gpgcheck=0
    enabled=1
    EOF
    
    yum clean all       #清理本地缓存
    yum clean plugins   #清理插件缓存
    yum makecache       #构建缓存
    
    # 区分REHL6还是REHL7
    which systemctl && yum group install -y "Development Tools" || yum groupinstall -y "Development Tools"
    
    2.源码方式安装依赖项和Apache HTTP Server
    # 安装expat-devel(这里注意使用yum安装,rpm安装后报错没有找到原因)
    yum install expat-2.0.1-13.el6_8.x86_64.rpm expat-devel-2.0.1-13.el6_8.x86_64.rpm
    
    # 下载依赖包
    wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
    wget http://mirror.bit.edu.cn/apache//apr/apr-1.6.5.tar.gz
    wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.37.tar.gz
    wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
    
    # 安装pcre
    tar vxzf pcre-8.42.tar.gz
    cd pcre-8.42
    ./configure
    make
    make install
    
    # 这里为了保证每一步都正确安装使用echo $?命令查看命令执行结果是否有问题
    # 0代表每问题,非0都是有问题的
    tar -vxf apr-1.6.5.tar.gz
    cd apr-1.6.5
    ./configure --prefix=/usr/local/apr
    echo $?
    make
    echo $?
    make install
    echo $?
    
    
    tar -vxf apr-util-1.6.1.tar.gz
    cd apr-util-1.6.1
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
    echo $?
    make && make install && echo $?
    
    
    tar -vxf httpd-2.4.37.tar.gz
    cp -a apr-1.6.5 httpd-2.4.37/srclib/apr
    cp -a apr-util-1.6.1 httpd-2.4.37/srclib/apr-util/
    cd httpd-2.4.37
    ./configure --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
    echo $?
    make && make install && echo $?
    
    3.启动httpd
    默认安装路径:/usr/local/apache2/
    
    # 编辑配置文件增加以下行
    vim /usr/local/apache2/conf/httpd.conf
    ServerName 0.0.0.0:80
    
    # 启动httpd
    /usr/local/apache2/bin/apachectl -k start
        
        -k start        启动
        -k restart      重新启动
        -k graceful     优雅的重启(重读配置文件,如果配置文件有问题,将继续用原来配置文件运行)
        -k graceful-stop    优雅的停止
        -k stop         停止
    
    # 参数解析
    [root@host66 bin]# /usr/local/apache2/bin/apachectl -h
    Usage: /usr/local/apache2/bin/httpd [-D name] [-d directory] [-f file]
                                        [-C "directive"] [-c "directive"]
                                        [-k start|restart|graceful|graceful-stop|stop]
                                        [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X]
    Options:
      -D name            : define a name for use in <IfDefine name> directives
      -d directory       : specify an alternate initial ServerRoot
                         # 
      -f file            : specify an alternate ServerConfigFile
      -C "directive"     : process directive before reading config files
      -c "directive"     : process directive after reading config files
      -e level           : show startup errors of level (see LogLevel)
      -E file            : log startup errors to file
      -v                 : show version number
                         # 显示版本信息
      -V                 : show compile settings
                         # 显示编译设置
      -h                 : list available command line options (this page)
                         # 显示帮助信息
      -l                 : list compiled in modules
      -L                 : list available configuration directives
      -t -D DUMP_VHOSTS  : show parsed vhost settings
                         # 显示虚拟主机设置
      -t -D DUMP_RUN_CFG : show parsed run settings
                         # 显示已解析的运行设置
      -S                 : a synonym for -t -D DUMP_VHOSTS -D DUMP_RUN_CFG
                         # 等于-t -D DUMP_VHOSTS -D DUMP_RUN_CFG
      -t -D DUMP_MODULES : show all loaded modules 
                         # 显示所有加载模块
      -M                 : a synonym for -t -D DUMP_MODULES
                         # 等同于 -t -D DUMP_MODULES
      -t -D DUMP_INCLUDES: show all included configuration files
                         # 显示所有包含的配置文件
      -t                 : run syntax check for config files
                         # 检查配置文件
      -T                 : start without DocumentRoot(s) check
      -X                 : debug mode (only one worker, do not detach)
                         # debug模式(只启动一个进程)
    

    方法2:YUM方式安装

    1.CentOS7(配置文件路径:/etc/httpd/conf/httpd.conf)

    yum install -y httpd
    
    systemctl start httpd
    systemctl enable httpd
    systemctl status httpd
    

    2.REHL6(配置文件路径:/etc/httpd/conf/httpd.conf)

    mkdir /mnt/cdrom
    mount /dev/cdrom /mnt/cdrom
    
    cat <<EOF >/etc/yum.repos.d/local.repo
    [local]
    name=local
    baseurl=file:///mnt/cdrom
    gpgcheck=0
    enabled=1
    EOF
    
    yum clean all       #清理本地缓存
    yum clean plugins   #清理插件缓存
    yum makecache       #构建缓存
    
    yum install -y httpd
    
    service httpd start
    service httpd status
    chkconfig httpd on
    chkconfig --list httpd
    

    3.REHL7(配置文件路径:/etc/httpd/conf/httpd.conf)

    mkdir /mnt/cdrom
    mount /dev/cdrom /mnt/cdrom
    
    cat <<EOF >/etc/yum.repos.d/local.repo
    [local]
    name=local
    baseurl=file:///mnt/cdrom
    gpgcheck=0
    enabled=1
    EOF
    
    yum clean all       #清理本地缓存
    yum clean plugins   #清理插件缓存
    yum makecache       #构建缓存
    
    yum install -y httpd
    
    systemctl start httpd
    systemctl enable httpd
    systemctl status httpd
    

    附录:配置文件常用参数介绍

    # 监听端口
    Listen 80
    
    # 监听端口(多IP可以这么写)
    Listen 10.0.1.66:8080
    Listen 10.0.1.5:80
    
    # 监听服务器名字(上边的端口号优先级更高)
    ServerName 0.0.0.0:80
    
    # 默认主页(如果默认主页不存在,默认将以目录模式展示文件列表)
    <IfModule dir_module>
        DirectoryIndex 1.html
    </IfModule>
    
    # 禁用目录模式(去掉以下Indexes)
    Options Indexes FollowSymLinks
    

    附录:建立虚拟目录

    # 编辑配置文件增加以下行
    # 访问http://10.0.1.66/test的时候访问的是/home目录
    vim /usr/local/apache2/conf/httpd.conf
    
    Alias /test /home
    <Directory "/home">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    

    附录:建立虚拟主机

    1.基于端口的虚拟主机

    Listen 8080
    Listen 8090
    # 访问:http://10.0.1.66:8080访问到的是/usr/local/apache2/111目录
    <VirtualHost *:8080>
        ServerAdmin 111.com
        DocumentRoot "/usr/local/apache2/111"
        ErrorLog "logs/port80-error_log"
        CustomLog "logs/port80-access_log" common
    </VirtualHost>
    <Directory "/usr/local/apache2/111">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    # 访问:http://10.0.1.66:9090访问到的是/usr/local/apache2/222目录
    <VirtualHost *:8090>
        ServerAdmin 222.com
        DocumentRoot "/usr/local/apache2/222"
        ErrorLog "logs/port8080-error_log"
        CustomLog "logs/port8080-access_log" common
    </VirtualHost>
    <Directory "/usr/local/apache2/222">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    

    2.基于IP地址的虚拟主机

    Listen 80
    
    # 访问:http://10.0.1.66访问到的是/usr/local/apache2/111目录
    <VirtualHost 10.0.1.66:80>
        ServerAdmin 111.com
        DocumentRoot "/usr/local/apache2/111"
        ErrorLog "logs/port80-error_log"
        CustomLog "logs/port80-access_log" common
    </VirtualHost>
    <Directory "/usr/local/apache2/111">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    # 访问:http://10.0.1.5访问到的是/usr/local/apache2/222目录
    <VirtualHost 10.0.1.5:80>
        ServerAdmin 222.com
        DocumentRoot "/usr/local/apache2/222"
        ErrorLog "logs/port8080-error_log"
        CustomLog "logs/port8080-access_log" common
    </VirtualHost>
    <Directory "/usr/local/apache2/222">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    

    3.基于域名的虚拟主机

    Listen 80
    # 访问111.com访问到的是/usr/local/apache2/111目录
    <VirtualHost *:80>
        ServerAdmin 111.com
        ServerName 111.com
        DocumentRoot "/usr/local/apache2/111"
        ErrorLog "logs/port80-error_log"
        CustomLog "logs/port80-access_log" common
    </VirtualHost>
    <Directory "/usr/local/apache2/111">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    # 访问222.com访问到的是/usr/local/apache2/222目录
    <VirtualHost *:80>
        ServerAdmin 222.com
        ServerName 222.com
        DocumentRoot "/usr/local/apache2/222"
        ErrorLog "logs/port8080-error_log"
        CustomLog "logs/port8080-access_log" common
    </VirtualHost>
    <Directory "/usr/local/apache2/222">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    

    附录:

    Apache官网:http://httpd.apache.org/download.cgi

    pcre-8.42.tar.gz下载地址:https://pan.baidu.com/s/1a8BjWf7FW7pER0dgoh_nwg

    httpd-2.4.37.tar.gz下载地址:https://pan.baidu.com/s/17S2L9m79j6RWGr1x6CoGbw

    apr-util-1.6.1.tar.gz下载地址:https://pan.baidu.com/s/1IhYxCU9uuDtHdKSzg-P_kg

    apr-1.6.5.tar.gz下载地址:https://pan.baidu.com/s/1uNjTp2ob-Reurkyv4Cyubg

    相关文章

      网友评论

        本文标题:Apache HTTP Server安装

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