Apache的常用配置

來源:互聯網
上載者:User

標籤:apache 配置

1. 配置啟動指令碼
如果是源碼編譯安裝,需手動設定啟動指令碼,
官方源碼包中已經提供了這個指令碼:build/rpm/httpd.init

cp /usr/local/src/httpd-2.4.25/build/rpm/httpd.init /etc/init.d/httpd
注意檔案中有三處主要的地方需要修改下的:
httpd=${HTTPD-/usr/local/apache2/bin/httpd}
pidfile=${PIDFILE-/usr/local/apache2/logs/${prog}.pid}
CONFFILE=/usr/local/apache2/conf/httpd.conf
請根據自己的實際情況更改相應的路徑!


然後運行如下命令加入啟動列表:
chmod +x /etc/init.d/httpd
chkconfig --add httpd
chkconfig httpd on

vi /usr/local/apache2/conf/httpd.conf
找到
#ServerName www.example.com:80
改成
ServerName localhost:80

檢查設定檔文法
/usr/local/apache2/bin/apachectl -t

用指令碼啟動apache
/etc/init.d/httpd start

[[email protected] ~]# /etc/init.d/httpd start
正在啟動 httpd:                                           [確定]


2. 配置虛擬機器主機
vi /usr/local/apache2/conf/httpd.conf

找到
#Include conf/extra/httpd-vhosts.conf
刪除前面的#號


Apache2.2版和2.4配置文法稍有不同,接下來需根據相應版本修改

Apache2.2版:

找到
<Directory />
    Order deny,allow
    Deny from all
</Directory>
改成
<Directory />
    Order deny,allow
    Allow from all
</Directory>


Apache2.4版:

找到
<Directory />
    AllowOverride none
    Require all denied
</Directory>
改成
<Directory />
    AllowOverride none
    Require all granted
</Directory>


改完儲存檔案退出

vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
加入如下配置:
<VirtualHost *:80>
    DocumentRoot "/data/www"
    ServerName www.test.com
    ServerAlias www.a.com
</VirtualHost>


3. 為虛擬機器主機配置使用者認證
例如對網站admin目錄設定使用者認證,在相應的虛擬機器主機設定檔段中加入:
    <Directory "/data/www/admin">
        AllowOverride AuthConfig
        AuthName "test"
        AuthType Basic
        AuthUserFile /data/.htpasswd
        require valid-user
    </Directory>

儲存後,建立驗證使用者
/usr/local/apache2/bin/htpasswd -cm /data/.htpasswd  test
參數說明:
-c:建立一個新檔案。增加第二個使用者時需去掉-c,否則會覆蓋之前產生的檔案。
-m: 使用MD5加密,2.4版可以省略這個參數,預設就是這個。
-d: 使用CRYPT加密,2.2版的預設加密方式。


4. 佈建網域名跳轉
<IfModule mod_rewrite> 
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.a.com$
    RewriteRule ^/(.*)$ http://www.test.com/$1 [R=301,L]
</IfModule>


如果是多個網域名稱,可以這樣設定:    
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.domain.com [OR]
    RewriteCond %{HTTP_HOST} ^www.domain1.com$
    RewriteRule ^/(.*)$ http://www.domain2.com/$1 [R=301,L]
或者:   
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www.domain2.com$
    RewriteRule ^/(.*)$ http://www.domain2.com/$1 [R=301,L]
    

防止rewrite出現死迴圈
例如:
    RewriteRule ^(.*) /abc/$1 [R,L]
本來訪問的是www.abc.com結果變成了www.abc.com/abc/abc/abc/.....
加上一個條件即可:
    RewriteCond   %{REQUEST_URI} !^/abc
    RewriteRule ^(.*) /abc/$1 [R,L]
這樣就不再迴圈了。


5. 配置apache的訪問日誌
    ErrorLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test.com-error_%Y%m%d.log 86400"
    #不記錄指定檔案類型的日誌
    SetEnvIf Request_URI ".*\.gif$" image-request
    SetEnvIf Request_URI ".*\.jpg$" image-request
    SetEnvIf Request_URI ".*\.png$" image-request
    SetEnvIf Request_URI ".*\.bmp$" image-request
    SetEnvIf Request_URI ".*\.swf$" image-request
    SetEnvIf Request_URI ".*\.js$" image-request
    SetEnvIf Request_URI ".*\.css$" image-request
    CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test.com-access_%Y%m%d.log 86400" combined env=!image-request


