วันศุกร์ที่ 27 มิถุนายน พ.ศ. 2557

Mikrotik : หยุด Bittorent 100%

/ip firewall layer7-protocol
add name=.Bittorrent regexp="^(\\x13bittorrent protocol|azver\\x01\$|get /scra\
    pe\\\?info_hash=get /announce\\\?info_hash=|get /client/bitcomet/|GET /dat\
    a\\\?fid=)|d1:ad2:id20:|\\x08'7P\\)[RP]"

/ip firewall filter
add action=add-src-to-address-list address-list=-Bit-Addr \
    address-list-timeout=1m chain=forward disabled=no dst-address-list=\
    !-Bit-Addr-Unblock hotspot="" p2p=bit-torrent src-address=192.168.88.1/21 \
    src-address-list=!-Bit-Addr-Unblock
add action=add-src-to-address-list address-list=-Bit-Addr \
    address-list-timeout=1m chain=forward disabled=no dst-address-list=\
    !-Bit-Addr-Unblock dst-address-type="" layer7-protocol=.Bittorrent \
    src-address=192.168.88.1/21 src-address-list=!-Bit-Addr-Unblock \
    src-address-type=local
add action=drop chain=forward disabled=no dst-port=!80,443 protocol=tcp \
    src-address-list=-Bit-Addr
add action=drop chain=forward disabled=no protocol=udp src-address-list=\
    -Bit-Addr

ที่มา : http://mikrotikclub.com/board/index.php/topic,1237.75.html

วันอาทิตย์ที่ 18 พฤษภาคม พ.ศ. 2557

เมื่อเน็ตหลุด....ท่านไม่ต้องโทรมานะครับ เดี๋ยวมันก็มาเอง


เมื่อเน็ตหลุด....ท่านไม่ต้องโทรมานะครับ เดี๋ยวมันก็มาเอง

==================================
/ip firewall nat
add action=redirect chain=dstnat comment="Redirect to Proxy" disabled=yes dst-port=80 protocol=tcp to-ports=8080
/ip proxy
set always-from-cache=no cache-administrator=webmaster cache-hit-dscp=4 cache-on-disk=yes enabled=yes max-cache-size=unlimited max-client-connections=600 \
max-fresh-time=3d max-server-connections=600 parent-proxy=0.0.0.0 parent-proxy-port=0 port=8080 serialize-connections=no src-address=0.0.0.0
/ip proxy access
add action=deny disabled=no dst-port="" redirect-to=192.168.88.1/hotspot/netdown.html
/tool netwatch
add disabled=no down-script=":log error \"ISP Link seems to be DOWN , ENABLING redirection to proxy so users will see mainteneace page / zaib\"\r\
\n/ip firewall nat enable [find comment=\"Redirect to Proxy\"]" host=8.8.8.8 interval=5s timeout=1s up-script=":log error \"ISP Link seems to be UP again , Disa\
bling redirection to proxy so users internet will start work again. / zaib\"\r\
\n/ip firewall nat disable [find comment=\"Redirect to Proxy\"]\r\
\n"
ืั
ที่มา : เปียโน ไร้คอร์ด

แจ้งเตือน pppoe mikrotik หมดอายุ



Non Payment Reminder for PPPoE/HOTSPOT Clients in Mikrotik


PPPOE


Mikrotik PPPoE Serve rIP = 10.0.0.1
Mikrotik User DHCP Pool = 10.0.0.0/8
Mikrotik PPPoE Pool = 172.16.0.0/16

##############################
Adding IP pool for Expired / Non Payment Users
/ip pool
add name=Expired-Pool ranges=172.16.1.1-172.16.1.255

############################
Adding Profile for Expired / Non Payment Users
/ppp profile
add change-tcp-mss=default dns-server=10.0.0.1 local-address=10.0.0.1 name=pppoe-expired only-one=default rate-limit=128k/128k remote-address=\
Expired-Pool use-compression=default use-encryption=default use-ipv6=yes use-mpls=default use-vj-compression=default

###########################
Enabling Mikrotik Web.Proxy for Expired / Non Payment Users
Now enable Web proxy, so that expired pool users can be redirected to local proxy where they will be denied and redirected to non payment page on any local web server.

