15 June 2012

OpenVPN System Based On User/Password Authentication with mysql & Day Control (lib-pam mysql) - Debian

OpenVPN System Based On User/Password Authentication with mysql &
Day Control (lib-pam mysql) - Debian
System detail: I. Install MySQL Server for User/Pass Authentication
1. Install MySQL Server
apt-get install mysql-server
    
2. Log in MySQL as root
mysql -uroot -p
    
3. Create the database 'openvpn'
CREATE DATABASE openvpn;
    
4. Create a MySQL user with username 'USERNAME' and password 'PASSWORD'
GRANT ALL ON openvpn.* TO 'USERNAME'@"%" IDENTIFIED BY 'PASSWORD';
    
5. Log out root user
exit;
    
6. Log in MySQL as new user 'USERNAME'
mysql -uUSERNAME -pPASSWORD
    
7. Switch database
USE openvpn;
    
8. Create user, log table and insert user data - user table
CREATE TABLE IF NOT EXISTS `user` (
    `user_id` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
    `user_pass` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '1234',
    `user_mail` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
    `user_phone` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
    `user_online` tinyint(1) NOT NULL DEFAULT '0',
    `user_enable` tinyint(1) NOT NULL DEFAULT '1',
    `user_start_date` date NOT NULL,
    `user_end_date` date NOT NULL,
PRIMARY KEY (`user_id`),
KEY `user_pass` (`user_pass`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    
- log table
CREATE TABLE IF NOT EXISTS `log` (
    `log_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `user_id` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
    `log_trusted_ip` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
    `log_trusted_port` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
    `log_remote_ip` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
    `log_remote_port` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
    `log_start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `log_end_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
    `log_received` float NOT NULL DEFAULT '0',
    `log_send` float NOT NULL DEFAULT '0',
PRIMARY KEY (`log_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    
- user data
INSERT INTO `user` (
    `user_id`, `user_pass`, `user_mail`, `user_phone`,
    `user_online`, `user_enable`, `user_start_date`, `user_end_date`
)
VALUES (
    'test', '1234', 'mr.tumcpe@gmail.com',
    '+66815447514', 0, 1, '2012-01-01', '0000-00-00'
);
    
9. Show tables
show tables;
+-------------------+
| Tables_in_openvpn |
+-------------------+
| log               |
| user              |
+-------------------+
    
10. Show user data
select * from user;
+---------+-----------+---------------------+--------------+-------------+-------------+-----------------+---------------+
| user_id | user_pass | user_mail           | user_phone   | user_online | user_enable | user_start_date | user_end_date |
+---------+-----------+---------------------+--------------+-------------+-------------+-----------------+---------------+
| test    | 1234      | mr.tumcpe@gmail.com | +66815447514 |           0 |           1 | 2012-01-01      | 0000-00-00    |
+---------+-----------+---------------------+--------------+-------------+-------------+-----------------+---------------+
    
11. Log out
exit;
    
II. Install OpenVPN Server and generation of certificate
1. Install OpenVPN
apt-get install openvpn
    
2. Generate the certificate Copy the certificate generator scripts from OpenVPN docs
cp -R /usr/share/doc/openvpn/examples/easy-rsa /etc/openvpn/.
cd /etc/openvpn/easy-rsa/2.0/
    
3. Modify certificate variables
vi vars
    
Edit this file and change the following lines into your case
export KEY_COUNTRY="TH"
export KEY_PROVINCE="BKK"
export KEY_CITY="Bangkok"
export KEY_ORG="Chtunnel-VPN"
export KEY_EMAIL="support@chtunnel.com"
    
4. Save and exit. Run the variable script and clean
source ./vars
./clean-all
    
5. Generate the public and private certificates. Just press ENTER or YES by default
./build-ca
./build-key-server server
./build-key client
./build-dh
mv keys /etc/openvpn/.
    
In fact, generation of client is not necessary for a User/Pass authentication approach.
III. Install lib-pam MySQL
1. Install pam_mysql module
apt-get install libpam-mysql
    
2. Configure PAM for OpenVPN Create file '/etc/pam.d/openvpn'
auth            sufficient      pam_mysql.so \
user=USERNAME passwd=PASSWORD host=localhost db=openvpn \
[table=user] usercolumn=user.user_id passwdcolumn=user.user_pass \
[where=user.user_enable=1 AND user.user_start_date!=user.user_end_date \
AND TO_DAYS(now()) >= TO_DAYS(user.user_start_date) \
AND (TO_DAYS(now()) <= TO_DAYS(user.user_end_date) OR user.user_end_date='0000-00-00')] sqllog=0 crypt=0
 
account         required        pam_mysql.so \
user=USERNAME passwd=PASSWORD host=localhost db=openvpn \
[table=user] usercolumn=user.user_id passwdcolumn=user.user_pass \
[where=user.user_enable=1 AND user.user_start_date!=user.user_end_date \
AND TO_DAYS(now()) >= TO_DAYS(user.user_start_date) \
AND (TO_DAYS(now()) <= TO_DAYS(user.user_end_date) OR user.user_end_date='0000-00-00')] sqllog=0 crypt=0
    
Detail
# Set db, user, passwd to your own values.
# Here crypt is the method to encrypt password in the database, which means
# 0 (or "plain")  = No encryption.
#                   Passwords stored in plaintext. HIGHLY DISCOURAGED.
# 1 (or "Y")      = Use crypt(3) function.
# 2 (or "mysql")  = Use MySQL PASSWORD() function. It is possible
#                   that the encryption function used by PAM-MySQL
#                   is different from that of the MySQL server, as
#                   PAM-MySQL uses the function defined in MySQL's
#                   C-client API instead of using PASSWORD()
#                   SQL function in the query.
# 3 (or "md5")    = Use plain hex MD5.
# 4 (or "sha1")   = Use plain hex SHA1.
    
3. Install saslauthd
apt-get install sasl2-bin
/etc/init.d/saslauthd restart
    

/etc/init.d/saslauthd restart
    
4. Test saslauthd config
testsaslauthd -u test -p 1234 -s openvpn
    
5. Copy OpenVPN PAM module
cp /usr/lib/openvpn/openvpn-auth-pam.so /etc/openvpn/
    
IV. Compose OpenVPN configuration files
1. Create file config.sh '/etc/openvpn/script/config.sh'
#!/bin/bash
##Dababase Server
HOST='127.0.0.1'
#Default port = 3306
PORT='3306'
#Username
USER='USERNAME'
#Password
PASS='PASSWORD'
#database name
DB='openvpn'
    
2. Create file connect.sh '/etc/openvpn/script/connect.sh'
#!/bin/bash
. /etc/openvpn/script/config.sh
##insert data connection to table log
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "INSERT INTO log (log_id,user_id,log_trusted_ip,log_trusted_port,log_remote_ip,log_remote_port,log_start_time,log_end_time,log_received,log_send) VALUES(NULL,'$common_name','$trusted_ip','$trusted_port','$ifconfig_pool_remote_ip','$remote_port_1',now(),'0000-00-00 00:00:00','$bytes_received','$bytes_sent')"
##set status online to user connected
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE user SET user_online=1 WHERE user_id='$common_name'"
    
3. Create file disconnect.sh '/etc/openvpn/script/disconnect.sh'
#!/bin/bash
. /etc/openvpn/script/config.sh
##set status offline to user disconnected
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE user SET user_online=0 WHERE user_id='$common_name'"
##insert data disconnected to table log
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE log SET log_end_time=now(),log_received='$bytes_received',log_send='$bytes_sent' WHERE log_trusted_ip='$trusted_ip' AND log_trusted_port='$trusted_port' AND user_id='$common_name' AND log_end_time='0000-00-00 00:00:00'"
    
OpenVPN server will scan .conf files in /etc/openvpn when it starts.
For each file, it forks a daemon. In this system,
we need both UDP and TCP support. I created two configuration files for two daemons in charge of UDP and TCP respectively.
4. Create file server-tcp-443.conf '/etc/openvpn/server-tcp-443.conf' for Server Port:443
##protocol port
port 443
proto tcp
dev tun

##ip server client
server 10.4.0.0 255.255.255.0

##key
ca /etc/openvpn/keys/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key
dh /etc/openvpn/keys/dh1024.pem

##option
persist-key
persist-tun
keepalive 5 60
reneg-sec 432000

##option authen.
comp-lzo
user nobody
#group nogroup
client-to-client
username-as-common-name
client-cert-not-required

##user/pass auth from mysql
plugin /etc/openvpn/openvpn-auth-pam.so openvpn

##push to client
max-clients 50
push "persist-key"
push "persist-tun"
push "redirect-gateway def1"
#push "explicit-exit-notify 1"

##DNS-Server
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"

##script connect-disconnect
script-security 3 system
client-connect /etc/openvpn/script/connect.sh
client-disconnect /etc/openvpn/script/disconnect.sh

##log-status
status /etc/openvpn/log/tcp_443.log
log-append /etc/openvpn/log/openvpn.log
verb 3
    
5. Create file server-udp-53.conf '/etc/openvpn/server-udp-53.conf' for Server Port:53
##protocol port
port 53
proto udp
dev tun

##ip server client
server 10.5.0.0 255.255.255.0

##key
ca /etc/openvpn/keys/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key
dh /etc/openvpn/keys/dh1024.pem

##option
persist-key
persist-tun
keepalive 5 60
reneg-sec 432000

##option authen.
comp-lzo
user nobody
#group nogroup
client-to-client
username-as-common-name
client-cert-not-required

##user/pass auth from mysql
plugin /etc/openvpn/openvpn-auth-pam.so openvpn

##push to client
max-clients 50
push "persist-key"
push "persist-tun"
push "redirect-gateway def1"
push "explicit-exit-notify 1"

##DNS-Server
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"

##script connect-disconnect
script-security 3 system
client-connect /etc/openvpn/script/connect.sh
client-disconnect /etc/openvpn/script/disconnect.sh

##log-status
status /etc/openvpn/log/udp_53.log
log-append /etc/openvpn/log/openvpn.log
verb 3
    
6. Create directory for log '/etc/openvpn/log'
mkdir /etc/openvpn/log
touch /etc/openvpn/log/openvpn.log
touch /etc/openvpn/log/tcp_443.log
touch /etc/openvpn/log/udp_53.log
    
7. Changes the permission of files
chmod -R 755 /etc/openvpn
    
8. Start serviece OpenVPN
/etc/init.d/openvpn start
    
V. Share Internet to Client
1. Edit file /etc/sysctl.conf Remove # In line : #net.ipv4.ip_forward=1
net.ipv4.ip_forward=1
    
3. Edit file /etc/rc.local Add before exit 0;
echo "1" > /proc/sys/net/ipv4/ip_forward
echo "1" > /proc/sys/net/ipv4/ip_dynaddr

iptables -A INPUT -i tun0 -j ACCEPT
iptables -A FORWARD -i tun0 -j ACCEPT

iptables -A INPUT -i tun1 -j ACCEPT
iptables -A FORWARD -i tun1 -j ACCEPT

iptables -A INPUT -p udp --dport 53 -j ACCEPT

iptables -t nat -A POSTROUTING -s 10.4.0.0/24 -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 10.5.0.0/24 -o eth0 -j MASQUERADE
    
4. Run Script Iptables Share Internet
/etc/rc.local
iptables-save
    
VI. Config for Client
1. Config for port TCP port 443
client
dev tun

proto tcp
remote 1.1.1.1 443

nobind
auth-user-pass
reneg-sec 432000
resolv-retry infinite

ca ca.crt
comp-lzo
verb 1
    
2. Config for port UDP port 53
client
dev tun

proto udp
remote 1.1.1.1 53

nobind
auth-user-pass
reneg-sec 432000
resolv-retry infinite

ca ca.crt
comp-lzo
verb 1        
    
3. Copy file ca.crt from /etc/openvpn/keys/ca.crt to same config path in client
Day of user in database
# If today = '2012-01-01'
# day = user_start_date | user_end_date 
# 0   = 0000-00-00 | 0000-00-00
# 0   = 2012-01-01 | 2012-01-01
# 0   = 2012-01-02 | 2012-01-01
# 1   = 2012-01-01 | 2012-01-02
# unlimited =  2012-01-01 | 0000-00-00
Install finish.

OpenVPN System Based On User/Password Authentication with mysql & Day Control (shell script)- Debian

OpenVPN System Based On User/Password Authentication with mysql &
Day Control (shell script) - Debian
System detail: I. Install MySQL Server for User/Pass Authentication, IP = 2.2.2.2
1. Install MySQL Server
apt-get install mysql-server
    
2. Log in MySQL as root
mysql -uroot -p
    
3. Create the database 'openvpn'
CREATE DATABASE openvpn;
    
4. Create a MySQL user with username 'USERNAME' and password 'PASSWORD'
GRANT ALL ON openvpn.* TO 'USERNAME'@"%" IDENTIFIED BY 'PASSWORD';
    
5. Log out root user
exit;
    
6. Log in MySQL as new user 'USERNAME'
mysql -uUSERNAME -pPASSWORD
    
7. Switch database
USE openvpn;
    
8. Create user, log table and insert user data - user table
CREATE TABLE IF NOT EXISTS `user` (
    `user_id` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
    `user_pass` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '1234',
    `user_mail` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
    `user_phone` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
    `user_online` tinyint(1) NOT NULL DEFAULT '0',
    `user_enable` tinyint(1) NOT NULL DEFAULT '1',
    `user_start_date` date NOT NULL,
    `user_end_date` date NOT NULL,
PRIMARY KEY (`user_id`),
KEY `user_pass` (`user_pass`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    
- log table
CREATE TABLE IF NOT EXISTS `log` (
    `log_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `user_id` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
    `log_trusted_ip` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
    `log_trusted_port` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
    `log_remote_ip` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
    `log_remote_port` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
    `log_start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `log_end_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
    `log_received` float NOT NULL DEFAULT '0',
    `log_send` float NOT NULL DEFAULT '0',
PRIMARY KEY (`log_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    
- user data
INSERT INTO `user` (
    `user_id`, `user_pass`, `user_mail`, `user_phone`,
    `user_online`, `user_enable`, `user_start_date`, `user_end_date`
)
VALUES (
    'test', '1234', 'mr.tumcpe@gmail.com',
    '+66815447514', 0, 1, '2012-01-01', '0000-00-00'
);
    
9. Show tables
show tables;
+-------------------+
| Tables_in_openvpn |
+-------------------+
| log               |
| user              |
+-------------------+
    
10. Show user data
select * from user;
+---------+-----------+---------------------+--------------+-------------+-------------+-----------------+---------------+
| user_id | user_pass | user_mail           | user_phone   | user_online | user_enable | user_start_date | user_end_date |
+---------+-----------+---------------------+--------------+-------------+-------------+-----------------+---------------+
| test    | 1234      | mr.tumcpe@gmail.com | +66815447514 |           0 |           1 | 2012-01-01      | 0000-00-00    |
+---------+-----------+---------------------+--------------+-------------+-------------+-----------------+---------------+
    
11. Log out
exit;
    
12. Edit file /etc/mysql/my.cnf insert # to line
bind-address  = 127.0.0.1
    
success
#bind-address  = 127.0.0.1
    
13. Edit file /etc/rc.local Add before exit 0;
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
    
14. Run Script Iptables
/etc/rc.local
iptables-save
    
II. Install OpenVPN Server and generation of certificate, IP = 1.1.1.1
1. Install OpenVPN
apt-get install openvpn
    
2. Generate the certificate Copy the certificate generator scripts from OpenVPN docs
cp -R /usr/share/doc/openvpn/examples/easy-rsa /etc/openvpn/.
cd /etc/openvpn/easy-rsa/2.0/
    
3. Modify certificate variables
vi vars
    
Edit this file and change the following lines into your case
export KEY_COUNTRY="TH"
export KEY_PROVINCE="BKK"
export KEY_CITY="Bangkok"
export KEY_ORG="Chtunnel-VPN"
export KEY_EMAIL="support@chtunnel.com"
    
4. Save and exit. Run the variable script and clean
source ./vars
./clean-all
    
5. Generate the public and private certificates. Just press ENTER or YES by default
./build-ca
./build-key-server server
./build-key client
./build-dh
mv keys /etc/openvpn/.
    
In fact, generation of client is not necessary for a User/Pass authentication approach.
III. Customize shell script, IP = 1.1.1.1
1. Create directory for script '/etc/openvpn/script'
mkdir /etc/openvpn/script
cd /etc/openvpn/script
    
2. Create file config.sh '/etc/openvpn/script/config.sh'
#!/bin/bash
##Dababase Server
HOST='2.2.2.2'
#Default port = 3306
PORT='3306'
#Username
USER='USERNAME'
#Password
PASS='PASSWORD'
#database name
DB='openvpn'
    
3. Create file test_connect_db.sh'/etc/openvpn/script/test_connect_db.sh'
#!/bin/bash
. /etc/openvpn/script/config.sh
##Test Authentication
username=$1
password=$2
user_id=$(mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -sN -e "select user_id from user where user_id = '$username' AND user_pass = '$password' AND user_enable=1 AND user_start_date != user_end_date AND TO_DAYS(now()) >= TO_DAYS(user_start_date) AND (TO_DAYS(now()) <= TO_DAYS(user_end_date) OR user_end_date='0000-00-00')")
##Check user
[ "$user_id" != '' ] && [ "$user_id" = "$username" ] && echo "user : $username" && echo 'authentication ok.' && exit 0 || echo 'authentication failed.'; exit 1
    
4. Create file login.sh '/etc/openvpn/script/login.sh'
#!/bin/bash
. /etc/openvpn/script/config.sh
##Authentication
user_id=$(mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -sN -e "select user_id from user where user_id = '$username' AND user_pass = '$password' AND user_enable=1 AND user_start_date != user_end_date AND TO_DAYS(now()) >= TO_DAYS(user_start_date) AND (TO_DAYS(now()) <= TO_DAYS(user_end_date) OR user_end_date='0000-00-00')")
##Check user
[ "$user_id" != '' ] && [ "$user_id" = "$username" ] && echo "user : $username" && echo 'authentication ok.' && exit 0 || echo 'authentication failed.'; exit 1
    
5. Create file connect.sh '/etc/openvpn/script/connect.sh'
#!/bin/bash
. /etc/openvpn/script/config.sh
##insert data connection to table log
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "INSERT INTO log (log_id,user_id,log_trusted_ip,log_trusted_port,log_remote_ip,log_remote_port,log_start_time,log_end_time,log_received,log_send) VALUES(NULL,'$common_name','$trusted_ip','$trusted_port','$ifconfig_pool_remote_ip','$remote_port_1',now(),'0000-00-00 00:00:00','$bytes_received','$bytes_sent')"
##set status online to user connected
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE user SET user_online=1 WHERE user_id='$common_name'"
    
6. Create file disconnect.sh '/etc/openvpn/script/disconnect.sh'
#!/bin/bash
. /etc/openvpn/script/config.sh
##set status offline to user disconnected
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE user SET user_online=0 WHERE user_id='$common_name'"
##insert data disconnected to table log
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE log SET log_end_time=now(),log_received='$bytes_received',log_send='$bytes_sent' WHERE log_trusted_ip='$trusted_ip' AND log_trusted_port='$trusted_port' AND user_id='$common_name' AND log_end_time='0000-00-00 00:00:00'"
    
Compose OpenVPN configuration files, OpenVPN server will scan .conf files in /etc/openvpn when it starts.
For each file, it forks a daemon. In this system,
we need both UDP and TCP support. I created two configuration files for two daemons in charge of UDP and TCP respectively.
7. Create file server-tcp-443.conf '/etc/openvpn/server-tcp-443.conf' for Server Port:443
##protocol port
port 443
proto tcp
dev tun

##ip server client
server 10.4.0.0 255.255.255.0

##key
ca /etc/openvpn/keys/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key
dh /etc/openvpn/keys/dh1024.pem

##option
persist-key
persist-tun
keepalive 5 60
reneg-sec 432000

##option authen.
comp-lzo
user nobody
#group nogroup
client-to-client
username-as-common-name
client-cert-not-required
auth-user-pass-verify /etc/openvpn/script/login.sh via-env

##push to client
max-clients 50
push "persist-key"
push "persist-tun"
push "redirect-gateway def1"
#push "explicit-exit-notify 1"

##DNS-Server
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"

##script connect-disconnect
script-security 3 system
client-connect /etc/openvpn/script/connect.sh
client-disconnect /etc/openvpn/script/disconnect.sh

##log-status
status /etc/openvpn/log/tcp_443.log
log-append /etc/openvpn/log/openvpn.log
verb 3
    
8. Create file server-udp-53.conf '/etc/openvpn/server-udp-53.conf' for Server Port:53
##protocol port
port 53
proto udp
dev tun

##ip server client
server 10.5.0.0 255.255.255.0

##key
ca /etc/openvpn/keys/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key
dh /etc/openvpn/keys/dh1024.pem

##option
persist-key
persist-tun
keepalive 5 60
reneg-sec 432000

##option authen.
comp-lzo
user nobody
#group nogroup
client-to-client
username-as-common-name
client-cert-not-required
auth-user-pass-verify /etc/openvpn/script/login.sh via-env

##push to client
max-clients 50
push "persist-key"
push "persist-tun"
push "redirect-gateway def1"
push "explicit-exit-notify 1"

##DNS-Server
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"

##script connect-disconnect
script-security 3 system
client-connect /etc/openvpn/script/connect.sh
client-disconnect /etc/openvpn/script/disconnect.sh

##log-status
status /etc/openvpn/log/udp_53.log
log-append /etc/openvpn/log/openvpn.log
verb 3
    
9. Create directory for log '/etc/openvpn/log'
mkdir /etc/openvpn/log
touch /etc/openvpn/log/openvpn.log
touch /etc/openvpn/log/tcp_443.log
touch /etc/openvpn/log/udp_53.log
    
10. Changes the permission of files
chmod -R 755 /etc/openvpn
    
11. Test authentication username 'test' and password '1234'
/etc/openvpn/script/test_connect_db.sh test 1234
# user : test
# authentication ok.
# if authentication failed. check user and password in database
# or detail database server in /etc/openvpn/script/config.sh
    
12. Start serviece OpenVPN
/etc/init.d/openvpn start
    
IV. Share Internet to Client
1. Edit file /etc/sysctl.conf Remove # In line : #net.ipv4.ip_forward=1
net.ipv4.ip_forward=1
    
3. Edit file /etc/rc.local Add before exit 0;
echo "1" > /proc/sys/net/ipv4/ip_forward
echo "1" > /proc/sys/net/ipv4/ip_dynaddr

iptables -A INPUT -i tun0 -j ACCEPT
iptables -A FORWARD -i tun0 -j ACCEPT

iptables -A INPUT -i tun1 -j ACCEPT
iptables -A FORWARD -i tun1 -j ACCEPT

iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT

iptables -t nat -A POSTROUTING -s 10.4.0.0/24 -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 10.5.0.0/24 -o eth0 -j MASQUERADE
    
4. Run Script Iptables Share Internet
/etc/rc.local
iptables-save
    
V. Config for Client
1. Config for port TCP port 443
client
dev tun

proto tcp
remote 1.1.1.1 443

nobind
auth-user-pass
reneg-sec 432000
resolv-retry infinite

ca ca.crt
comp-lzo
verb 1
    
2. Config for port UDP port 53
client
dev tun

proto udp
remote 1.1.1.1 53

nobind
auth-user-pass
reneg-sec 432000
resolv-retry infinite

ca ca.crt
comp-lzo
verb 1        
    
3. Copy file ca.crt from /etc/openvpn/keys/ca.crt to same config path in client
Day of user in database
# If today = '2012-01-01'
# day = user_start_date | user_end_date 
# 0   = 0000-00-00 | 0000-00-00
# 0   = 2012-01-01 | 2012-01-01
# 0   = 2012-01-02 | 2012-01-01
# 1   = 2012-01-01 | 2012-01-02
# unlimited =  2012-01-01 | 0000-00-00
Install finish.

14 June 2012

MySQL update update more than one table

MySQL update update more than one table In a MySQL update statement you can update more than one table like this
UPDATE tableA A
JOIN tableB B ON A.ID = B.ID
SET B.status = '1', B.status = '1'
WHERE A.ID = 'ID'
Or
UPDATE C
JOIN tableB B ON B.ID = C.ID
JOIN tableA A ON A.ID = B.ID
SET C.status = '1', A.status = '1'
WHERE A.ID = '1' OR A.ID = '2' OR A.ID = '3'

11 June 2012

SQL query : count and result in MySQL

SQL query : count and result one query in MySQL Table: banks
bank_id bank_name bank_abbrev
1 Bangkok BBL
2 Kasikornthai KBANK
3 Siam Commercial SCB
4 Krungthai KTB
5 Krungsri BAY
SQL query:
SELECT b.*, t.total
FROM banks b
LEFT JOIN (
  SELECT count(*) AS total
  FROM banks
) AS t ON b.bank_id != -1
LIMIT 0, 3
Query results:
bank_id bank_name bank_abbrev total
1 Bangkok BBL 5
2 Kasikornthai KBANK 5
3 Siam Commercial SCB 5

01 February 2012

VirtualHost on Linux

VirtualHost on Linux Create file :
<VirtualHost *:80>
  ServerAdmin admin@project
  ServerName  project.dev
  DocumentRoot /home/Workspace/project
  <Directory /home/Workspace/project/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>
  ErrorLog "|/usr/sbin/rotatelogs /home/log/apache_logs/project/error.log.%Y-%m-%d-%H_%M_%S 86400"
  CustomLog "|/usr/sbin/rotatelogs /home/log/apache_logs/project/access.log.%Y-%m-%d-%H_%M_%S 86400" combined
</VirtualHost>
Make link
ln -s /etc/apache2/sites-available/project /etc/apache2/sites-enabled/.
Insert to file
127.0.0.1   project.dev
Create dir for log
mkdir /home/log/apache_logs/project
Restart Apache
/etc/init.d/apache2 restart

.htacceess: Invalid command 'RewriteEngine'

.htacceess: Invalid command 'RewriteEngine' You haven't loaded mod_rewrite. (as a superuser)
a2enmod rewrite
apache2ctl restart

13 December 2011

sudoers command in root user

sudoers command in root user file :
user_group    ALL=(ALL) NOPASSWD: /usr/local/bin/command.sh
#Cmnd_Alias   COMMAND_USER = /usr/local/bin/command.sh
#user_group   ALL=(ALL) NOPASSWD: COMMAND_USER

06 July 2011

ASP.NET Error The conversion of a nvarchar data type to a smalldatetime data type resulted in an out-of-range value.

ASP.NET Error The conversion of a nvarchar data type to a smalldatetime data type resulted in an out-of-range value. Can fix:
userQuizDataSource.InsertParameters.Add(
    "DateTimeComplete",
    TypeCode.DateTime,
    DateTime.Now.ToString()
);

28 March 2011

ReadFile Encoding UTF-8 - java

ReadFile Encoding UTF-8 - java
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadUTF8Data {
    private static final String FILE_ENCODE = "UTF-8";
    public static void main(String[] args) {
        String data = null;
        BufferedReader dataIns = null;
        String file = "";
        try {
            dataIns = new BufferedReader(new InputStreamReader(
            new FileInputStream(file), FILE_ENCODE));
            
            while ((data = dataIns.readLine()) != null) {
                System.out.println("Line 1 :" + data);
            }
            dataIns.close();
            dataIns = null;
            } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

15 March 2011

mysql server เข้าใช้ได้จากเครื่องอื่น

mysql server เข้าใช้ได้จากเครื่องอื่น แก้ไฟล์ /etc/mysql/my.cnf
bind-address  = 127.0.0.1
เป็น
#bind-address  = 127.0.0.1
กำหนดสิทธิ์การเข้าใช้ Database
mysql> GRANT ALL ON DB_NAME.* TO 'USERNAME'@"%" IDENTIFIED BY 'NEW_PASSWORD';

08 March 2011

Turn on Bash Smart Completion

Turn on Bash Smart Completion The Bash shell has this sweet feature where you can use the TAB key to auto-complete certain things. For example, when I am in my home directory, the following command:
cd Do[TAB-key]
will automatically yield:
cd Documents
If you are an absolute novice, like I was, not so long ago, discovering tab completion in the terminal can make you go “Wow!”. Wait till you hear the rest now Though you can use the TAB key to complete the names of files and directories, by default the completion is pretty “dumb”. If you have already typed
cd D
you would expect that the tab key would cause only the directory names to be completed, but if I try it on my machine, the tab completion tool uses filenames too. Now, don’t despair! There is now a smart bash tab completion trick you can use. Smart completion even complete the arguments to commands!! To enable smart completion, edit your
/etc/bash.bashrc
file. Uncomment the following lines, by removing the # in the beginning of the lines:
#if [ -f /etc/bash_completion ]; then
# . /etc/bash_completion
#fi
Now you can use tab completion to power your way through commands. You can even extend bash smart completion to your own favourite commands
by using , the “complete” utility and
Explaining the nitty-gritty is beyond me. I refer you to the Debian Administration gurus for more information regarding smarter bash completion.

25 February 2011

ป้องกันการโดนโจมตีเครื่องเซิร์ฟเวอร์ด้วย iptable

ป้องกันการโดนโจมตีเครื่องเซิร์ฟเวอร์ด้วย iptable IPTABLES เป็น Firewall พื้นฐานของ Linux เกือบทุก Distro และให้ประสิทธิภาพที่สูงมากในการ Filtering Traffic และ การป้องกันการ Attack ต่างๆ โดยที่จะมีตัวอย่างพอสังเขป ดังนี้ เปิดการใช้งาน IP Forward ป้องกัน Syn Flood และ อนุญาติให้มีการใช้งานแบบ Dynamic IP (ต่อเนต DSL ทั่วไป)
echo 1 > /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
echo 1 > /proc/sys/net/ipv4/ip_dynaddr
Drop Packet ก่อนหน้านี้ทั้งหมด
iptables -F INPUT
iptables -F FORWARD
iptables -F OUTPUT
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -i lo -j ACCEPT
อนุญาติเฉพาะ SSH, SMTP, DNS, Web Services, SSL และ POP3 ให้ผ่านเข้าออก
iptables -A INPUT -p tcp -m state –state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp –dport 22 –syn -j ACCEPT
iptables -A INPUT -p tcp –dport 25 –syn -j ACCEPT
iptables -A INPUT -p tcp –dport 53 –syn -j ACCEPT
iptables -A INPUT -p udp –dport 53 –syn -j ACCEPT
iptables -A INPUT -p tcp –dport 80 –syn -j ACCEPT
iptables -A INPUT -p tcp –dport 443 –syn -j ACCEPT
iptables -A INPUT -p tcp –dport 110 –syn -j ACCEPT
ป้องกันการ scan ports
iptables -N check-flags
iptables -F check-flags
iptables -A check-flags -p tcp –tcp-flags ALL FIN,URG,PSH -m limit –limit 5/minute -j LOG –log-level alert –log-prefix "NMAP:"
iptables -A check-flags -p tcp –tcp-flags ALL FIN,URG,PSH -j DROP
iptables -A check-flags -p tcp –tcp-flags ALL ALL -m limit –limit 5/minute -j LOG –log-level 1 –log-prefix "XMAS:"
iptables -A check-flags -p tcp –tcp-flags ALL ALL -j DROP
iptables -A check-flags -p tcp –tcp-flags ALL SYN,RST,ACK,FIN,URG -m limit –limit 5/minute -j LOG –log-level 1 –log-prefix "XMAS-PSH:"
iptables -A check-flags -p tcp –tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
iptables -A check-flags -p tcp –tcp-flags ALL NONE -m limit –limit 5/minute -j LOG –log-level 1 –log-prefix "NULL_SCAN:"
iptables -A check-flags -p tcp –tcp-flags ALL NONE -j DROP
iptables -A check-flags -p tcp –tcp-flags SYN,RST SYN,RST -m limit –limit 5/minute -j LOG –log-level 5 –log-prefix "SYN/RST:"
iptables -A check-flags -p tcp –tcp-flags SYN,RST SYN,RST -j DROP
iptables -A check-flags -p tcp –tcp-flags SYN,FIN SYN,FIN -m limit –limit 5/minute -j LOG –log-level 5 –log-prefix "SYN/FIN:"
iptables -A check-flags -p tcp –tcp-flags SYN,FIN SYN,FIN -j DROP
ป้องกันการ flood SSH (SSH Brute Force)
iptables -I INPUT -p tcp –dport 22 -i eth0 -m state –state NEW -m recent –set
iptables -I INPUT -p tcp –dport 22 -i eth0 -m state –state NEW -m recent –update –seconds 600 –hitcount 2 -j DROP
ห้าม ping
iptables -A INPUT -p ICMP -i eth0 –icmp-type 8 -j DROP
ห้าม traceroute
iptables -A INPUT -p ICMP -i eth0 –icmp-type 11 -j DROP
Protect Syn Flood
iptables -N syn-flood
iptables -A syn-flood -i eth0 -m limit –limit 75/s –limit-burst 100 -j RETURN
iptables -A syn-flood -j LOG –log-prefix "SYN-FLOOD: "
iptables -A syn-flood -j DROP
REDIRECT PORT 10080 to 80 (192.168.xxx.xxx = ip ของเรา)
iptables -t nat -A PREROUTING -i eth0 -p tcp –dport 80 -j DNAT –to 192.168.xxx.xxx:10080
iptables -A FORWARD -p tcp -i eth0 -d 192.168.xxx.xxx –dport 80 -j ACCEPT
iptables -A FORWARD -p tcp -i eth0 -d 192.168.xxx.xxx –sport 80 -j ACCEPT
Transparent Proxy
iptables -t nat -A PREROUTING -p TCP –dport 80 -j REDIRECT -to-ports 3128

14 February 2011

DNS Thailand

DNS Thailand Dns in thailand and other
#true-old
203.144.255.71
203.144.255.72

#true-new
203.144.207.29
203.144.207.49
 
#A-net
Primary Domain Name Server (DNS) : 203.148.255.70
Secondary Domain Name Server (DNS) : 203.148.255.80

#Asia Access
Primary Domain Name Server (DNS) : 203.145.0.1
Secondary Domain Name Server (DNS) : 203.145.0.2

#Asia Infonet
Primary Domain Name Server (DNS) : 203.144.255.71
Secondary Domain Name Server (DNS) : 203.144.255.72

#Assumption University ABAC
Primary Domain Name Server (DNS) : 202.6.100.1
Secondary Domain Name Server (DNS) : 202.6.100.2

#CS Internet
Primary Domain Name Server (DNS) : 202.183.255.20
Secondary Domain Name Server (DNS) : 202.183.255.21

#CWN internet ( Chomanan Worldnet )
Primary Domain Name Server (DNS) : 202.74.32.3
Secondary Domain Name Server (DNS) : 202.74.32.4

#DataLine Thai
Primary Domain Name Server (DNS) : 202.80.252.1
Secondary Domain Name Server (DNS) : 202.80.252.2

#Internet Thailand
Primary Domain Name Server (DNS) : 202.44.202.2
Secondary Domain Name Server (DNS) : 202.44.202.3
Proxy Cache Server : proxy.inet.co.th Port : 8080

#Jasmine Internet ( Ji-Net )
Primary Domain Name Server (DNS) : 203.147.0.2
Secondary Domain Name Server (DNS) : 203.147.0.3

#KSC
Primary Domain Name Server (DNS) : 203.155.33.1
Secondary Domain Name Server (DNS) : 202.44.144.33
Incoming mail (POP3) : ksc.th.com or 203.155.33.38
Outgoing mail (SMTP) : ksc.th.com or 203.155.33.38

#Loxinfo
Primary Domain Name Server (DNS) : 203.146.0.20
Secondary Domain Name Server (DNS) : 203.146.0.30

#MWEB
Primary Domain Name Server (DNS) : 203.155.33.1
Secondary Domain Name Server (DNS) : 202.44.144.33

#Samart Infonet
Primary Domain Name Server (DNS) : 203.149.0.2
Secondary Domain Name Server (DNS) : 203.149.0.3

#Siam Global Access
Primary Domain Name Server (DNS) : 202.51.128.65
Secondary Domain Name Server (DNS) : 202.51.128.66

#TOT online
Primary Domain Name Server (DNS) : 203.113.93.1
Secondary Domain Name Server (DNS) : 203.113.93.2
 
#บริษัท เค เอส ซี คอมเมอร์เชียล อินเตอร์เนต จำกัด
203.155.33.1
202.44.144.33
203.155.33.3
203.155.33.44

#บริษัท ทรู อินเทอร์เน็ต จำกัด
203.144.255.71
203.144.207.29
203.144.207.49

#บริษัท กสท โทรคมนาคม จำกัด (มหาชน)
202.129.27.135
202.129.27.133

#บริษัท ทีโอที จำกัด (มหาชน)
203.113.24.199
203.113.127.199
203.113.15.99
203.113.15.100
203.113.15.133
203.113.15.143

#บริษัท อินเทอร์เน็ตประเทศไทย จำกัด (มหาชน) หรือไอเน็ต
203.150.213.1
203.150.218.161
202.44.202.2
202.44.202.3

#บริษัท ซีเอส ล็อกซอินโฟ จำกัด (มหาชน)
203.146.237.237
203.146.237.222
203.146.0.30
203.146.0.20

#บริษัท จัสมิน อินเตอร์เนต จำกัด
203.147.0.3
203.147.0.2
203.130.159.20
203.130.159.21

#บริษัท ทีทีแอนด์ที จำกัด (มหาชน)
202.69.137.131
202.69.137.132
202.69.137.83
202.69.137.84
 
##OpenDns
208.67.220.220
208.67.222.222

##GoogleDns
8.8.4.4
8.8.8.8
 
server=202.69.137.137 # Triple T Global Net 41
server=202.69.137.138 # Triple T Global Net 45
server=203.146.237.237 # csloxinfo 39
server=203.121.130.40 # Pacific 39.9
server=203.144.255.72 # asianet 40.9
server=202.44.144.33 # KSC 41.5
server=203.146.102.231 # truefaster 42.5
server=203.144.255.71 # asianet 42.8
server=202.129.27.134 # cattelecom 43.6
server=203.146.237.222 # csloxinfo 48.5
server=202.57.160.129 # Issp 48.9
server=202.6.100.1 # ABAC 50
server=203.121.130.39 # Pacific 51.5
server=203.155.33.1 # KSC 51.9
server=202.44.68.3 # Sripatum 52.7
server=203.144.207.49 # asianet (True) 54.5
server=203.144.207.29 # asianet (True) 58.1
server=202.57.128.71 # SGA 62
server=203.147.0.3 # Jasmine Internet 62.4
server=61.19.245.246 # cattelecom *
server=61.19.254.134 # cattelecom *
server=202.47.249.4 # cattelecom *
server=203.148.255.70 # A-Net *
server=203.148.255.78 # A-Net *
server=203.149.0.2 # Samart *
server=203.149.0.3 # Samart *
server=203.146.222.3 # buddybb *
server=203.158.144.1 # Rmutp *
server=203.146.64.33 # Tnet *
server=202.182.0.1 # FarEast *
server=202.182.0.2 # FarEast *
server=192.150.249.11 # Thammasat *
server=208.67.222.222 # OpenDNS 277.8
server=208.67.220.220 # OpenDNS 285.1

21 January 2011

Set Zone Time in Debian

Set Zone Time in Debian Set Zeone Time
dpkg-reconfigure tzdata
Check
date

How to kill a thread in delphi?

How to kill a thread in delphi? TMyThread Execute
procedure TMyThread.Execute;
begin
  while not Terminated do begin
    //Here you do a chunk of your work.
    //It's important to have chunks small enough so that "while not Terminated"
    //gets checked often enough.
  end;
  //Here you finalize everything before thread terminates
end;
With this, you can call
MyThread.Terminate;
There is another method, called 'forced termination'. You can call
TerminateThread(MyThread.ThreadId);

13 December 2010

java JDBC MySQL Connect

java JDBC MySQL Connect Table: test
ID Name
1 A
2 B
3 C

import java.io.File;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {   
    public static void main(String args[]) {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost","USERNAME", "PASSWORD");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from test");
            int count = 0;
            while(rs.next())
            {
                System.out.println("ID : " + rs.getString(0));
                System.out.println("NAME : " + rs.getString(1));
                count++;
            }
            System.out.println("sum = "+count);
            
            } catch(Exception e) {
            e.printStackTrace();
            System.out.println("Exception: " + e.getMessage());
            } finally {
            try {
                if(con != null)
                con.close();
        } catch(SQLException e) {}
        }
    }
}

11 December 2010

List ชื่อไฟล์ใน Directory - java

List ชื่อไฟล์ใน Directory ด้วย java อ่านชื่อไฟล์ทั้งหมดใน Directory ตัวอย่าง แสดงเฉพาะ .txt และ .TXT
import java.io.File;
public class ListFiles
{
    public static void main(String[] args)
    {
        // Directory path here
        String path = ".";
        String files_name;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        for (int i = 0; i < listOfFiles.length; i++)
        {
            if (listOfFiles[i].isFile())
            {
                files_name = listOfFiles[i].getName();
                if (files_name.endsWith(".txt") || files_name.endsWith(".TXT"))
                {
                    System.out.println(files_name);
                }
            }
        }
    }
}

23 November 2010

URLReader - java

URLReader ด้วย java อ่าน HTML code จากเว็บ
import java.net.*;
import java.io.*;
public class URLReader  {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.google.com");
        BufferedReader in = new BufferedReader(
        new InputStreamReader(
        url.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
        in.close();
    }
}

16 October 2010

การใช้งาน และ config github เบื้องต้น

การใช้งาน และ config github เบื้องต้น - สมัครสมาชิก github ที่
- สร้าง Github Repository โดยเลือกที่
กรอกรายละเอียดให้ครบ หลังจากนั้นจะเห็นการแนะนำขั้นตอนการใช้งาน
#Global setup:
#Set up git
  git config --global user.name "Chagridsada  Boonthus"
  git config --global user.email mr.tumcpe@gmail.com
      
#Next steps:
  mkdir chagridsada-blog
  cd chagridsada-blog
  git init
  touch README
  git add README
  git commit -m 'first commit'
  git remote add origin https://github.com/chagridsada/chagridsada-blog.git
  git push -u origin master
      
#Existing Git Repo?
  cd existing_git_repo
  git remote add origin https://github.com/chagridsada/chagridsada-blog.git
  git push -u origin master   

#Importing a Subversion Repo?
#Push the repo    
  git remote add origin git@github.com:chagridsada/chagridsada-blog.git
  git push origin master

#When you're done:
#Continue
- Download และ ติดตั้ง Git ได้ที่
- เพิ่ม public key เพื่อให้สามารถติดต่อกับ Github ได้โดย
1.สร้าง public key โดยดูขั้นตอนจาก 2.ไปที่หน้า profile เลือก 3.เลือกที่ 4.นำ public key ที่ได้มาใส่ แล้วบันทึก - ทดสอบสร้าง project ตามขั้นตอนที่ได้มาตอนแรกเป็นอันเสร็จ.

13 October 2010

การติดตั้ง Apache, PHP, MySQL และ phpMyAdmin บน Ubuntu

การติดตั้ง Apache, PHP, MySQL และ phpMyAdmin บน Ubuntu การติดตั้ง Apache
1. เปิด Terminal จาก Applications/Utilities/Terminal จากนั้นให้พิมพ์คำสั่งนี้
sudo apt-get install apache2
2. หากใน Terminal เห็นข้อความว่า
gksu vi /etc/apache2/conf.d fqdn
เมื่อเปิดเสร็จแล้ว ให้พิมพ์ ServerName localhost ลงไปในไฟล์และบันทึก จากนั้นก็ปิดไฟล์ 3. เมื่อติดตั้งเสร็จแล้ว ทดสอบโดยใฃ้เบราว์เซอร์ และเปิด http://localhost
หากใช้งานได้จะพบข้อความว่า It works!
การติดตั้ง PHP5
1. พิมพ์คำสั่ง
sudo apt-get install php5 libapache2-mod-php5
2. เมื่อลง PHP5 เสร็จแล้วจำเป็นต้องเริ่มการทำงานของ Apache อีกครั้ง ด้วยการใช้คำสั่ง
sudo /etc/init.d/apache2 restart
3. ทดสอบการใช้งาน PHP โดยพิมพ์คำสั่ง
sudo vi /var/www/info.php
- เมื่อไฟล์ถูกเปิดขึ้นมาแล้ว ให้พิมพ์โค้ด PHP ดังนี้
<?php
    phpinfo();
?>
    
- จากนั้นให้บันทึกไฟล์และปิดไฟล์นี้ 4. ทดสอบโดยใฃ้เบราว์เซอร์ และเปิด http://localhost/info.php หากใช้งานได้จะผลดังนี้
Display PHP Info
การติดตั้ง MySQL
1. พิมพ์คำสั่ง
sudo apt-get install mysql-server
2. เมื่อเห็นภาพดังด้านล่างนี้ ให้กรอกรหัสผ่านสำหรับผู้ใช้ root ฃอง MySQL
Configuring mysql-server-5.0
3. หลังจากติดตั้งเสร็จ จะต้องให้ PHP ทำงานร่วมกับ MySQL ได้โดยเปิดไฟล์ php.ini ด้วยคำสั่งนี้
gksudo vi /etc/php5/apache2/php.ini
- จากนั้นแก้ไขบรรทัดที่มี
;extension=msql.so
เป็น
extension=msql.so
- ต่อไปก็เริ่มการทำงาน Apache อีกครั้งด้วยคำสั่ง
sudo /etc/init.d/apache2 restart
การติดตั้ง phpMyAdmin
1. พิมพ์คำสั่ง
sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin
2. เมื่อเห็นภาพดังด้านล่างนี้ ให้เลือก Apache2
Configuring phpmyadmin
3. สร้างทางลัดใน /var/www
sudo ln -s /usr/share/phpmyadmin/ /var/www/phpmyadmin
4. เมื่อติดตั้งเสร็จแล้ว ให้ทดสอบโดยใฃ้เบราว์เซอร์ และเปิด http://localhost/phpmyadmin จะได้ผลดังนี้
Display phpMyAdmin

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Blog by Chagridsada Boonthus | http://chagridsada.blogspot.com/