zoukankan      html  css  js  c++  java
  • Apache服务搭建

    httpd,服务                                                                                      
        网站访问基本流程:                                                                                 
                        有则返回                LDNS            网站                                    
            用户----->本地的缓存/host文件---->local dns------->web sit                                     
            |<===========|                        |                 |                             
            |<====================================|                 |                             
            |<======================================================|                             
                                                                                                  
    lamp系列 http服务                                                                                 
    服务排行                                                                                          
        apache  老大哥,安全稳定web服务器                                                                    
        mcrosoft-iis                                                                              
        nginx   新型主流的web服务器,适合静态网页                                                                
        tomcat  java容器                                                                            
        resin                                                                                     
                                                                                                  
    Apache,主流的,安全稳定的web服务端软件                                                                      
    查看网站是否正常:                                                                                     
            curl -I -s IPaddr   //返回(状态码) ok 则正常                                                  
                                                                                                  
    ------------------------------------------------------------------------                      
    隐藏Apache版本号的方法是修改Apache的配置文件,如RedHat系的Linux默认是:                                               
    vim /etc/httpd/conf/httpd.conf                                                                
    分别搜索关键字ServerTokens和ServerSignature,修改:                                                       
        ServerTokens OS 修改为 ServerTokens ProductOnly                                              
        ServerSignature On 修改为 ServerSignature Off                                                
                                                                                                  
    重启或重新加载Apache就可以了。                                                                            
    apachectl restart                                                                             
    测试一下,如下:                                                                                      
        [root@localhost ~]# curl -I 192.168.80.128:88                                             
        HTTP/1.1 403 Forbidden                                                                    
        Date: Wed, 21 Jul 2010 13:23:22 GMT                                                       
        Server: Apache                                                                            
        Accept-Ranges: bytes                                                                      
        Content-Length: 5043                                                                      
        Connection: close                                                                         
        Content-Type: text/html; charset=UTF-8                                                    
    版本号与操作系统信息已经隐藏了。                                                                              
    --------------------------------------------------------------------                          
    如果是编译安装的,还可以用修改源码编译的方法:                                                                       
    进入Apache的源码目录下的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 2                                                   
        #define AP_SERVER_PATCHLEVEL_NUMBER 15                                                    
        #define AP_SERVER_DEVBUILD_BOOLEAN 0                                                      
    可以根据自己喜好,修改或隐藏版本号与名字。                                                                         
                                                                                                  
                                                                                                  
                                                                                                  
    *********************************************************                                     
    下载-->安装-->配置                                                                                  
                                                                                                  
    先卸载自带的apache,                                                                                 
    rpm -e --nodeps http* ,不检查依赖关系卸载                                                              
    rpm -aq http    //查看卸载干净了没有                                                                   
                                                                                                  
    www.apache.org ---->httpd---->httpd.tar                                                       
    tar xf httpd.tar                                                                              
    cd httpd                                                                                      
    ./configure -help //path                                                                      
    #############################################################                                 
    ./configure --prefix=/ser_app/apache2                                                        
    --enable-deflate                                                                             
    --enable-expires                                                                             
    --enable-headers                                                                             
    --enable-modules=most                                                                        
    --enable-so                                                                                  
    --with-mpm=worker                                                                            
    --enable-rewrite                                                                              
    ##########################################################                                    
                                                                                                  
    make -j4                                                                                      
    make install                                                                                  
                                                                                                  
    ln -s /application/apache2.2.2 /application/apache                                            
    ls -l /application/apache                                                                     
                                                                                                  
    查看安装情况                                                                                        
    /application/apache/bin/apachectl -l                                                          
    /application/apache/bin/apachectl -M                                                          
    查看参数                                                                                          
    /application/apache/bin/apachectl                                                             
    启动apache                                                                                      
    /application/apache/bin/apachectl start                                                       
                                                                                                  
    查看http端口80是否存在                                                                                
    netstat -lnt|grep 80                                                                          
    查看端口号                                                                                         
    lsof -i tcp:80                                                                                
                                                                                                  
    查看进程                                                                                          
    ps -ef|grep http                                                                              
                                                                                                  
    打开浏览器http://192.168.1.106查看apache是否安装成功                                                       
    如果出现it works,说明成功,没有则排查问题                                                                     
    1查看iptables防火墙和selinux是否关闭                                                                    
    /etc/init.d/iptables stop                                                                     
                                                                                                  
    非正式情况下,允许iptables永久关闭                                                                         
    chkconfig iptables off                                                                        
                                                                                                  
    setenforce 0                                                                                  
    vim /etc/selinux/config                                                                       
    SELINUX=disabled                                                                              
                                                                                                  
    在本地服务器测试wget http://192.168.1.106测试                                                           
                                                                                                  
    查看apache日志,tail -100 /application/apache/logs/error_log                                       
                                                                                                  
    web站点布置                                                                                       
    apache的默认站点目录,是安装目录下的/application/apache/htdocs                                               
    配置文件/application/apache/conf/httpd.conf中查到,内容如下:                                              
    #documentRoot:the directory out of which you will serve your                                  
    #documents,By default,all requests are taken from this directory....                          
                                                                                                  
    cd /application/apache/htdocs/                                                                
    index.html 为apache默认网页替换掉就好                                                                   
                                                                                                  
    *************************************************************                                 
    apache配置说明                                                                                    
    apache的目录结构说明                                                                                 
        ls -l /application/apache/                                                                
                                                                                                  
                                                                                                  
        [root@centos ~]# tree /app_ser/apache/bin                                                 
        /app_ser/apache/bin                                                                       
        ├── ab              //压力测试工具                                                              
        ├── apachectl       //apache启动命令                                                          
        ├── apr-1-config                                                                          
        ├── apu-1-config                                                                          
        ├── apxs            //为apache HTTP服务编译器和安装扩展模块                                            
        ├── checkgid                                                                              
        ├── dbmmanage                                                                             
        ├── envvars                                                                               
        ├── envvars-std                                                                           
        ├── htcacheclean    //清理磁盘缓冲命令                                                            
        ├── htdbm                                                                                 
        ├── htdigest                                                                              
        ├── htpasswd        //建立更新基本认证文件,配置nagios等时会用                                             
        ├── httpd           //为apache的控制命令程序                                                      
        ├── httxt2dbm                                                                             
        ├── logresolve                                                                            
        └── rotatelogs      //apache自带的日志轮询命令                                                     
                                                                                                  
                                                                                                  
                                                                                                  
        [centos@centos ~]# tree -L 1 /app_ser/apache/conf/                                        
        /app_ser/apache/conf/                                                                     
        ├── extra   //这是额外的apache配置文件目录,这目录里的文件我们经常访问修改                                           
        ├── httpd.conf  //apache的主配置文件,经常访问修改,每一行都要能清楚                                            
        ├── magic                                                                                 
        ├── mime.types                                                                            
        └── original                                                                              
                                                                                                  
        [root@centos ~]# tree -L 1 /app_ser/apache/htdocs/                                        
        /app_ser/apache/htdocs/     //这是编译安装时apache的默认站点目录                                        
        └── index.html      //默认的首页文件,在实际环境中,习惯使用,不是必须的                                           
                                                                                                  
                                                                                                  
        ot@centos ~]# tree -L 1 /app_ser/apache/logs/                                             
        /app_ser/apache/logs/                                                                     
        ├── access_log      //apache的默认访问日志文件                                                     
        ├── cgisock.37361                                                                         
        ├── error_log       //apache的错误日志文件                                                       
        └── httpd.pid       //http的pid文件,http进程启动后,会把所有的进程id号写到此文件                                
                                                                                                  
                                                                                                  
        [root@centos ~]# tree -L 1 /app_ser/apache/modules/                                       
        /app_ser/apache/modules/    //apache的模块目录                                                 
        └── httpd.exp                                                                             
                                                                                                  
                                                                                                  
                                                                                                  
                                                                                                  
    apache的主配置文件                                                                                  
        /app_ser/apache/conf/httpd.conf                                                           
                                                                                                  
    apache的扩展配置文件                                                                                 
        apache虚拟主机的配置                                                                             
        vim /app_ser/apache/conf/extra/httpd-vhosts.conf                                          
                                                                                                  
    [root@centos ~]# tree /app_ser/apache/conf/extra/                                             
    /app_ser/apache/conf/extra/                                                                   
    ├── httpd-autoindex.conf                                                                      
    ├── httpd-dav.conf          //dav支持配置                                                         
    ├── httpd-default.conf      //配置的是apache的相关服务参数                                               
    ├── httpd-info.conf                                                                           
    ├── httpd-languages.conf    //语言支持配置                                                          
    ├── httpd-manual.conf                                                                         
    ├── httpd-mpm.conf          //服务池管理,也就是优化apache的一个配置文件                                        
    ├── httpd-multilang-errordoc.conf                                                             
    ├── httpd-ssl.conf          //提供apache SSL的支持配置文件                                             
    ├── httpd-userdir.conf                                                                        
    └── httpd-vhosts.conf       //这是虚拟主机的配置文件                                                     
                                                                                                  
                                                                                                  
                                                                                                  
    ----------------------------------------------------------------                              
    摘录httpd.conf主配置文件中+395的include行,默认是注释掉的                                                       
    #include conf/extra/httpd-vhosts.conf                                                         
    去掉#号,表示开启虚拟主机的配置                                                                              
    ----------------------------------------------------------------                              
                                                                                                  
    基于域名的虚拟主机实战配置                                                                                 
        配置apache基于域名的虚拟主机                                                                         
        将/app_ser/apache/conf/httpd.conf +395 的注释取消                                               
        在修改/app_ser/apache/conf/extra/httpd.vhosts.conf文件                                         
        修改完后使用下面的命令检查,看是否有语法错误                                                                    
    [root@centos extra]# ../../bin/apachectl -t                                                   
                                                                                                  
    授权chown -R apache:apache /var/blog                                                            
                                                                                                  
    将index.html添加在/var/blog/,测试使用                                                                 
    使用graceful参数,静默重启apache,使用户无感知,而不使用restart                                                    
    /apache/bin/apachectl graceful                                                                
                                                                                                  
    检查进程                                                                                          
    ps -ef|grep httpd                                                                             
    检查端口                                                                                          
    netstat -lnt|grep 80                                                                          
                                                                                                  
    将httpd.config的S指定域名和端口修改为:                                                                    
     ServerName 127.0.0.1:80                                                                      
                                                                                                  
    将/app_ser/apache/conf/extra/httpd-vhosts.conf的内容修改为:                                          
        <VirtualHost *:80>                                                                        
            ServerAdmin 522802558@qq.com                                                          
            DocumentRoot "/var/blog"                                                              
            ServerName  www.sheng163.com                                                          
            ServerAlias sheng163.com                                                              
            ErrorLog "logs/shengerror_log"                                                        
            CustomLog "logs/sheng-access_log" common                                              
        </VirtualHost>                                                                            
                                                                                                  
                                                                                                  
    做DNS解析,本笔记本为                                                                                  
    C:WindowsSystem32driversetchosts                                                         
    192.168.1.106 www.sheng163.com sheng163.com                                                   
    将会出现403错误,修改#<Directory "/app_ser/apache2/htdocs">为                                           
     <Directory "/var/blog">即可                                                                    
                                                                                                  
    在浏览器中输入自定义的域名就可访问                                                                             
    --------------------------------------------------------------                                
                                                                                                  
    基于ip的虚拟主机配置:(现在很少使用了)                                                                         
    添加别名ip   ifconfig eth0:181 192.168.1.181 netmask 255.255.255.0 up                             
    删除别名ip   ifconfig eth0:181 192.168.1.181 netmask 255.255.255.0 down                           
    修改apache虚拟主机配置文件,修改服务器名即可                                                                     
    检查                                                                                            
    /bin/apachectl -t                                                                             
    重启                                                                                            
    /bin/apachectl graceful                                                                       
    打开浏览器输入ip家端口号即可访问                                                                             
    http://192.168.1.181:8090/                                                                    
                                                                                                  
    ---------------------------------------------------------------                               
    基于端口的虚拟主机配置:(也不多见,一般用在内部人员使用)                                                                 
    在httpd.conf    listen行添加端口号8091即可                                                             
    修改httpd-vhosts.conf 的端端口为上面的端口号8091                                                           
    检查                                                                                            
    /bin/apachectl -t                                                                             
    重启                                                                                            
    /bin/apachectl graceful                                                                       
    打开浏览器输入ip家端口号即可访问                                                                             
    http://www.sheng163.com                                                                       
    ---------------------------------------------------------------                               
                                                                                                  
    apache优化                                                                                      
    mod_deflate模块安装                                                                               
    查看模块是否安装                                                                                      
    bin/apachectl -l|grep mod_deflate                                                             
    若没有安装则安装                                                                                      
    cd /data/tool/httpd-2.2.31/modules/filters/                                                   
    ll mod_deflate.c                                                                              
    /app_ser/apache/bin/apxs -i -c -a mod_deflate                                                 
    报错则处理                                                                                         
                                                                                                  
    将压缩配置写入http.conf里面就可使所有虚拟主机生效                                                                 
    <ifmodule mod_deflate.c>                                                                      
        DeflateCompressionLevel 9                                                                 
        SetOutFilter DEFLATE                                                                      
        AddOutputFilterBytype DEFLATE  text/html text/plain text/xml                              
        AddOutputFilterBytype DEFLATE application/javascript                                      
        AddOutputFilterBytype DEFLATE text/css                                                    
    </ifmodule>                                                                                   
                                                                                                  
    -------------------------------------------------------------                                 
    mod_exprise模块安装:允许apache配置文件控制http的“Exprise:”和                                                
    “cache-control:”头内容,这个模块控制服务器应答时的exprise头内容                                                   
    和cache-control头的max-age指令,有效可以设置为相对与源文件的最后修改时刻或者客户访问时刻                                        
                                                                                                  
    检查是否安装mod_exprise                                                                             
    /app_ser/apache/module/|grep exprise                                                          
    grep mo_exprise.so /app_ser/apache/conf/httpd.conf                                            
    以上两种情况不能同时存在,否则可能会有冲突                                                                         
    查看mod_expires.so是否编译                                                                          
    grep mod_expires.so /app_ser/apache/conf/httpd.conf                                            
                                                                                                   
                                                                                 
                                                                                                  
    ===============================================================                               
    处理apache的403错误问题                                                                              
    http状态码:403 禁止访问                                                                              
    apache配置文件中没有对站点目录的权限许可配置                                                                     
        <Directory "/app_ser/apache2/cgi-bin">  //修改路径                                            
            AllowOverride None                                                                    
            Options None                                                                          
            Order allow,deny                                                                      
            Allow from all                                                                        
        </Directory>                                                                              
                                                                                                  
    apache站点目录下无首页文件,而apache没有预设首页index.html                                                      
    禁止站点目录浏览,在Index 前添加- 或者删除                                                                     
    或者将Option设为none                                                                                                                                      
                                                                                                  

  • 相关阅读:
    EF5+MVC4系列(5) 删除的方法 1:系统推荐的先查询后remove删除的方法 2:自己new一个包含主键的类,然后 attach附加 remove删除;3:使用db.Entry 修改状态删除4:EntityState的几种状态
    指定webapi 返回 json 格式 ; GlobalConfiguration.Configuration.Formatters.Clear()
    远程桌面连接工具 Remote Desktop Manager 9.1.2.0 Enterprise 多国语言绿色版附注册码 简单使用
    .NET WebAPI 正确抛出错误详细信息
    观察者模式 发布订阅者模式
    7月目标 socket , 一致性哈希算法 ; mongodb分片; 分布式消息队列; 中间件的使用场景
    在js中 把 json对象转化为String对象的方法
    mvc4中的 webapi 的使用方式
    Uncaught TypeError: TableInit is not a constructor
    Jquery复选框操作
  • 原文地址:https://www.cnblogs.com/erick2/p/5878826.html
Copyright © 2011-2022 走看看