美文网首页我爱编程
Apache源码编译安装及优化

Apache源码编译安装及优化

作者: Hi_One | 来源:发表于2017-09-05 13:08 被阅读326次

    下载httpd源码包

    wget http://archive.apache.org/dist/httpd/httpd-2.2.25.tar.gz
    tar -xvf httpd-2.2.25.tar.gz
    cd httpd-2.2.25.tar.gz
    

    版本信息的屏蔽

    1.编译前,屏蔽版本信息
    vim include/ap_release.h
    #define AP_SERVER_BASEVENDOR "Apache Software Foundation" #服务的供应商名称
    #define AP_SERVER_BASEPROJECT "Apache HTTP Server" #服务的项目名称
    #define AP_SERVER_BASEPRODUCT "Apache" #服务的产品名
    #define AP_SERVER_MAJORVERSION_NUMBER 2 #主要版本号
    #define AP_SERVER_MINORVERSION_NUMBER 4 #小版本号
    #define AP_SERVER_PATCHLEVEL_NUMBER 6 #补丁级别
    #define AP_SERVER_DEVBUILD_BOOLEAN 0 #
    
    2.对apache进行编译安装
    yum install openssl*
    ./configure --prefix=/usr/local/apache2.2.25/ --enable-so --enable-rewrite --enable-ssl --with-mpm=worker
    

    配置参数用途

    --perfix=/usr/local/apache2.2.25        指定安装路径
    --enable-so                     支持动态加载模块
    --enable-rewrite                    支持网站地址重写
    --enable-ssl                        支持ssl加密
    --with-mpm=worker                   更改Apache运行模式为
    
    make -j 4 && make install 安装
    
    3.再次隐藏服务信息
    vim /usr/local/apache2.2.25/conf/httpd.conf
    Include conf/extra/httpd-default.conf   取消注释
    
    vim /usr/local/apache2.2.25/conf/extra/httpd-default.conf
    ServerTokens Full ===》》ServerTokens Prod        不显示服务器操作系统类型
    ServerSignature On ===》》ServerSignature OFF  不显示web服务版本号和模块版本
    
    4.启动apache
    cp /usr/local/apache2.2.25/bin/apachectl /etc/init.d/apachectl
    /etc/init.d/apachectl -k start
    重载
    /etc/init.d/apachectl -k gracefu
    

    长连接功能

    优点:保持连接,减少三次握手,但是会小号内存

    1.开启长连接
    vim /usr/local/apache2.2.25/conf/httpd.conf
    在Lient
    KeepAlive on
    KeepAliveTimeout 15     #保持长连接的时长
    #MaxKeepAliveRequests       #保持长连接后允许发送的请求数,一般不需要配置,默认100
    

    压缩功能

    1.查看模块时候安装
    /usr/local/apache2.2.25/bin/apachectl -M | grep defalte
    Syntax OK 没有安装
    defalte_module(static)  编译时已安装
    defalte_module(shared)  以DSO的方式安装
    

    DSO安装方法:

    cd /root/httpd-2.2.25/modules/filters
    /usr/local/apache2.2.25/bin/apxs -c -i -a /root/httpd-2.2.25/modules/filters/mod_deflate.c
    安装完成后http.conf文件 54行左右会有
    LoadModule deflate_module modules/mod_deflate.so
    以下配置文件在这行下添加
    
    2.配置压缩模块
    vim /usr/local/apache2.2.25/conf/httpd.conf
    <ifModule mod_deflate.c>
    DeflateCompressionLevel 9       压缩等级,越大效率越高,消耗CPU也越高
    SetOutputFilter DEFLATE
    DeflateFilterNote Input instream
    DeflateFilterNote Output outstream
    DeflateFilterNote Ratio ratio
    AddOutputFilterByType   DEFLATE text/html text/plain text/xml text/css application/javascript
    #限制特定的MIME类型文件
    </ifmodule>
    

    对apache进行重启

    /usr/local/apache2.2.25/bin/httpd -k graceful       重新加载配置文件
    

    缓存功能

    1.DSO安装
    /usr/local/apache2.2.25/bin/apxs -i -c -a /root/httpd-2.2.25/modules/metadata/mod_expires.c
    
    2.修改配置文件httpd.conf

    使用
    对全局 对目录 对虚拟机

    vim /usr/local/apache2.2.25/conf/httpd.conf
    <IfModule !mpm_winnt_module>
    <IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault "access plus 12 month"
    ExpiresByType text/html "access plus 12 months"
    ExpiresByType text/css "access plus 12 months"
    ExpiresByType image/gif "access plus 12 months"
    ExpiresByType image/jpeg "access plus 12 months"
    ExpiresByType image/jpg "access plus 12 months"
    ExpiresByType image/png "access plus 12 months"
    EXpiresByType application/x-shockwave-flash "access plus 12 months"
    EXpiresByType application/x-javascript "access plus 12 months"
    ExpiresByType video/x-flv "access plus 12 months"
    </IfModule>
    

    对apache进行重启

    /etc/init.d/apachectl graceful      重新加载配置文件
    

    Apache运行模式worker

    1.查看apache是否运行work模式
    /usr/local/apache2.2.25/bin/httpd -l | grep work
    
    2.修改配置文件
    vim /usr/local/apache2.2.25/conf/httpd.conf
    最后一行添加
    <IfModule worker.c>
    StartServers 2                  最初建立的子进程
    MaxClients 150              apache同时支持的并发量
    MinSpareThreads 25          最小空闲线程,如果线程小于这个值,自动创建线程
    MaxSpareThreads 75          最大线程数,如果县城大于这个值,自动kill掉多余线程
    ThreadsPerChild 25              每个子迚程包含固定的线程数
    MaxRequestsPerChild 0           每个子迚程可以支持的请求数,这要设置为 0
    </IfModule>
    

    相关文章

      网友评论

        本文标题:Apache源码编译安装及优化

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