Showing posts with label Apache. Show all posts
Showing posts with label Apache. Show all posts

17 October 2016

How To Rate Limit Download Speed and Throttling for Apache HTTP with mod_ratelimit


ประเด็นคือผมอยาก Limit Speed ของการ Download ไฟล์บน Apache HTTP โดยใช้ Mod RateLimit

ข้อมูลเพิ่มเติม
http://httpd.apache.org/docs/trunk/mod/mod_ratelimit.html

1. ทำการ Enable mod ratelimit บน Apache (HTTP)
a2enmod ratelimit
2. ทำการ Configuration ระบุในส่วนของ URL ที่ต้องการ Limit Speed Download
vi /etc/apache2/conf-available/ratelimit.conf


3. ทำการ Enable Configure
a2enconf ratelimit
4. ทำการ Restart Apache
service apache2 restart

5. ทดสอบ Download file ขนาด 1.3GB ทั้ง Limite 500Kbps และ Unlimite ในการ Download Speed จาก Browser



เรียบร้อยแล้วครับ


How To Configure Username and Password Authentication with Apache HTTP


เนื่องจากผมต้องการ Share file บน HTTP Server (Apache2) โดยอยากให้มี User และ password

1. ทำการ update repo และติดตั้ง apache2 , apache2-utils เพิ่มเติม
root@linux:~#sudo apt-get update
root@linux:~#sudo apt-get install apache2 apache2-utils -y

2. ทำการสร้าง Account สำหรับเข้าใช้งาน
root@linux:~# htpasswd -c /etc/apache2/.htpasswd user-share
New password:
Re-type new password:
Adding password for user user-share
root@linux:~#

3. ทำการ Configure Site บน Apache2 สำหรับการเข้าใช้งาน
root@linux:~# vi /etc/apache2/sites-enabled/000-default.conf



4. ทำการ Restart Apache2
root@linux:~#/etc/init.d/apache2 restart
 * Restarting web server apache2                                                                                                         [ OK ]
root@linux:~#

5. จากนั้นลองเรียก URL path ที่ทำการ Configure ข้างต้นและใส่ user และ password ที่ได้ตั้งไว้ครับ





เสร็จเรียบร้อยครับ


URL Refer:
https://www.digitalocean.com/community/tutorials/how-to-set-up-password-authentication-with-apache-on-ubuntu-14-04

26 March 2012

How to install Mod-Security2 on WebServer Ubuntu 11.10



ModeSecurity คือ Software Open Source ตัวหนึ่งที่ทำหน้าที่ ในการ Detection และ Prevention Engine สำหรับ เว็บไซต์ของเราที่อยู่ฝั่ง Server ที่มีประสิทธิภาพในการป้องกันการโจมตี ซึ่งเป็นส่วนหนึ่งของโมดูล Apache
1.  Packet ที่ระบบต้องการ ก่อนทำการติดตั้งนะครับ
           #apt-get install libxml2-dev liblua5.1-0 lua5.1 apache2-dev build-essential
2. ในทำการ Download โปรแกรมมาเก็บไว้ใน /tmp ก่อน
          #cd /tmp
          # wget http://www.modsecurity.org/download/modsecurity-apache_2.5.11.tar.gz



3. ให้ Extract ไฟล์ที่ Download มาในครับ
          # tar xvfz modsecurity-apache_2.5.11.tar.gz
4. เข้าไปที่ Directory mod-security
          #cd modsecurity-apache_2.5.11/apache2/
5. ให้ทำการ Build ตัว Mod-security ซะ
          #./configure && make && make install
6. ทำการสร้างไฟล์ Mod-Security.load ใน apache
          #vi /etc/apache2/mods-available/mod-security2.load
     แล้วทำการ พิมพ์หรือ Copy ข้อความข้างล่างลงไปนะครับ
             
LoadFile /usr/lib/libxml2.so
LoadFile /usr/lib/i386-linux-gnu/liblua5.1.so.0
LoadModule security2_module /usr/lib/apache2/modules/mod_security2.so

จากนั้นทำการ Save ไฟล์ โดยพิมพ์ :wq
หมายเหตุ : ตำแหน่ง (Path) ไฟล์แต่ละเครื่องอาจไม่เหมือนกัน ต้องลอง #Find / -name ตามด้วยชื่อไฟล์ที่ต้องการหา ดูนะครับ
7.  ให้ Enable โมดูล mod-security2 และ unique_id

#a2enmod mod-security2
#a2enmod unique_id


8. ทำการเรียกค่า config เข้ามาที่ไฟล์โมดูล ดังนี้