/ip proxy
set always-from-cache=no cache-administrator=webmaster cache-hit-dscp=4 cache-on-disk=no enabled=yes max-cache-size=none max-client-connections=600 \
max-fresh-time=3d max-server-connections=600 parent-proxy=0.0.0.0 parent-proxy-port=0 port=8080 serialize-connections=no src-address=0.0.0.0

#############################
Redirecting Expired User Requests from web.proxy to any local web server page
Now creating rule in web.proxy to block non payment / expired ip pool access and redirect to local web server page.
/ip proxy access
add action=deny disabled=no dst-port=”” redirect-to=10.0.0.2/nonpayment/nonpayment.htm src-address=172.16.1.0/24

10.0.0.2 is a local web server where non payment page is uploaded.
You must create any normal html page (which have the non payment message) and upload it to any web server of your choice, Either It can be local (Preferred) or remote. If its local you won’t have to create any further rules , otherwise if its on remote server like internet, you have to allow user to access that specific particular site folder and place them on top.

#############################
Adding NAT Rule to redirect Expired / Non Payment Users to local web proxy
Now add NAT rule so that only users with expired ip pool should be redirected to local web proxy where he will be redirected to any local web server page.

/ip firewall nat
add action=redirect chain=dstnat disabled=no dst-port=80 protocol=tcp src-address=172.16.1.0/24 to-ports=8080

############################
Restrict Access for Expired Users so they cannot use any other internet service
Now add filter rule to restrict expired ip pool users so they cannot access other internet services.

/ip firewall filter
add action=reject chain=forward disabled=no dst-port=!80,8080 protocol=tcp reject-with=icmp-network-unreachable src-address=172.16.1.1-172.16.1.255

Now when you want to block any users for non payment, just change his profile to pppoe-expired profile.

############################



Tips for HOTSPOT


Using above method you can also implement non payment reminder for HOTSPOT users also. I assume you already have working HOTSPOT already running in place.

Mikrotik HOTSPOT Serve rIP = 101.11.11.241
Mikrotik HOTSPOT DHCP Pool = 101.11.11.0/8
Mikrotik HOTSPOT Expired/NON_Payment Users Pool = 101.11.21.0/24

First add expired ip pool for expired/non payment users.

/ip pool
add name=expired-pool ranges=101.11.21.1-101.11.21.255

Now add profile for expired/non payment users.

/ip hotspot user profile
add address-pool=expired-pool idle-timeout=none keepalive-timeout=2m name=expired-profile shared-users=1 status-autorefresh=1m transparent-proxy=no

Now enable web proxy service so that we can redirect expired users to web proxy where they will be redirected to local web server showing non payment page.

/ip proxy
set always-from-cache=no cache-administrator=webmaster cache-hit-dscp=4 cache-on-disk=no enabled=yes max-cache-size=none max-client-connections=600 \
max-fresh-time=3d max-server-connections=600 parent-proxy=0.0.0.0 parent-proxy-port=0 port=8080 serialize-connections=no src-address=0.0.0.0

In proxy, add rule to block and redirect only expired/non payment ip pool.

/ip proxy access
add action=deny disabled=no dst-port=”” redirect-to=101.11.11.240/nonpayment/nonpayment.htm src-address=101.11.21.0/24

101.11.11.240 is the local web server ip.
Create NAT rule to redirect expired/non payment users to local web proxy. Move this rule at the end in the NAT List.

/ip firewall nat
add action=passthrough chain=unused-hs-chain comment=”place hotspot rules here” disabled=yes
add action=masquerade chain=srcnat comment=”masquerade hotspot network-DEFAULT RULE, ADD ONLY ALLOWED USER IPS RANGE HERE e.g” disabled=no src-address=101.11.11.1-101.11.11.255

add action=redirect chain=dstnat comment=”Redirect Expired Users to Proxy” disabled=no dst-address=!101.11.11.241 dst-port=80 protocol=tcp to-ports=8080
Remember, in HOTSPOT default masquerading rule, you must add only allowed users ip range. so add second rule in the last which will actually redirect only expired ip pool to local web proxy. (the red one)
Create Firewall Filter to Restrict Access for expired/non payment users only. Move this rule at the end in Filter list.

