1. 程式人生 > >OpenVPN + Php+ Mysql 搭建使用者和證書驗證的VPN伺服器

OpenVPN + Php+ Mysql 搭建使用者和證書驗證的VPN伺服器

本人按照該教程實踐論證,可以正常使用

搭建環境

centos6.5 ,64位 ,minidesktop

openvpn2.0.9

mysql5.1

部分指令碼需要根據自己的需要修改,提供了原作者的一份openvpn配置檔案,裡面的錯誤已經修正

下載地址:http://download.csdn.net/detail/mimi00x/8101427

轉載http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1823384

一、當前Linux為本人定製的作業系統CentOS5.5,自動安裝,大約要10分鐘,安裝完成後將系統更新升級到最新內容,及安裝必要的軟體包;
1、更新系統核心到最新.
#yum -y update
將系統更新到最新版本,系統更新完成後,如果yum安裝時提示錯誤資訊,請執行以下命令修復;
# rpm -import /etc/pki/rpm-gpg/RPM-GPG-KEY-*
2、yum 安裝必要的編譯環境(安裝GCC,GCC++編譯環境)
#yum install gcc gcc-c++


3、 安裝Apahce, PHP, Mysql, 以及php連線mysql庫元件,如果要換行的話請加"\";否則更新不完整;
[

[email protected] ~]# yum -y install httpd php mysql mysql-server php-mysql httpd-manual \
mod_ssl mod_perl         mod_auth_mysql php-mcrypt  php-gd php-xml php-mbstring \
php-ldap php-pear  php-xmlrpc  mysql-connector-odbc mysql-devel libdbi-dbd-mysql \
libgcrypt  libgcrypt-devel openssl openssl-devel  pam  pam-devel  pkgconfig

注意:將以上內容完放在一行執行,將以上的工作完成後就可以接著以下的工作了;
如圖:


安裝完成後啟動相關的服務:
[
[email protected]
~]# chkconfig mysqld on
[[email protected] ~]# chkconfig httpd on
[[email protected] ~]# service httpd start
Starting httpd:                                            [  OK  ]
[[email protected] ~]# [[email protected] ~]# service mysqld start
Starting MySQL:                                            [  OK  ]

4、配置動態域名更新htsprings.3322.org
#yum –y install lynx
安裝完成後設定定時更新:
#crontab -e
*/15 * * * * /usr/bin/lynx -mime_header -auth=nansen:8888888 "http://www.3322.org/dyndns/update?system=dyndns&hostname=test.3322.org"
儲存一下重啟crond服務;以後每隔15分鐘更新一次;
[
[email protected]
/]# service crond restart
停止 crond:                                               [確定]
啟動 crond:                                               [確定]

5、Yum安裝rpm-build,安裝好後可以製作RPM包;
[[email protected] /]# yum -y install rpm-build

6、檢視openssl是否安裝成功;
[[email protected] openvpn]# rpm -qa |grep openssl
openssl-0.9.8e-12.el5_4.6
openssl-devel-0.9.8e-12.el5_4.6

7、注意:關閉SELinux ,iptables


二、OpenVPN軟體及相關軟體下載
(A)準備軟體
1、建立下載軟體目錄;
[[email protected] /]# mkdir -p /home/src/openvpn
[[email protected] /]# cd /home/src/openvpn/
2、下載openvpn-2.0.9.tar.gz軟體,建議不要裝2.1.3版本的軟體(不支援外部驗證);
        # wget http://openvpn.net/release/openvpn-2.0.9.tar.gz
3、安裝 lzo
如果你想使用VPN連線的壓縮特性,或者你想將OpenVPN安裝為一個RPM包,安裝LZO Library。
        #wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.03.tar.gz
4、下載mysql管理軟體phpmyadmin軟體
#wget http://nchc.dl.sourceforge.net/p ... 1.11-english.tar.gz