#vi /etc/apache2/conf.d/mod-security2.conf
แล้วพิมพ์เข้าไป


Include /etc/modsecurity2/*.conf

9. ทำการสร้าง Directory เพื่อเก็บไฟล์ต่างๆ ที่จำเป็น

#mkdir /etc/modsecurity2
#mkdir /etc/modsecurity2/logs
#touch /etc/modsecurity2/logs/modsec_audit.log
#touch /etc/modsecurity2/logs/modsec_debug.log

10. ทำการ Copy core rule ไปไว้ใน mod-security โฟเดอร์
#cp /tmp/modsecurity-apache_2.5.11/rules/*.conf /etc/modsecurity2

11. ทำการแก้ไขที่ Path ไฟล์ของ Log ให้ตรงตามที่เราได้สร้างเอาไว้
#vi /etc/modsecurity2/modsecurity_crs_10_config.conf

โดย :
           ค้นหา  SecDebugLog logs/modsec_debug.log
           แทนที่ด้วย SecDebugLog /etc/modsecurity2/logs/modsec_debug.log

           ค้นหา SecAuditLog logs/modsec_audit.log
           แทนที่ด้วย  SecAuditLog /etc/modsecurity2/logs/modsec_audit.log
จากนั้นก็ Save ไฟล์ แล้วออก

12. ทำการตรวจสอบค่า Config ของ apache ซึ่งถ้าไม่มีปัญหาอะไร จะคืนมาเป็น Syntax OK
       #apache2ctl configtest

13. ทำการ Restart Apache
       #/etc/init.d/apache2 restart

14. การตรวจสอบการทำงานของ mode-security2 ทำได้โดย
      #cat /var/log/apache2/error.log | grep ModSecurity

8 July 2011

How to Config Apache Customize on Linux

    อันนี้ note ไว้กันลืม ครับ
ถ้าเกิดตอนลง apache2  แล้วเกิดข้อความแจ้งเตือนขึ้นมาดังนี้
-----------------------------------
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName

วิธีแก้ไขคือ    1. ให้แก้ไขที่ /etc/host โดยใช้คำสั่ง vi /etc/hosts  แล้วให้ทำดังนี้


127.0.0.1    localhost
127.0.1.1    greanjub
ไปเป็น

127.0.0.1    localhost.local       localhost
127.0.1.1    greanjub.local       greanjub  (ตรงนี้เป็นชื่อเครื่องของเราเด้อ)



                  2. แก้ไขที่  vi /etc/apache2/httpd.conf แล้ว ปกติแล้ว ถ้าลงใหม่ๆ จะไม่มีข้อมูลอะไรเลยให้เพิ่ม 

            ServerName greanjub     (ตรงนี้เป็นชื่อเครื่องของเราเด้อ)

จากนั้นให้ทำการ restart apache2 โดยใช้คำสั่ง  /etc/init.d/apache2 restart  แค่นี้ก็จะไม่มีข้อความเตือนอีกแล้วครับ เรียบร้อยโรงเรียนไทยเลย ครับวัยล๊วน
-----------------------------------------------------------------------------------------------------------------------------------
-apache web server Hardening (เบื้องต้น)
                   group:: www-data, user:: www-data
                           /var/www   (default DecumentRoot)
                          /etc/apache2 (directory)
                                 conf.d  (sub-directory)
                                           charset  //file config กำหรดค่าตัวอักษรที่ใช้
                                     sercurity  //file config กำหนดค่า security
                                                             ตัวแปรที่สำคัญ
                                                            ServerTokens=Prod
                                                               ServerSignature = off
                                                            TracerEnable=off

                     mods-available (sub-directory) //Full modul
                     mods-enable (sub-directory) //modul บางส่วนของ apache2 $sudo a2enmod
                                           ถ้าต้องการ enable module ที่ต้องการ
                                  sudo a2enmod ____ <-(proxy cach)



                   site-available (sub-directory) //full site ถ้าต้องการเว็บอีกเว็บที่มากว่าเว็บเดิม ก็
                                                                         copy default ไปเป็นอีกชื่อหนึ่ง
                   site-enable    (sub-dircetory) // กรณีต้องการ สร้าง site เพิ่ม   $ sudo a2ensite
                                        ถ้าต้องการเปิด เว็บไซด์ ที่มากขึ้น
                                     sudo a2ensite ชื่อ------

  envvers    //file  กำหนดค่าตัวแปรระบบให้ apache
  magic
  apache2.conf    //file config
  httpd.conf    //file config แต่ ใน Ubuntu ไม่ใช่
// linux อื่นๆ ใช้ file นี้ file เดี๋ยวเก็บค่า config ทุกอย่าง
  ports.conf   // file config ใช้กำหนด port  number.

------------------------------------------------------------------------------------------------------
ถ้าต้องการ check PID
cat /var/run/apache2.pid
---------------------------------------------------------------------------------------------------------

หากต้องการ WebServer ของเรา ไม่ให้แสดง List Directory เมื่อ User เปิด index of ของเรา
ไปที่
nano /etc/apache2/site-available/default

...............................................
<Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews (ให้ลบ Indexes  ทิ้งไปเด้อ)
                AllowOverride None
                Order allow,deny
                allow from all
</Directory>

...............
......ทำการ Save (Ctrl+O Enter) แล้ว Exit (Ctrl+X)   ทำการ Restart Apache โดย /etc/init.d/apache2 restart
--------------------------------------------------------------------------------------------------------

อยากตั้งค่าให้เว็บแสดงผลเป็นภาษาไทยโดย Auto แบบ UTF-8
ไปที่  nano /etc/apache2/conf.d/charset
ให้ เพิ่ม AddDefaultCharset UTF-8 ลงไป นะครับ^^
ทำการ Save (Ctrl+O Enter) แล้ว Exit (Ctrl+X)   ทำการ Restart Apache โดย /etc/init.d/apache2 restart
--------------------------------------------------------------------------------------------------------

การทำ Virtual Host ในเครื่อง windows นะครับ ^^

ทำความเข้าใจนิดหน่อย ว่าเป็นการทำ ให้ 1 IP มีหลาย Domain Name นะครับ

1.  เปิด C:\WINDOWS\system32\drivers\etc\hosts ใน Notepad โดยปกติ
        127.0.0.1   localhost
127.0.0.1 web.local (เพิ่มเข้าไปเท่าที่ต้องการเลยครับ)
127.0.0.1   web2.local
2. เปิด C:\AppServ\Apache2.2\conf\httpd.conf ทำการแก้ไขไฟล์โดย เอา # ออกครับ
#Virtual hosts
Include conf/extra/httpd-vhosts.conf

แล้วทำการ Save file
3.เปิด C:\AppServ\Apache2.2\conf\extra\httpd-vhosts.conf
------------------------------------------------------------------------
# You may use the command line option '-S' to verify your virtual host # configuration.
 

<Directory c:/AppServ/www>  
Order Deny,Allow  
Allow from all  
</Directory>  
<Directory d:/>  
Order Deny,Allow
Allow from all
</Directory>   
ถ้าเราไม่กำหนด ในข้างต้น เวลาเราเรียก ชื่อ Virtual host แล้วจะมีข้อความ ที่ Browser ครับ
Forbidden
You don't have permission to access /index.php on this server.



# Use name-based virtual hosting. 
#
  NameVirtualHost *:8088 (เปิด Port ที่ให้บริการครับ)  

# # VirtualHost example: 
# Almost any Apache directive may go into a VirtualHost container. 
# The first VirtualHost section is used for all requests that do not 
# match a ServerName or ServerAlias in any <VirtualHost> block. 
#
  <VirtualHost *:8088>
DocumentRoot c:/AppServ/www
ServerName localhost 
</VirtualHost>
<VirtualHost *:8088>
DocumentRoot c:/AppServ/www/21072011_5151001277
ServerName web.local (ชื่อ host URL)
</VirtualHost>
<VirtualHost *:8088>
DocumentRoot d:/21072011_5151001277
ServerName web1.local (ชื่อ host URL)
</VirtualHost>

จากกันให้ Save File

4. ให้ ทำการ Restart Service Apache  โดย จะ Restart คอมพิวเตอร์ก็ได้นะครับ อาจเสียเวลานิดหน่อย ถ้าไม่มีเวลาเพราะไม่มีนาฬิกาข้อมือใส่ก็ไม่เป็นไร ไปที่ Run แล้วพิมพ์ Cmd ให้พิมพ์
         C:/>net stop apach2.2
         C:/>net start apach2.2

5. น่าจะไม่มีปัญหาอะไร นะครับ น่าเกิดมันไม่ มี Error นะครับ   เรามาทดสอบโดยการเปิด Browser แล้วพิมพ์ ชื่อ Virtual host ที่ได้กำหนดไว้ นะครับ แค่นี้ก็เสร็จแล้วครับ สู้ๆๆ
----------------------------------------------------------------------------------------------