/ip firewall filter
add action=reject chain=input disabled=no dst-port=!80,8080,53 protocol=tcp reject-with=icmp-network-unreachable src-address=101.11.21.0/24

Finally create user and tag him with expired-profile.

/ip hotspot user
add disabled=no name=admin password=admin profile=default
add disabled=no name=test password=test profile=expired-profile server=hotspot1

Now from user end, try to browse any site, hotspot login page will appear, login with the test id, and try to browse, you will be redirected to your local web server page showing non payment reminder.
Remember: Rules placement is very important in this method. if you misplace rules, you will not get desired results.

credit : http://aacable.wordpress.com/2012/11/14/non-payment-reminder-for-pppoe-clients-in-mikrotik/

วันพฤหัสบดีที่ 10 ตุลาคม พ.ศ. 2556

การ Config php.ini

สำหรับการ setup php ตั้งแต่เวอร์ชั่น 5.3 เป็นต้นไป จะมีไฟล์ php.ini-development เอาไว้เป็น sample ให้ copy เป็น php.ini ถ้าต้องการใช้ทดสอบโปรแกรมเฉยๆ และไฟล์ php.ini-production เอาไว้ใช้ใน server ที่ให้บริการกับลูกค้าจริง
โดย config ที่ผมจะแนะนำให้รู้จักมีดังนี้
  • display_errors - On/Off - หากเกิด error ขึ้นให้แสดงภายในหน้าเว็บหรือไม่ ถ้าเป็นเครื่อง development ควร On ไว้เพื่อ debug ถ้าเป็นเครื่อง production ควร Off เพื่อไม่ให้ผู้ใช้เห็น
  • error_reporting - E_ALL/0 - ใช้ปรับระดับ level การแสดง error ว่าจะให้แสดงมากน้อยเพียงใด หากต้องการปิด error ไม่ให้แสดงเลย ให้ใส่เป็น 0 (ศูนย์) ได้
  • short_open_tag - On/Off - ถ้าเปิด On ไว้จะสามารถใช้ <? ?> แทน <?php ?> และ <?=’Hello World’?> แทน <?php echo ‘Hello World’; ?> ได้ (feature นี้เพื่อความสะดวกของตัวโปรแกรมเมอร์นั่นเอง)
  • max_execution_time - int - กำหนดเป็นตัวเลขที่ต้องการให้ script สามารถรันได้นานที่สุด ทั่วไปกำหนดที่ 30 วินาทีซึ่งค่อนข้างเพียงพอต่อการใช้งานทั่วไป หากต้องมีการทำงานที่ใช้การประมวลผลเป็นเวลานานสามารถปรับเพิ่มขึ้นได้ แต่ไม่แนะนำให้ใส่เยอะเกินไป เพราะอาจทำให้ loop infinite ก่อนความเสียหายเป็นระยะเวลานาน โดยที่เราไม่รู้ตัวได้
  • max_input_time - int - ใช้กำหนดเวลามากที่สุดที่อนุญาตให้ php ประมวลผลข้อมูลที่ผู้ใช้ส่งเข้ามาได้เช่นการ submit form post
  • memory_limit - int - กำหนดขนาดของ memory ที่อนุญาตให้ script แต่ละตัวใช้ได้ ถ้ากำหนดสูงไปอาจเกิดปัญหา memory leak ได้
  • post_max_size - int - ขนาดข้อมูลใหญ่ที่สุดที่อนุญาตให้ผู้ใช้ส่งเข้ามาได้ ขนาดตรงนี้รวมทั้ง file ที่ upload และข้อมูลที่ submit ผ่าน form เข้ามา
  • auto_prepend_file - string - path ไฟล์ php ที่ต้องการทำงานก่อนการทำงาน php ทุกๆ ไฟล์ที่รันตามปกติ ตรงนี้มีประโยชน์มากต่อการกำหนดตัวแปร Global ที่ต้องการให้ใช้งานได้ทุกๆ ไฟล์ ซึ่งเราจะเขียนอย่างไรก็ได้ตามใจเราเลย
  • auto_append_file - string - เหมือน  auto_prepend_file  ต่างกันแค่เป็นการทำงานหลังไฟล์ php ทุกๆ ไฟล์
  • file_uploads - On/Off - อนุญาตให้ upload file ขึ้น server ได้หรือไม่
  • upload_max_filesize - int - ขนาดไฟล์สูงสุดที่ให้ upload ได้ (ต้อง <=  post_max_size )
  • max_file_uploads - int - จำนวนไฟล์ที่ upload ได้พร้อมกันทั้งหมด
  • extension - string - กำหนด extension ของ php ที่ต้องการใช้งาน
  • session.gc_maxlifetime - int - อายุของ session ของผู้ใช้แต่ละคนที่จะเก็บไว้สูงสุด หากมีการเรียกใช้ session