(B)開始安裝軟體
1、安裝lzo軟體;
[[email protected] openvpn]# ls
[[email protected] openvpn]# tar -zxvf lzo-2.03.tar.gz
[[email protected] openvpn]# cd lzo-2.03
[[email protected] openvpn]# ./configure && make &&make install
等待片刻直到完成,注意看有沒有出錯,一般情況不會有問題;
編輯 /etc/ld.so.conf
[[email protected] lzo-2.03]# cat >> /etc/ld.so.conf << EOF
/usr/local/lib
EOF
或者使用vi /etc/ld.so.conf
加入下內容:/usr/local/lib
編輯完後執行:
[[email protected] lzo-2.03]#ldconfig
使動態連線庫檔案生效,接下來編譯openvpn

2、安裝openvpn軟體
[[email protected] lzo-2.03]# cd /home/src/openvpn
[[email protected] openvpn]# tar -zxvf openvpn-2.0.9.tar.gz
[[email protected] openvpn]# cd openvpn-2.0.9
[[email protected] openvpn-2.0.9]# ./configure && make &&make install
Openvpn預設安裝位置是 /usr/local/sbin/openvpn
[[email protected] openvpn-2.0.9]# whereis openvpn
openvpn: /usr/local/sbin/openvpn

====================================================================================
此部分不必操作,想RPM安裝的上面部分LZO不用裝;
或者使製作RPM包後安裝需要安裝 lzo-devle包,可以從rpmfind.net網站上下載,這兩個軟體我已經下載好了,傳到伺服器上,過程略;
[[email protected] openvpn]# rpm -ivh lzo-2.03-2.1.i386.rpm
Preparing...             ########################################### [100%]
   1:lzo                    ########################################### [100%]
[[email protected] openvpn]# rpm -ivh lzo-devel-2.03-2.1.i386.rpm
Preparing...                ########################################### [100%]
   1:lzo-devel              ########################################### [100%]
[[email protected] openvpn]# rpmbuild -tb openvpn-2.0.9.tar.gz
製作好後,RPM安裝檔案在/usr/src/redhat/RPMS/i386/
[[email protected] openvpn]# cp /usr/src/redhat/RPMS/i386/openvpn-2.0.9-1.i386.rpm /home/src/openvpn
安裝省略
====================================================================================

三、配置openvpn伺服器
1、讓VI顯示彩色;
[[email protected] openvpn]# rpm -e sendmail --nodeps
[[email protected] conf]# vi /root/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias vi='vim'             //加入這一行,編輯器使用VIM
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

2、        建立配置環境
[[email protected] openvpn-2.0.9]# mkdir -p /etc/openvpn/keys
[[email protected] openvpn-2.0.9]# mkdir -p /etc/openvpn/easy-rsa
[[email protected] openvpn-2.0.9]# cp easy-rsa/2.0/* /etc/openvpn/easy-rsa/
[[email protected] openvpn-2.0.9]# cd /etc/openvpn/easy-rsa/
--------------------------------------------------------------------
此目錄下以許多程式及指令碼, 以下為使用到的程式及指令碼說明
vars      指令碼, 是用來建立環境變數,設定所需要要的變數的指令碼
clean-all 指令碼,是建立生成ca證書及金鑰檔案所需要的檔案及目錄
build-ca  指令碼, 生成ca證書(互動)
build-dh  指令碼, 生成Diffie-Hellman檔案(互動)
build-key-server 指令碼, 生成伺服器端金鑰(互動)
build-key 指令碼, 生成客戶端金鑰(互動)
pkitool 指令碼, 直接使用vars的環境變數設定, 直接生成證書(非互動)
---------------------------------------------------------------------
# These are the default values for fields
# which will be placed in the certificate.
# Don't leave any of these fields blank.
export KEY_COUNTRY="CN"
export KEY_PROVINCE="GD"
export KEY_CITY="DG"
export KEY_ORG="OCEAN"
export KEY_EMAIL="[email protected]"
[[email protected] easy-rsa]# vi vars
You have new mail in /var/spool/mail/root
[[email protected] easy-rsa]# source ./vars
NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
[[email protected] easy-rsa]# ./clean-all
[[email protected] easy-rsa]# ./build-ca
Generating a 1024 bit RSA private key
................++++++
...............++++++
writing new private key to 'ca.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:
Country Name (2 letter code) [CN]:
State or Province Name (full name) [GD]:
Locality Name (eg, city) [DG]:
Organization Name (eg, company) [OCEAN]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) [OCEAN CA]:
Email Address [[email protected]]:
                       
