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:\Windows\System32\drivers\etc\hosts                                                         
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                                                                                                                                      
                                                                                               javascript

相關文章
相關標籤/搜索