Update Join Sql

update `hipdata` h right join pttype_bb pb on h.cid=pb.`3420900078775` set moi_status="OFC",pttype='26' ;

การติดตั้ง Upgrade Mysql

การติดตั้ง Upgrade Mysql 

ผมขอยกตัวอย่าง MariaDB-5.5.32 สำหรับ Centos 6 ขึ้นไปครับ ส่วนถ้าใครอยากติดตั้ง Centos 5 ขึ้นไปต้องดูว่า Os กี่บิต และใช้สำหรับอะไร เช่น

MariaDB-5.5.32-centos6-x86_64-client.rpm คือ
MariaDB-5.5.32 คือ Version MariaDB
centos6 คือ ใช้สำหรับ Centos 6 ขึ้นไป
x86_64 คือ ใช้กับ OS 64bit เท่านั้น


พอเริ่มเข้าใจโครงสร้างแล้วเราก็มาเริ่มกันเลยครับ

[root@hosxp ~]mysql -v      #ดูเวอร์ชั่น mysql
[root@hosxp ~]uname -a      #ดูเวอร์ชั่น OS
[root@hosxp ~]#vi /etc/sysconfig/iptables
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
setenforce 0

[root@hosxp ~]#vi /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT ## เพิ้มค่านี้เข้าไป
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT ## เพิ้มค่านี้เข้าไป
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

[root@hosxp ~]#vi /etc/security/limits.conf
# เพิ่มข้อมูลนี้เข้าไป
root     soft     nofile     50000
root     hard     nofile     50000
mysql     soft     nofile     50000
mysql     hard     nofile     50000


## ลบ MySQL ในเครื่องออก
[root@hosxp ~]#rpm -qa|grep mysql                #  คนตัวติดตั้งที่ขึ้นต้นด้วย mysql
[root@hosxp ~]#rpm -e --nodeps mysqlxxxxx        # ลบ Mysql
[root@hosxp ~]cp my_MariaDB_xxx.cnf /etc/my.cnf

## Install MySQL MariaDB
[root@hosxp ~]#rpm -ihv MariaDB-5.5.32-centos6-x86_64-client.rpm --nodeps --force
[root@hosxp ~]#rpm -ihv MariaDB-5.5.32-centos6-x86_64-shared.rpm --nodeps --force
[root@hosxp ~]#rpm -ihv MariaDB-5.5.32-centos6-x86_64-server.rpm --nodeps --force

[root@hosxp ~] service mysql start

