Posts tagged apache
เล่นกับ .htaccess
2จดไว้กันลืม ลอกมาจาก askapache.com
Require password for 1 file only
<Files login.php> Order deny,allow Deny from all AuthName "htaccess password prompt" AuthType Basic AuthUserFile /home/askapache.com/.htpasswd Require valid-user </Files>
Protect multiple files:
<FilesMatch "^(exec|env|doit|phpinfo|w)\.*$"> Order deny,allow Deny from all AuthName "htaccess password prompt" AuthUserFile /.htpasswd AuthType basic Require valid-user </FilesMatch>
Using the Apache Allow Directive in htaccess
network/netmask pair
Order deny,allow Deny from all Allow from 10.1.0.0/255.255.0.0
IP address
Order deny,allow Deny from all Allow from 10.1.2.3
More than 1 IP address
Order deny,allow Deny from all Allow from 192.168.1.104 192.168.1.205
Partial IP addresses, first 1 to 3 bytes of IP, for subnet restriction
Order deny,allow Deny from all Allow from 10.1 Allow from 10 172.20 192.168.2
network/nnn CIDR specification
Order deny,allow Deny from all Allow from 10.1.0.0/16
IPv6 addresses and subnets
Order deny,allow Deny from all Allow from 2001:db8::a00:20ff:fea7:ccea Allow from 2001:db8::a00:20ff:fea7:ccea/10
Deny subdomains
Order Allow,Deny Allow from apache.org Deny from wireshark.apache.org
Allow from IP without password prompt, and also allow from any address with password prompt
Order deny,allow Deny from all AuthName "htaccess password prompt" AuthUserFile /home/askapache.com/.htpasswd AuthType Basic Require valid-user Allow from 172.17.10.1 Satisfy Any
Skeleton .htaccess file to start with. Ultimate htaccess file sample
#
# DEFAULT SETTINGS
#
Options +ExecCGI -Indexes
DirectoryIndex index.php index.html index.htm
ErrorDocument 400 /cgi-bin/error.php
ErrorDocument 401 /cgi-bin/error.php
ErrorDocument 403 /cgi-bin/forbidden.cgi
ErrorDocument 404 /404.html
ErrorDocument 405 /cgi-bin/error.php
ErrorDocument 406 /cgi-bin/error.php
ErrorDocument 409 /cgi-bin/error.php
ErrorDocument 413 /cgi-bin/error.php
ErrorDocument 414 /cgi-bin/error.php
ErrorDocument 500 /cgi-bin/error.php
ErrorDocument 501 /cgi-bin/error.php
### DEFAULTS
ServerSignature Off
AddType video/x-flv .flv
AddType application/x-shockwave-flash .swf
AddType image/x-icon .ico
AddDefaultCharset UTF-8
AddLanguage en-US .html .htm .txt .xml .php
SetEnv TZ America/Indianapolis
SetEnv SERVER_ADMIN webmaster@askapache.com
### PHPINI-CGI
#AddHandler php-cgi .php
#Action php-cgi /cgi-bin/php5.cgi
### FAST-CGI
#AddHandler fastcgi-script .fcg .fcgi .fpl
#AddHandler php5-fastcgi .php
#Action php5-fastcgi /cgi-bin/fastcgi.fcgi
#
# HEADERS and CACHING
#
# 1 YEAR
<FilesMatch "\.(flv|ico|pdf)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>
# 1 WEEK
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# 3 HOUR
<FilesMatch "\.(txt|xml|js|css)$">
Header set Cache-Control "max-age=10800"
</FilesMatch>
# 1 MIN
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
Header set P3P "policyref=\"/w3c/p3p.xml\", CP=\"NOI DSP COR NID CUR ADM DEV OUR BUS\""
Header set imagetoolbar "no"
</FilesMatch>
#
# REWRITES AND REDIRECTS
#
### SEO REDIRECTS
#Redirect 301 /ssl-ns.html /2006/htaccess/apache-ssl-in-htaccess-examples.html
#Redirect 301 /ht.tml
#Redirect 301 /index.html /
#RedirectMatch 301 /2006/htaccess-forum/(.*) /2006/htaccess/$1
#RedirectMatch 301 /(.*)rfc2616(.*) http://rfc.askapache.com/rfc2616/rfc2616.html
#RedirectMatch 301 /phpmanual(.*) /manual/en/$1
### REWRITES
RewriteEngine On
RewriteBase /
### WORDPRESS
#<IfModule mod_rewrite.c>
#RewriteEngine On
#RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule . /index.php [L]
#</IfModule>
### REQUIRE WWW
#RewriteCond %{HTTP_HOST} !^www\.askapache\.com$ [NC]
#RewriteRule ^(.*)$ /$1 [R=301,L]
### STOP LOOP CODE
#RewriteCond %{ENV:REDIRECT_STATUS} 200
#RewriteRule ^.*$ - [L]
### REDIRECT BLOG FEED TO FEEDBURNER
#RewriteCond %{HTTP_USER_AGENT} !^.*(FeedBurner|FeedValidator|Recent) [NC]
#RewriteRule ^feed/?.*$ http://feeds.feedburner.com/apache/htaccess [L,R=302]
### BLOCK WGET
#RewriteCond %{HTTP_USER_AGENT} ^Wget.* [NC]
#RewriteRule .* /cgi-bin/forbidden.cgi [L]
#
# AUTHENTICATION
#
### BASIC PASSWORD PROTECTION
#AuthName "Prompt"
#AuthUserFile /home/askapache.com/.htpasswd
#AuthType basic
#Require valid-user
### UNDER CONSTRUCTION PROTECTION
#AuthName "Under Development"
#AuthUserFile /home/askapache.com/.htpasswd
#AuthType basic
#Require valid-user
#Order Deny,Allow
#Deny from all
#Allow from 23.23.23.1 w3.org googlebot.com google.com google-analytics.com
#Satisfy AnyHow To: ติดตั้งและรัน Redmine ด้วย Apache + mod_passenger บน Debian
3Redmine เป็น Project Management Software (Bug Tracker) ตัวหนึ่ง เอาไว้จัดการโปรเจคต่างๆที่ทำกันเป็นทีมใหญ่ๆ เจ้า Redmine นี่เขียนและรันบน Ruby on Rails ซึ่งยังจัดเป็นของแปลกอยู่สำหรับทุกวันนี้ มันก็เลยติดตั้งค่อนข้างยุ่งยากกว่า PHP ที่นิยมใช้กันแพร่หลายและมักจะติดตั้งมาเป็น Default ของ Web Server ส่วนใหญ่