[[email protected] easy-rsa]# ./build-key-server server
Generating a 1024 bit RSA private key
................................++++++
...............++++++
writing new private key to 'server.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [GD]:
Locality Name (eg, city) [DG]:
Organization Name (eg, company) [OCEAN]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) [server]:
Email Address [[email protected]]:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/easy-rsa/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           RINTABLE:'CN'
stateOrProvinceName   RINTABLE:'GD'
localityName          RINTABLE:'DG'
organizationName      RINTABLE:'OCEAN'
commonName            RINTABLE:'server'
emailAddress          :IA5STRING:'[email protected]'
Certificate is to be certified until Oct 17 12:24:13 2020 GMT (3650 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
                       
                        [[email protected] easy-rsa]# ./build-key client1
Generating a 1024 bit RSA private key
........++++++
...................++++++
writing new private key to 'client1.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [GD]:
Locality Name (eg, city) [DG]:
Organization Name (eg, company) [OCEAN]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) [client1]:
Email Address [[email protected]]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/easy-rsa/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           RINTABLE:'CN'
stateOrProvinceName   RINTABLE:'GD'
localityName          RINTABLE:'DG'
organizationName      RINTABLE:'OCEAN'
commonName            RINTABLE:'client1'
emailAddress          :IA5STRING:'[email protected]'
Certificate is to be certified until Oct 17 12:26:39 2020 GMT (3650 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
                       
生成完畢後會在keys目錄下多三個檔案出來,client1. crt client.key, client1.csr
Ca.crt ca.key client1. crt client.key, client1.csr五個檔案打包,以備客戶端vpn使用

[[email protected] easy-rsa]# ./build-dh
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
...+.................+.............+..........+........................................................+........+....+........+...................+......................................+........................................................+...............................................++*++*++*

為了防止DDOS,生成ta.key
[[email protected] easy-rsa]# /usr/local/sbin/openvpn --genkey --secret keys/ta.key

壓縮keys下的所有檔案,以方便傳到FTP,供客戶端下載
[[email protected] easy-rsa]# tar -cvf client1.tar.gz keys/*
keys/ca.crt
keys/ca.key
keys/client1.crt
keys/client1.csr
keys/client1.key
[[email protected] easy-rsa]# tar -cvf client1.tar.gz keys/*
keys/01.pem
keys/02.pem
keys/ca.crt
keys/ca.key
keys/client1.crt
keys/client1.csr
keys/client1.key
keys/dh1024.pem
keys/index.txt
keys/index.txt.attr
keys/index.txt.attr.old
keys/index.txt.old
keys/serial
keys/serial.old
keys/server.crt
keys/server.csr
keys/server.key

keys/ta.key [[email protected] easy-rsa]#mv client1.tar.gz /var/ftp/pub
將證書複製到ftp公用目錄下
[[email protected] easy-rsa]# cp -rf keys/* /etc/openvpn/keys/
複製keys下的檔案到/etc/openvpn/keys下
[[email protected] easy-rsa]# rm -rf client*
刪除以client1開頭的檔案

3、複製配置檔案及指令碼;
[[email protected] openvpn]# cd /home/src/openvpn/openvpn-2.0.9
[[email protected] openvpn-2.0.9]# cp sample-config-files/server.conf /etc/openvpn/
[[email protected] openvpn]# vi server.conf
#################################################
# Sample OpenVPN 2.0 config file for            #
# multi-client server.                          #
#                                               #
# This file is for the server side              #
# of a many-clients <-> one-server              #
# OpenVPN configuration.                        #
#                                               #
# OpenVPN also supports                         #
# single-machine <-> single-machine             #
# configurations (See the Examples page         #
# on the web site for more info).               #
#                                               #
# This config should work on Windows            #
# or Linux/BSD systems.  Remember on            #
# Windows to quote pathnames and use            #
# double backslashes, e.g.:                     #
# "C:\\Program Files\\OpenVPN\\config\\foo.key" #
#                                               #
# Comments are preceded with '#' or ';'         #
#################################################

# Which local IP address should OpenVPN
# listen on? (optional)
;local a.b.c.d

# Which TCP/UDP port should OpenVPN listen on?
# If you want to run multiple OpenVPN instances
# on the same machine, use a different port
# number for each one.  You will need to
# open up this port on your firewall.
port 1194
;port 1723
# TCP or UDP server?
;proto tcp
proto udp

# "dev tun" will create a routed IP tunnel,
# "dev tap" will create an ethernet tunnel.
# Use "dev tap0" if you are ethernet bridging
# and have precreated a tap0 virtual interface
# and bridged it with your ethernet interface.
# If you want to control access policies
# over the VPN, you must create firewall
# rules for the the TUN/TAP interface.
# On non-Windows systems, you can give
# an explicit unit number, such as tun0.
# On Windows, use "dev-node" for this.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun

# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel if you
# have more than one.  On XP SP2 or higher,
# you may need to selectively disable the
# Windows firewall for the TAP adapter.
# Non-Windows systems usually don't need this.
;dev-node MyTap

# SSL/TLS root certificate (ca), certificate
# (cert), and private key (key).  Each client
# and the server must have their own cert and
# key file.  The server and all clients will
# use the same ca file.
#
# See the "easy-rsa" directory for a series
# of scripts for generating RSA certificates
# and private keys.  Remember to use
# a unique Common Name for the server
# and each of the client certificates.
#
# Any X509 key management system can be used.
# OpenVPN can also use a PKCS #12 formatted key file
# (see "pkcs12" directive in man page).

ca keys/ca.crt
cert keys/server.crt
key keys/server.key  # This file should be kept secret

# Diffie hellman parameters.
# Generate your own with:
#   openssl dhparam -out dh1024.pem 1024
# Substitute 2048 for 1024 if you are using
# 2048 bit keys.
dh keys/dh1024.pem

# Configure server mode and supply a VPN subnet
# for OpenVPN to draw client addresses from.
# The server will take 10.8.0.1 for itself,
# the rest will be made available to clients.
# Each client will be able to reach the server
# on 10.8.0.1. Comment this line out if you are
# ethernet bridging. See the man page for more info.
server 172.16.0.0 255.255.0.0

# Maintain a record of client <-> virtual IP address
# associations in this file.  If OpenVPN goes down or
# is restarted, reconnecting clients can be assigned
# the same virtual IP address from the pool that was
# previously assigned.
ifconfig-pool-persist ipp.txt

# Configure server mode for ethernet bridging.
# You must first use your OS's bridging capability
# to bridge the TAP interface with the ethernet
# NIC interface.  Then you must manually set the
# IP/netmask on the bridge interface, here we
# assume 10.8.0.4/255.255.255.0.  Finally we
# must set aside an IP range in this subnet
# (start=10.8.0.50 end=10.8.0.100) to allocate
# to connecting clients.  Leave this line commented
# out unless you are ethernet bridging.
;server-bridge 172.16.0.0 255.255.0.0 172.16.0.50 172.16.0.100

# Configure server mode for ethernet bridging
# using a DHCP-proxy, where clients talk
# to the OpenVPN server-side DHCP server
# to receive their IP address allocation
# and DNS server addresses.  You must first use
# your OS's bridging capability to bridge the TAP
# interface with the ethernet NIC interface.
# Note: this mode only works on clients (such as
# Windows), where the client-side TAP adapter is
# bound to a DHCP client.
;server-bridge

# Push routes to the client to allow it
# to reach other private subnets behind
# the server.  Remember that these
# private subnets will also need
# to know to route the OpenVPN client
# address pool (10.8.0.0/255.255.255.0)
# back to the OpenVPN server.

;push "route 172.16.0.0 255.255.0.0"
push "route 192.168.6.0 255.255.255.0"



# To assign specific IP addresses to specific
# clients or if a connecting client has a private
# subnet behind it that should also have VPN access,
# use the subdirectory "ccd" for client-specific
# configuration files (see man page for more info).

# EXAMPLE: Suppose the client
# having the certificate common name "Thelonious"
# also has a small subnet behind his connecting
# machine, such as 192.168.40.128/255.255.255.248.
# First, uncomment out these lines:
;client-config-dir ccd
;route 192.168.40.128 255.255.255.248
# Then create a file ccd/Thelonious with this line:
#   iroute 192.168.40.128 255.255.255.248
# This will allow Thelonious' private subnet to
# access the VPN.  This example will only work
# if you are routing, not bridging, i.e. you are
# using "dev tun" and "server" directives.

# EXAMPLE: Suppose you want to give
# Thelonious a fixed VPN IP address of 10.9.0.1.
# First uncomment out these lines:
;client-config-dir ccd
;route 10.9.0.0 255.255.255.252
# Then add this line to ccd/Thelonious:
#   ifconfig-push 10.9.0.1 10.9.0.2

# Suppose that you want to enable different
# firewall access policies for different groups
# of clients.  There are two methods:
# (1) Run multiple OpenVPN daemons, one for each
#     group, and firewall the TUN/TAP interface
#     for each group/daemon appropriately.
# (2) (Advanced) Create a script to dynamically
#     modify the firewall in response to access
#     from different clients.  See man
#     page for more info on learn-address script.
;learn-address ./script

# If enabled, this directive will configure
# all clients to redirect their default
# network gateway through the VPN, causing
# all IP traffic such as web browsing and
# and DNS lookups to go through the VPN
# (The OpenVPN server machine may need to NAT
# or bridge the TUN/TAP interface to the internet
# in order for this to work properly).
;push "redirect-gateway def1 bypass-dhcp"
;push "redirect-gateway def1"

# Certain Windows-specific network settings
# can be pushed to clients, such as DNS
# or WINS server addresses.  CAVEAT:
# http://openvpn.net/faq.html#dhcpcaveats
# The addresses below refer to the public
# DNS servers provided by opendns.com.
push "dhcp-option DNS 202.96.128.166"
push "dhcp-option DNS 192.168.6.2"

# Uncomment this directive to allow different
# clients to be able to "see" each other.
# By default, clients will only see the server.
# To force clients to only see the server, you
# will also need to appropriately firewall the
# server's TUN/TAP interface.
client-to-client

# Uncomment this directive if multiple clients
# might connect with the same certificate/key
# files or common names.  This is recommended
# only for testing purposes.  For production use,
# each client should have its own certificate/key
# pair.
#
# IF YOU HAVE NOT GENERATED INDIVIDUAL
# CERTIFICATE/KEY PAIRS FOR EACH CLIENT,
# EACH HAVING ITS OWN UNIQUE "COMMON NAME",
# UNCOMMENT THIS LINE OUT.
duplicate-cn

# The keepalive directive causes ping-like
# messages to be sent back and forth over
# the link so that each side knows when
# the other side has gone down.
# Ping every 10 seconds, assume that remote
# peer is down if no ping received during
# a 120 second time period.
keepalive 10 120

# For extra security beyond that provided
# by SSL/TLS, create an "HMAC firewall"
# to help block DoS attacks and UDP port flooding.
#
# Generate with:
#   openvpn --genkey --secret ta.key
#
# The server and each client must have
# a copy of this key.
# The second parameter should be '0'
# on the server and '1' on the clients.
tls-auth keys/ta.key 0 # This file is secret

# Select a cryptographic cipher.
# This config item must be copied to
# the client config file as well.
;cipher BF-CBC        # Blowfish (default)
;cipher AES-128-CBC   # AES
;cipher DES-EDE3-CBC  # Triple-DES

# Enable compression on the VPN link.
# If you enable it here, you must also
# enable it in the client config file.
comp-lzo

# The maximum number of concurrently connected
# clients we want to allow.
max-clients 10

# It's a good idea to reduce the OpenVPN
# daemon's privileges after initialization.
#
# You can uncomment this out on
# non-Windows systems.
user nobody
group nobody

# The persist options will try to avoid
# accessing certain resources on restart
# that may no longer be accessible because
# of the privilege downgrade.
persist-key
persist-tun

# Output a short status file showing
# current connections, truncated
# and rewritten every minute.
status openvpn-status.log

# By default, log messages will go to the syslog (or
# on Windows, if running as a service, they will go to
# the "\Program Files\OpenVPN\log" directory).
# Use log or log-append to override this default.
# "log" will truncate the log file on OpenVPN startup,
# while "log-append" will append to it.  Use one
# or the other (but not both).
log         openvpn.log
log-append  openvpn.log

# Set the appropriate level of log
# file verbosity.
#
# 0 is silent, except for fatal errors
# 4 is reasonable for general usage
# 5 and 6 can help to debug connection problems
# 9 is extremely verbose
verb 3

# Silence repeating messages.  At most 20
# sequential messages of the same message
# category will be output to the log.
;mute 20

;plugin /etc/openvpn/openvpn-auth-pam.so openvpn
;client-cert-not-required
;username-as-common-name


tmp-dir /tmp   
auth-user-pass-verify "php /etc/openvpn/pass.php" via-env
client-cert-not-required
username-as-common-name

client-connect /etc/openvpn/connect.sh
client-disconnect /etc/openvpn/disconnect.sh



====================================
4、啟動openvpn服務
[[email protected]]# cp /hoe/src/openvpn/sample-scripts/openvpn.init /etc/init.d/openvpn
[[email protected]]# vi /etc/init.d/openvpn
                                        修改以下內容到openvpn的安裝位置
openvpn_locations="/usr/local/sbin/openvpn"

第119行去掉#
echo 1 > /proc/sys/net/ipv4/ip_forward
加入:
iptables -t nat -A POSTROUTING -s 10.8.0.0/16 -j SNAT --to 192.168.6.0
這樣客戶端就可以訪問伺服器所在的網路;

                        啟動Openvpn服務
[[email protected] keys]# service openvpn restart
正在關閉openvpn:                                          [確定]
正在啟動 openvpn:                                         [確定]
            [[email protected] keys]# service vsftpd restart
關閉 vsftpd:                                              [失敗]
為 vsftpd 啟動 vsftpd:                                    [確定]

5、        客戶端配置:Winxp SP3,下載客戶端軟體並安裝,我下載的是2.1.3版
配置檔案在安裝目錄下config目錄,可以制樣版配置然後修改;

Client.opvn
client
proto udp
dev tun
;auth-user-pass
auth-nocache
resolv-retry infinite
nobind
persist-key
persist-tun
comp-lzo
verb 3
remote 192.168.0.20 1194

;remote htsprings.3322.org 1194
#####################################################
# 以你客戶端ca認證檔案及金鑰檔案目錄為準
#####################################################
ca ca.crt
cert client1.crt
key client1.key
tls-auth ta.key 1
#####################################################
               
從伺服器上下載客戶端的證書檔案
這是我的伺服器ftp://192.168.0.20,下載後從伺服器上刪除它;

開啟client1.tar.gz這個包,複製客戶端證書到config目錄
Ca.key  ca.crt  client1.key  client1.src client1.crt  ta.key
點選connect,連線成功!
至此以證書方式驗證完成,已測試OK!

三、配置以Php驗證使用者連線;
之前已經安裝好了php mysql httpd等軟體,接下來配置相關的內容;
編輯伺服器配置檔案,當前目錄為/etc/openvpn/:
[[email protected] openvpn]# vi server.conf
加入以下內容並儲存:
tmp-dir /tmp   
auth-user-pass-verify "php /etc/openvpn/pass.php" via-env
client-cert-not-required
username-as-common-name

[[email protected] openvpn]# vi pass.php
#!/usr/bin/php -q
<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "openvpn";
$db_table="vpnuser";
$link =mysql_connect($db_host,$db_user,$db_pass);

#從openvpn獲得需要驗證的使用者名稱以及密碼
$username = getenv('username');
$password = getenv('password');

#初始化認證結果
$final=1;
$final=auth($username,$password,$link);
exit($final);
function auth($username,$password,$link){
        global $db_host,$db_user,$db_pass ,$db_name,$db_table;
        mysql_select_db($db_name,$link);

        $sql = "SELECT * FROM `vpnuser` WHERE `username`='$username' and password=md5('$password') and active='1' and expire_date >now()";
        $result = mysql_query($sql,$link)or die("Invalid query: " . mysql_error());;
        $rows = mysql_fetch_array($result);       

if ($rows)
{
return 0;
}
else
{
return 1;
}
}
?>



修改客戶端配置:
;auth-user-pass   將前面的;去掉,連線時出現輸入使用者名稱對話方塊,
點選connect,輸入用記和密碼即可成功連線。。。。。。。

相關推薦

OpenVPN + Php+ Mysql 搭建使用者證書驗證VPN伺服器

本人按照該教程實踐論證,可以正常使用 搭建環境 centos6.5 ,64位 ,minidesktop openvpn2.0.9 mysql5.1 部分指令碼需要根據自己的需要修改,提供了原作者的一份openvpn配置檔案,裡面的錯誤已經修正 下載地址:http://dow

一步一步教你用PHP+MySql搭建網站 No.0 準備工作

新開一個系列教程吧,這次是講如何用PHP+MySQL搭建網站。 之前一直有想過搭建自己的個人網站,然後上週通過阿里雲申請的域名和空間都通過稽核了,於是就開始研究如何用PHP+MYSQL搭建網站,研究了差不多兩週,總算搞定了一個小型的blog類的網站。當然,整個過程是通過學習

Windows下Apache+PHP+MySQL搭建web伺服器

Apache+PHP+MySQL搭建伺服器 工欲善其事必先利其器。 最近由於電腦出了問題不得不重新安裝需要的檔案,程式碼什麼的都沒了,以前也沒怎麼寫過東西這回就先試試手,寫的不是太好,希望大家不要介意哈。 比較窮開不起CSDN的會員,所以Apache,PHP,mysql

一步一步教你用PHP+MySql搭建網站 No.1 主頁&資料庫連線

這一章節我們來看使用者輸入網頁後的主介面。 一般來說,預設主頁都是index點xxx,比如 index.php, index.html , index.jsp等等。我們來看一下我們的index.php吧 index.php 提醒: 在<?php?>的兩端,不要

PHP MySQL 建立資料庫

建立表 CREATE TABLE 用於在 MySQL 中建立資料庫表。 語法 CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, ....... ) 為了執行此命令

