請確認已載入 mod_rewrite 模組後,在 httpd.conf 加入下方設定,重啟 apache 即可
方法一
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
方法二
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
2015年12月7日
2014年11月24日
Apache mod_h264 影片串流模組
Apache2 加上 H264 streaming module 後,就能支援影片串流的功能,安裝方式也相當容易。但特別注意到授權部份,非商業使用是採 Creative Commons 授權可免費使用,而商業用途則是要付費的,費用我覺得還滿便宜的,一台伺服器才39歐元,詳細資訊請參閱官方網站說明
shell># wget http://h264.code-shop.com/download/apache_mod_h264_streaming-2.2.7.tar.gz
shell># tar xzvf apache_mod_h264_streaming-2.2.7.tar.gz
確認系統是否有安裝apxs套件,沒有的話自行用yum安裝
shell># rpm -q httpd-devel
shell># yum -y install httpd-devel
編譯安裝
shell># cd apache_mod_h264_streaming-2.2.7
shell># ./configue --with-apxs=/usr/sbin/apxs
shell># make && make install
shell># ls /etc/httpd/modules/mod_h264_streaming.so
/etc/httpd/modules/mod_h264_streaming.so
設定 httpd.conf
shell># vim /etc/httpd/conf/httpd.conf
加上以下兩行
LoadModule h264_streaming_module modules/mod_h264_streaming.so
AddHandler h264-streaming.extensions .mp4
__儲存離開__
重啟httpd完成安裝
shell># service httpd restart
若無法正常播放影片時,請先確認 Apache access_log,http status code是否為 415,若是可能是影片編碼格式問題導致無法播放,可以用ffmpeg確認問題
How to convert video in H264 format
另外FFMPEG有份值得參考的文件
https://trac.ffmpeg.org/wiki/Encode/H.264
安裝程序
首先到首頁下載tarball安裝檔並解壓縮shell># wget http://h264.code-shop.com/download/apache_mod_h264_streaming-2.2.7.tar.gz
shell># tar xzvf apache_mod_h264_streaming-2.2.7.tar.gz
確認系統是否有安裝apxs套件,沒有的話自行用yum安裝
shell># rpm -q httpd-devel
shell># yum -y install httpd-devel
編譯安裝
shell># cd apache_mod_h264_streaming-2.2.7
shell># ./configue --with-apxs=/usr/sbin/apxs
shell># make && make install
shell># ls /etc/httpd/modules/mod_h264_streaming.so
/etc/httpd/modules/mod_h264_streaming.so
設定 httpd.conf
shell># vim /etc/httpd/conf/httpd.conf
加上以下兩行
LoadModule h264_streaming_module modules/mod_h264_streaming.so
AddHandler h264-streaming.extensions .mp4
__儲存離開__
重啟httpd完成安裝
shell># service httpd restart
測試
下載完整影片
shell># wget -O sample.mp4 "http://1.2.3.4/sample.mp4"
給予影片起始時間
shell># wget -O 500_sample.mp4 "http://1.2.3.4/sample.mp4?start=500"
比較下載的兩個檔案大小,若有不一樣代表設定成功
shell># wget -O sample.mp4 "http://1.2.3.4/sample.mp4"
給予影片起始時間
shell># wget -O 500_sample.mp4 "http://1.2.3.4/sample.mp4?start=500"
比較下載的兩個檔案大小,若有不一樣代表設定成功
除錯
若無法正常播放影片時,請先確認 Apache access_log,http status code是否為 415,若是可能是影片編碼格式問題導致無法播放,可以用ffmpeg確認問題
1.2.3.4 - - [24/Nov/2014:11:01:38 +0800] "GET /content/1080p.mp4 HTTP/1.1" 415 263 "http://1.2.3.4/player.php?file=/content/1080p.mp4" "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36"
編碼
若需要自己處理轉碼,可以參考官網 wiki 採用的參數How to convert video in H264 format
另外FFMPEG有份值得參考的文件
https://trac.ffmpeg.org/wiki/Encode/H.264
2014年8月27日
Apache mod_log_forensic
要瞭解 client request 資訊, mod_log_forensic 是非常好用的模組
它會在每個 request 前後安插一組紀錄
參考資料:http://httpd.apache.org/docs/2.2/en/mod/mod_log_forensic.html
它會在每個 request 前後安插一組紀錄
參考資料:http://httpd.apache.org/docs/2.2/en/mod/mod_log_forensic.html
2011年11月26日
將 apache log 紀錄到遠端主機
一般狀況 apache 會利用 customlog 及 errorlog 指令將紀錄寫到指定的文件裡,但想利用 syslog 的遠端紀錄將 apache log 送到其他主機上要如何處理? 作法如下:
第一種方式(但不適用於CustomLog指令)
# 利用 ErrorLog 指令將紀錄檔送給 syslog
shell># vim /etc/httpd/conf/httpd.conf
ErrorLog syslog:local1
# 設定 syslog 接收 apache 丟過來的紀錄
shell># vim /etc/syslog.conf
local1.* /var/log/httpd_error_log
# 重新啟動服務並設定權限
shell># service httpd restart
shell># service syslog restart
shell># chown apache.apache /var/log/httpd_error_log
第二種方式
#利用 logger 指令
shell># vim /etc/httpd/httpd.conf
CustomLog "| /usr/bin/logger -p local1.info" combined
ErrorLog "|/usr/bin/logger -p local2.info"
shell># vim /etc/syslog.conf
local1.* /var/log/httpd_access_log
local2.* /var/log/httpd_error_log
shell># service httpd restart
shell># service syslog restart
新個資法即將要上路,企業有責任要保存系統紀錄,為確保紀錄檔不被攻擊者清除,將apache的log送到遠端的主機是有必要的,另外也可以將紀錄檔送到遠端的主機進行分析,以減少分析時對伺服器產生的衝擊
第一種方式(但不適用於CustomLog指令)
# 利用 ErrorLog 指令將紀錄檔送給 syslog
shell># vim /etc/httpd/conf/httpd.conf
ErrorLog syslog:local1
# 設定 syslog 接收 apache 丟過來的紀錄
shell># vim /etc/syslog.conf
local1.* /var/log/httpd_error_log
# 重新啟動服務並設定權限
shell># service httpd restart
shell># service syslog restart
shell># chown apache.apache /var/log/httpd_error_log
第二種方式
#利用 logger 指令
shell># vim /etc/httpd/httpd.conf
CustomLog "| /usr/bin/logger -p local1.info" combined
ErrorLog "|/usr/bin/logger -p local2.info"
shell># vim /etc/syslog.conf
local1.* /var/log/httpd_access_log
local2.* /var/log/httpd_error_log
shell># service httpd restart
shell># service syslog restart
新個資法即將要上路,企業有責任要保存系統紀錄,為確保紀錄檔不被攻擊者清除,將apache的log送到遠端的主機是有必要的,另外也可以將紀錄檔送到遠端的主機進行分析,以減少分析時對伺服器產生的衝擊
2010年10月2日
增強apache安全的方法
請注意這些設定並不一定符合您的環境,請斟酌使用哦!
1.更新所有關於security的patch
若patch沒上,做在多的設定也是枉然~~
2.隱藏主機資訊
apache預設會提供許多主機資訊,你可以透過下方的參數關閉
3.不與其他服務共用帳號
有些Linux發行版本會使用nobody作為apache預設的帳號及群組,若共用的服務遭受攻擊,apache也可能連帶受影響,反之亦然
4.避免apache存取網站根目錄以外的檔案
設定Options為None或-Indexes
設定Options為None或-Includes
若沒用到CGI功能,利用Options關閉
利用Options關閉此功能
假如一定要用.htaccess,要確保不能被下載或改檔名
相關資訊可以參考 ModSecurity
11.關閉沒有必要的模組
通常Linux隨附的apache,預設都會載入一堆很少用到的模組,請關掉它
12.確保只有root可以存取apache的設定檔及執行檔
13.降低Timeout的值,降低DoS攻擊的影響
14.限制large requests
LimiteRequestBody指令預設是無限制,若你允許小於1M的檔案上傳,你可以利用此指令限制服務請求,若你不允許檔案上傳,你甚至可以設定更小的值
另外還有LimitRequestFields, LimitRequestFieldSize, LimitRequestLine等參數,你可以依照本身的需求調校
15.限制存取IP
若你有些網站,只希望某些網段可以存取時,可利用Order管控
16.apache chroot
apache chroot有以下幾種方法
a. mod_security (推薦)
b. mod_chroot
c. 自己手動建制環境
17.經常檢查Log
雖然Log顯示的是已經發生的紀錄,但透過Log可以得知使用者在做什麼,哪邊該加強
18. 設定 X-Frame-Options
在Apache設定檔加上 X-Frame-Options 以確保本身內容不會遭惡意嵌入道其他網站、避免 clickjacking 攻擊
限制 cookie 只能經由 HTTP(S) 協定來存取,降低 XSS 攻擊的機會
參考文獻:
Apache Security Tips
20 ways to Secure your Apache Configuration
https://developer.mozilla.org/zh-TW/docs/HTTP/X-Frame-Options
1.更新所有關於security的patch
若patch沒上,做在多的設定也是枉然~~
2.隱藏主機資訊
apache預設會提供許多主機資訊,你可以透過下方的參數關閉
ServerSignature off
ServerTokens Prod
3.不與其他服務共用帳號
有些Linux發行版本會使用nobody作為apache預設的帳號及群組,若共用的服務遭受攻擊,apache也可能連帶受影響,反之亦然
User apache
Group apache
4.避免apache存取網站根目錄以外的檔案
5.關閉目錄瀏覽功能<Directory /> Order Deny,Allow Deny from all Options None AllowOverride None </Directory> <Directory /DocumentRoot> Order Allow,Deny Allow from all </Directory>
設定Options為None或-Indexes
6.關閉includes功能<Directory /> Options -Indexes </Directory>
設定Options為None或-Includes
7.關閉CGI<Directory /> Options -Includes </Directory>
若沒用到CGI功能,利用Options關閉
8.不要允許符號連結<Directory /> Options -ExecCGI </Directory>
利用Options關閉此功能
9.停用.htaccess檔<Directory /> Options -FollowSymLinks </Directory>
假如一定要用.htaccess,要確保不能被下載或改檔名
10.使用mod_security模組<Files ~ "^\.ht"> Order allow,deny Deny from all Satisfy All </Files> 或者直接禁止 <Directory /> AllowOverride None </Directory>>
相關資訊可以參考 ModSecurity
11.關閉沒有必要的模組
通常Linux隨附的apache,預設都會載入一堆很少用到的模組,請關掉它
例如:
mod_imap, mod_include, mod_info, mod_userdir, mod_status, mod_cgi, mod_autoindex
12.確保只有root可以存取apache的設定檔及執行檔
chown -R root:root /usr/sbin/httpd
chmod -R o-rwx /usr/sbin/httpd
13.降低Timeout的值,降低DoS攻擊的影響
Timeout 45
14.限制large requests
LimiteRequestBody指令預設是無限制,若你允許小於1M的檔案上傳,你可以利用此指令限制服務請求,若你不允許檔案上傳,你甚至可以設定更小的值
LimitRequestBody 1048576
另外還有LimitRequestFields, LimitRequestFieldSize, LimitRequestLine等參數,你可以依照本身的需求調校
15.限制存取IP
若你有些網站,只希望某些網段可以存取時,可利用Order管控
Order Deny,Allow
Deny from all
Allow from 192.168.1.0/24
16.apache chroot
apache chroot有以下幾種方法
a. mod_security (推薦)
b. mod_chroot
c. 自己手動建制環境
17.經常檢查Log
雖然Log顯示的是已經發生的紀錄,但透過Log可以得知使用者在做什麼,哪邊該加強
18. 設定 X-Frame-Options
在Apache設定檔加上 X-Frame-Options 以確保本身內容不會遭惡意嵌入道其他網站、避免 clickjacking 攻擊
19. 設置 HttpOnlyHeader always append X-Frame-Options SAMEORIGIN
限制 cookie 只能經由 HTTP(S) 協定來存取,降低 XSS 攻擊的機會
Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure
參考文獻:
Apache Security Tips
20 ways to Secure your Apache Configuration
https://developer.mozilla.org/zh-TW/docs/HTTP/X-Frame-Options
2010年8月23日
設定 apache 執行 CGI 程式
編輯 httpd.conf
利用 ScriptAlias 指令設定 cgi-bin 這個目錄為 CGI 程式專用,雖然ScriptAlias與 Alias 指令很類似,通常用來連結不在 DocumentRoot 下的目錄,但 ScriptAlias 指令更能表達該目錄內含為 CGI 程式
接下來必須明確指定某目錄可以執行 CGI
告訴 Apache 什麼檔案應視為 CGI 程式,大部分的 CGI 程式的副檔名為 .cgi 或 .pl
完成上述設定後,重新啟動 apache ,將 CGI 程式放到 /var/www/cgi-bin 目錄下,確認apache有執行程式的權限,在網址列上輸入 http://server/cgi-bin/YourProg.cgi 就可以看到結果,若有問題可以參閱 error_log 查看訊息。
使用 UserDir 模組,讓一般使用者能在自己的家目錄下執行 CGI 程式
#!/usr/bin/perl -w
use strict;
print "Content-type: text/html\n\n";
foreach my $key (keys %ENV) {
print "key --> $ENV{$key}<br>";
}
參考文獻
Apache Tutorial: Dynamic Content with CGI
shell>% vim /etc/httpd/conf/httpd.conf載入 cgi 模組
LoadModule cgi_module modules/mod_cgi.so
利用 ScriptAlias 指令設定 cgi-bin 這個目錄為 CGI 程式專用,雖然ScriptAlias與 Alias 指令很類似,通常用來連結不在 DocumentRoot 下的目錄,但 ScriptAlias 指令更能表達該目錄內含為 CGI 程式
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
接下來必須明確指定某目錄可以執行 CGI
<Directory /var/www/cgi-bin/>
Options +ExecCGI
</Directory>
告訴 Apache 什麼檔案應視為 CGI 程式,大部分的 CGI 程式的副檔名為 .cgi 或 .pl
AddHandler cgi-script .cgi .pl
完成上述設定後,重新啟動 apache ,將 CGI 程式放到 /var/www/cgi-bin 目錄下,確認apache有執行程式的權限,在網址列上輸入 http://server/cgi-bin/YourProg.cgi 就可以看到結果,若有問題可以參閱 error_log 查看訊息。
使用 UserDir 模組,讓一般使用者能在自己的家目錄下執行 CGI 程式
<IfModule mod_userdir.c>印出伺服器的環境變數
UserDir public_html
</IfModule>
<Directory /home/*/public_html>
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
#!/usr/bin/perl -w
use strict;
print "Content-type: text/html\n\n";
foreach my $key (keys %ENV) {
print "key --> $ENV{$key}<br>";
}
參考文獻
Apache Tutorial: Dynamic Content with CGI
2008年7月8日
RHEL / CentOS Apache SSL 筆記
RHEL / CentOS Apache SSL 筆記
參考文件:RHEL5 Deployment Guidedate: 2007/11/12
套件需求:
http:apache web servermod_ssl:apache mod_ssl模組
openssl:用來實踐SSL及TLS協定
crypto-utils:提供一系列套件產生、管理憑證
使用現有的憑證與金鑰
例外情況:- IP或Domain name有更改的狀況下不得使用先前建立的憑證和金鑰,若有這種狀況必需要重新建立憑證。
- 若你有購買VeriSign的憑證並且準備更換軟體,由於VeriSign所發行的憑證是給特定用途的軟體、IP或domain name,所以不得延續使用現有的憑證。
產生憑證及金鑰
1.移除在安裝時建立的測試憑證及金鑰Shell> cd /etc/httpd/conf2.使用 crypto-utils 所含的 genkey 指令來產生金鑰,假設要建立 www.example.com 的金鑰,使用如下指令
Shell> rm ssl.key/server.key
Shell> rm ssl.crt/server.crt
Shell> genkey --days 3650 www.mycompany.com3. genkey www.example.com 後的畫面
#注意RHEL5不再使用make的方式產生金鑰,改由genkey代替。
# --days 代表憑證有效時間,單位為天,若不指定預設為30天
RHEL4版預設將金鑰及憑證存放於/usr/share/ssl/private和/usr/share/ssl/certs
4.選擇鑰產生的金鑰的位元,位元愈小Server回應的速度愈快,但比較不安全。請依照自己的需求進行選擇。
7.填寫憑證內容
Country Name (ISO 2 letter code):TW
#填寫國家代碼,台灣為TW
State or Province Name (full name):
#填寫州、省,台灣沒有州、省,所以略過
Locality Nmae (e.g. city):Taipei
#填寫城市名如Taipei
Organization Name (eg, company):MyCompany
#填寫公司或組織名稱如MyCompany
Organizational Unit Name (eg, section):IT
#填寫公司或組織單位如IT
common Name (FQDN):www.mycompany.com
#填寫網站 hostname如:www.mycompany.com
其餘可以略過不填寫
10.修改 /etc/httpd/conf.d/ssl.conf 裡的 SSLCertificateFile 和 SSLCertificateKeyFile 參數,指到存放憑證及金鑰的路徑。
Shell> vi /etc/httpd/conf.d/ssl.conf11.重新啟動apache
SSLCertificateFile /usr/share/ssl/certs/www.example.com.crt
SSLCertificateKeyFile /usr/share/ssl/private/www.example.com.key
新增vertualhost
<VirtualHost *:443>
DocumentRoot /var/lib/www
ServerName www.example.com
</VirtualHost>
Shell> /sbin/service httpd restart
Troboshooting
- Q1:
連結 https://hostname 都會出現錯誤,apache 的 error log 出現 Invalid method in request x16x03x01。
A1:
將所有的 <VirtualHost 0.0.0.0> 改成 <VirtualHost *:80>試試看,若有NameVirtualHost 0.0.0.0 也改成 NameVirtualHost *:80
即可解決上述問題。
參考:
http://www.mail-archive.com/modssl-users@modssl.org/msg17516.html
2008年2月29日
apache error_log 一直出現 (OS 64)指定的網路名稱無法使用 winnt_accept: Asynchronous AcceptEx failed.
這幾天會議系統的 web 服務老是停擺,查看了 apache 的 error_log 發現了一堆異樣的訊息
[warn] (OS 64)指定的網路名稱無法使用。 : winnt_accept: Asynchronous AcceptEx failed.
[notice] Parent: child process exited with status 4294967295 -- Restarting.
以錯誤訊息當關鍵字在 google 查了一下覺得可能是 AcceptEx() 這個東西造成的,在 apache 的 httpd.conf 裡設定將AcceptEx()關掉,觀察到現在,錯誤訊息果然就不會出現了而且服務執行也正常了。
httpd.conf 的設定:
找到 mpm_winnt 區段,並新增 Win32DisableAcceptEx 參數並重新啟動 apache。
<IfModule mpm_winnt.c>
Win32DisableAcceptEx # <--加上這行
ThreadsPerChild 250
MaxRequestsPerChild 0
</IfModule>
參考: http://httpd.apache.org/docs/2.0/mod/mpm_winnt.html#win32disableacceptex
[warn] (OS 64)指定的網路名稱無法使用。 : winnt_accept: Asynchronous AcceptEx failed.
[notice] Parent: child process exited with status 4294967295 -- Restarting.
以錯誤訊息當關鍵字在 google 查了一下覺得可能是 AcceptEx() 這個東西造成的,在 apache 的 httpd.conf 裡設定將AcceptEx()關掉,觀察到現在,錯誤訊息果然就不會出現了而且服務執行也正常了。
httpd.conf 的設定:
找到 mpm_winnt 區段,並新增 Win32DisableAcceptEx 參數並重新啟動 apache。
<IfModule mpm_winnt.c>
Win32DisableAcceptEx # <--加上這行
ThreadsPerChild 250
MaxRequestsPerChild 0
</IfModule>
參考: http://httpd.apache.org/docs/2.0/mod/mpm_winnt.html#win32disableacceptex
訂閱:
文章 (Atom)