Redmine Screenshot
Blog นี้จะอธิบายขั้นตอนการติดตั้ง Redmine ไม่ใช่วิธีใช้
ถ้าต้องการอ่านวิธีใช้งานให้ไปอ่านที่ ใช้ Redmine พัฒนา project ของ @ploysics
Subversion: Bug แสดงรายการ Repository ทั้งหมดไม่ได้
1เวลาอยากให้ SVN ที่รันผ่าน Apache (mod_dav_svn) มันสามารถ List รายการ Repository ทั้งหมดในนั้นได้ ถ้างงลองดูรูปประกอบ
ปกติมันก็ควรทำได้ง่ายๆและเป็นอย่างในรูป โดยการเพิ่มคำสั่ง SVNListParentPath On ลงไปก็แค่นั้น
<Location />
DAV svn
SVNParentPath /home/vhost/<hidden>/svn
SVNListParentPath On
...
</Location>
แต่พอลองทำแล้ว มันดัน Forbidden !@#$%!@#$% (more…)
ปรับ Apache ให้ซ่อนรายละเอียด Server
0สิ่งที่ผมจะพูดต่อไปนี้อ้างอิงกับ Debian เท่านั้นนะครับ อาจดูเป็นเรื่องง่ายๆ แต่ผมไม่เคยเอะใจสักนิด
ปัญหาก็คือผมต้องการซ่อนรายละเอียดของ Server ที่รันเว็บอยู่ ซึ่งปกติมันจะบอกข้อมูลมาแบบนี้
Apache/2.2.9 (Debian) DAV/2 SVN/1.5.1 mod_python/3.3.1 Python/2.5.2 mod_ssl/2.2.9 OpenSSL/0.9.8g mod_wsgi/2.3 Phusion_Passenger/2.0.4 Server at <hidden> Port 80
วิธีซ่อนก็ไม่ยาก แค่ไปแก้สองคำสั่งใน apache2.conf ตามนี้ รายละเอียดพวกนี้ก็จะถูกซ่อนไป
ServerSignature Off
ServerTokens Prod
แต่เมื่อแก้เสร็จแล้ว Apache ก็ยังคายรายละเอียดของ Server ออกมาเยอะเหมือนเดิมอยู่ ซึ่งตามปกติถ้าเราปรับอย่างนั้นแล้ว มันไม่ควรจะคายอะไรออกมาเลย
ก็เลยลองไล่ดูคำสั่งในโฟลเดอร์ config ของ Apache ไปเรื่อยๆ จนเจอว่ามีอีกไฟล์ที่มีสองคำสั่งนั้นเช่นกัน คือ /etc/apache2/conf.d/security เลยตามเข้าไปแก้ในไฟล์นั้น และแล้วมันก็ซ่อนตามที่เราต้องการ
สาเหตุที่ปรับใน /etc/apache2/apache2.conf แล้วไม่ได้ผล นั่นเพราะ 2 คำสั่งข้างบนนั้น มันมาก่อนคำสั่ง
Include /etc/apache2/conf.d/
ซึ่งคำสั่งนี้ทำให้ Apache ดึงทุกไฟล์ในโฟลเดอร์ conf.d เข้ามาประมวลผล เนื่องจาก Apache ใช้หลักการ "มาทีหลังดังกว่า" ก็คือถ้ามีคำสั่งซ้ำกัน มันจะยึดตามอันที่มาหลังสุดเป็นหลัก ด้วยเหตุนี้ มันเลยอ่านคำสั่งในไฟล์ conf.d/security มาทับคำสั่งเดิมใน apache2.conf แทน
บทเรียนวันนี้: คิดจะแก้อะไร ลอง <code>grep -R</code> ดูก่อนทุกครั้ง – -"
Shortnote: Install Trac on Debian
0Standalone Trac
- aptitude install trac
- trac-admin <envdir> initenv
- trac-admin <envdir> permission add <user> TRAC_ADMIN
- tracd –port <port> <envdir>
Apache + mod_python
- aptitude install libapache2-mod-python
- zless /usr/share/doc/trac/README.Debian.gz
- chmod -R www-data.www-data <envdir>
- If failed, see apache’s error.log