nginx+php+mysql 搭建完整web伺服器

1、獲取相關開源程式【適用CentOS作業系統】利用CentOS Linux系統自帶的yum命令安裝、升級所需的程式庫: sudo -s LANG=C yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-

PHP-Websockets 上傳檔案2 優化支援php socket客戶端websocket連線websocket伺服器 以守護程序方式執行編碼

WebsocketServer: users.php <?php class WebSocketUser { public $socket; public $id; public $headers = array(); public $handsh

使用nginx搭建點播直播流媒體伺服器

#使用的使用者和組 #user  nobody; #指定工作衍生的程序數,為cpu的核心數總和 worker_processes  2; #指定錯誤日誌的存放路徑 日誌記錄級別[debug,info,notice,warn,error,crit] error_log   /usr/local/nginx/lo

CA證書(企業內網搭建CA服務器生成自簽名證書,CA簽署,實現企業內網基於key驗證訪問服務器)

file type ima 1.5 x509 項目 索引 分享 是否 一些CA基礎 PKI:Public Key Infrastructure簽證機構:CA(Certificate Authority)註冊機構:RA證書吊銷列表:CRL X.509:定義了證書的結構以及

php+mysql+nginx+phpmyadmin環境搭建

nts home sem 鏈接 ipv4 1.5 不出 英文 由於 英文原文:http://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/ 參照

如何在Ubuntu 14.04上利用jexus搭建支持php+mysql數據庫的網站服務

建立數據庫 wordpress rpc 一個 upd chmod lca chm spel 準備部分:sudo apt-get update sudo apt-get install unzip -y第一部分:安裝jexus 在終端運行以下命令

window下Apache+php+Mysql環境的搭建及其涉及的知識

並運行 分鐘 sql 網上 文件 多模塊 多功能 服務管理 加密 一.安裝Apache 1. 在網上搜索以下3個文件,以及找一個地方新建一個文件夾 文件夾內有:apache,mysql,php文件夾,最好都不要有中文路徑      http

php+Mysql分頁 類引用詳解

echo padding 數字 進行 else if sub var min func 一下內容為專用於分頁的類以及具體的方法和解析。<?php class Page { private $total;

CentOS7 搭建nginx+php+mysql運行環境

centos7 nginx php mysql 在centOS上有兩種方式可以安裝nginx、php、mysql,即通過yum指令來安裝;通過編譯源 碼安裝。CentOS 7上系統自帶有yum源,下介紹nginx通過源碼及yum安裝的方法及php、 mysql通過yum指令安裝的方法。 安裝ngi

CentOS+Nginx+Tomcat+Mysql+PHP 環境搭建及系統部署

Linux Centos tomcat mysql nginx ==============安裝centos 7.0=======================選擇最小安裝,將相關的"調試工具"、“兼容性程序庫”、“開發工具”選中。此操作是為了減少後期安裝或編譯相關服

Centos上安裝Apache+Mysql+PHP+WordPress搭建博客

下載 .org 一行 reat 接下來 博客 搭建 admin his 1.安裝Apache 輸入這一行安裝Apache sudo yum install httpd 然後啟動 sudo service httpd start 如果想檢驗是否安裝成功,就在瀏覽器中輸入你服務

ubuntu 下搭建redisphp的redis的拓展

nbsp gpo tps AR 服務 pre bsp 技術分享 csdn 系統環境: 騰訊雲服務器, ubuntu16.0.4.4 ,php7.0 一.安裝redis服務 sudo apt-get install redis-server 安裝好的redis目錄

docker搭建apache+php+mysql

docker搭建apache+php+m說明,本實驗在centos7——64位下搭建,兩個容器,用Dockerfie實現容器1:mysql 包括文件Dockerfile, build_table.sh, run.sh容器2:php+apache 包括文件Dockerfile, run.sh測試

[經驗技巧] “php+mysql+apache”環境搭建及"手動SQL註入",20180527-0

版本 lin AC 旗艦 分號 正常 HERE sele primary [經驗技巧] “php+mysql+apache”環境搭建及"手動SQL註入" 1.“php+mysql+apache”環境搭建 環境載體:虛擬機Window7 Service Pack 1

PHP開發大發彩票源碼搭建接口安全驗證

可能 後臺 搭建 轉換成 源碼 取數 取數據 獲取數據 暗號 php的大發彩票源碼搭建接口企 娥:217 1793 408在實際工作中,使用PHP寫api接口是經常做的,PHP寫好接口後,前臺就可以通過鏈接獲取接口提供的數據,而返回的數據一般分為兩種情況,xml和json,