** หาก Start MySQL ไม่ได้ บอก pid error ให้ทำตามนี้
[root@hosxp ~]rm -rf /var/lib/mysql/*
[root@hosxp ~]ls -al /var/lib/mysql/
[root@hosxp ~]chown -Rv mysql:mysql /var/lib/mysql/*
[root@hosxp ~]sudo -u mysql mysql_install_db
[root@hosxp ~]service mysql start

[root@hosxp ~]/usr/bin/mysqladmin -u sa password 'sa'

##MySQL Create database
[root@hosxp ~]mysql -u root -p  << Enter

MariaDB [(none)]>create database hosxp_pcu default character set tis620;
MariaDB [(none)]>grant all privileges on *.* to sa@'sa' identified by 'sa' with grant option;
MariaDB [(none)]>grant all on *.* to sa@'%' identified by 'sa' with grant option;
MariaDB [(none)]>flush privileges;
MariaDB [(none)]>Ctrl-C -- exit!
Aborted
[root@hosxp ~]

## จบครับ

วันพฤหัสบดีที่ 1 สิงหาคม พ.ศ. 2556

ปรับ my.cnf

[client]
#password    = [your_password]
port        = 3306
socket        = /var/lib/mysql/mysql.sock
default-character-set = tis620

[mysqld]
default-character-set=tis620  ###ทำให้ใช้งานภาษาไทยได้ ต้องเซ็ทตรงนี้ให้เสร็จก่อนที่จะ import ข้อมูลภาษาไทยเข้าไป
skip-charecter-set-client-handshark = ใช้เซ็ตภาษาให้กับโปรแกรมอื่น ช่น access
key_buffer=128M    ###ควรเซ็ทเป็น 40% ของ ram ที่มี
table_cache=256 ###ให้เท่ากับตารางทั้งหมดที่มีใน DB
sort_buffer_size=1M ###เป็นหน่วยความจำที่จองไว้ต่อจำนวน connection
read_buffer_size=1M  ###เป็นหน่วยความจำที่จองไว้ต่อจำนวน connection
read_rnd_buffer_size=1M  ###เป็นหน่วยความจำที่จองไว้ต่อจำนวน connection
myisam_sort_buffer_size=32M  ###ในการที่เรา create index จะใช้หน่วยความจำส่วนนี้ ควรเซ็ทไว้ 10-20%
max_allowed_packet=32M ###เป็นตัวกำหนดค่าการส่งข้อมูลใน 1 statement เช่น คำสั่ง insert ที่มีรูปมาด้วย หากคำสั่งนั้นมีขนาดใหญ่กว่าจำนวนที่กำหนดไว้ จะ error แนะนำให้กำหนดไว้ 128M
query_cache_size=128M ### ควรเซ็ทไว้ที่  10%
#interaction_timeout=300
#wait_timeout=180 ### เช่นกรณีที่รายงานนั้นใช้เวลาในการดึงรายงานออกมานานเกินเวลาที่กำหนดไว้จะทำงานตัด connection นั้นทิ้งไป
innodb_data_file_path=ibdata1:100M:autoextend ###กำหนดขนาดเริ่มต้นของ logfile
innodb_flush_log_at_trx_commit=1 ###ตัวกำหนดพฤติกรรมในการ commit ข้อมูล ถ้ามีค่าเป็น 1 คือมีการบันทึกข้อมูลลง HD ทันที ถ้าเป็น 0 จะบันทึกข้อมูลทุก 1 วินาที ถ้าไฟดับ ข้อมูลจะหายไป 1 วินาที
innodb_buffer_pool_size=64M ###อันนี้สำคัญ เป็นหน่วยความจำที่แชร์ ยิ่งเยอะยิ่งดีแต่ไม่ควรเกิน 40-60% ของหน่วยความจำที่มี
innodb_additional_mem_pool_size=8M
innodb_log_file_size=16M ### ถ้า mysql start แล้วตรงนี้ห้ามแก้ ถ้าจะแก้ต้องแก้ก่อนเริ่มใช้งาน เพราะถ้า ลบไฟล์ทิ้งข้อมูลแบบ innodbจะไม่สมบูรณ์
innodb_log_buffer_size=4M ###ระบบ Set ให้เอง
innodb_lock_wait_timeout=50
max_connections=1000 ###อันนี้สำคัญ จำนวนเครื่องที่จะ connect เข้ามาที่ mysql ได้
innodb_file_per_table ###อันนี้สำคัญ เนื่องจาก innodb เก็บทุกตารางไว้ในไฟล์เดียวกันหมด ถ้ามีคำสั่งบรรทัดนี้ จะเป็นการสั่งให้การเก็บข้อมูลเป็นแบบ 1 ตาราง เป็น 1 file
skip-character-set-client-handshake   ###ผู้ใช้สามารถใช้โปรแกรมอื่นมาดึงข้อมูลจาก mysql ได้ เช่น Access
skip-locking
skip-name-resolve ###ถ้าไม่เปิดอันนี้ mysql จะเสียเวลาแปลง ip ให้เป็น ชื่อเครื่อง
big-tables ###เปิดไว้สำหรับ myisam ให้เก็บข้อมูลได้ในระดับ tera

[mysql]
default-character-set=tis620

[mysqldump]
default-character-set=tis620
max_allowed_packet=16M
allow-keywords

ที่มา:http://group.wunjun.com/#!/hospital/topic/420259-13594