6. 配置靜態檔案快取
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType image/gif  "access plus 1 days"
    ExpiresByType image/jpeg "access plus 24 hours"
    ExpiresByType image/png "access plus 24 hours"
    ExpiresByType text/css "now plus 2 hour"
    ExpiresByType application/x-javascript "now plus 2 hours"
    ExpiresByType application/javascript "now plus 2 hours"
    ExpiresByType application/x-shockwave-flash "now plus 2 hours"
    ExpiresDefault "now plus 0 min"
</IfModule>


或者使用mod_headers模組實現
<IfModule mod_headers.c> 
    # htm,html,txt類的檔案快取一個小時 
    <filesmatch "\.(html|htm|txt)$"> 
        header set cache-control "max-age=3600" 
    </filesmatch> 
 
    # css,js,swf類的檔案快取一個星期 
    <filesmatch "\.(css|js|swf)$"> 
        header set cache-control "max-age=604800" 
    </filesmatch> 


    # jpg,gif,jpeg,png,ico,flv,pdf等檔案快取一年
    <filesmatch “\.(ico|gif|jpg|jpeg|png|flv|pdf)$”>
        header set cache-control “max-age=29030400″
    </filesmatch>
</IfModule> 


7. 配置防盜鏈
    SetEnvIfNoCase Referer "^http://www.test.com" local_ref
    SetEnvIfNoCase Referer "www.a.com" local_ref
    SetEnvIfNoCase Referer "^$" local_ref
    <filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)">
        Order Allow,Deny
        Allow from env=local_ref
    </filesmatch>


8. 存取控制

1) 允許或限制ip訪問

<Directory /data/www/admin>
      Order deny,allow
      Deny from all
      Allow from 192.168.0.1
</Directory>


關於Order Allow,Deny的幾個例子:

Order Deny,Allow
Deny from All
先拒絕,再允許,預設為允許,當拒絕與允許衝突時,允許優先,結果為拒絕所有


Order Allow,Deny
Deny from All
交換Allow,Deny順序,這也是拒絕所有


Order Allow,Deny
Allow from All
先允許,再拒絕,預設為拒絕,當拒絕與允許衝突時,拒絕優先,結果為允許所有


Order Deny,Allow
Allow from All
交換Allow,Deny順序,這也是允許所有


Order Allow,Deny
allow from 192.168.0.1
Deny from all
拒絕所有


Order deny,allow
deny from 192.168.0.1
allow from all
允許所有


Order Deny,Allow
Deny from all
allow from 192.168.0.1
只允許192.168.0.1


Order allow,deny
allow from all
deny from 192.168.0.1
允許所有,只有192.168.0.1被拒絕


2) 禁止訪問admin.php
<Directory /data/www>
    <Filesmatch  "^admin.php(.*)$">
        Order deny,allow
        Deny from all
    </Filesmatch>
</Directory>


3) 禁止解析php
<Directory /data/www>
    php_admin_flag engine off
    <filesmatch "(.*)php">
        Order deny,allow
        Deny from all
    </filesmatch>
</Directory>



幾點說明:

1. 設定檔修改完畢後先用/usr/local/apache2/bin/apachectl -t檢查設定檔有無語法錯誤,然後再使用/usr/local/apache2/bin/apachectl graceful重新載入設定檔。


2. 如果是share方式編譯,使用相應模組時需在httpd.conf中開啟。

例如啟用rewrite模組:
vi /usr/local/apache2/conf/httpd.conf
去掉下面這句前的#號:
#LoadModule rewrite_module modules/mod_rewrite.so


3. 可以使用/usr/local/apache2/bin/httpd查看模組載入情況:
httpd -l 輸出一個靜態編譯在伺服器中的模組的列表。它不會列出使用LoadModule指令動態載入的模組。
httpd -M 輸出一個已經啟用的模組列表,包括靜態編譯在伺服器中的模組和作為DSO動態載入的模組。


Apache的常用配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.