搭建自己的 VPS 服務器

點擊打開鏈接

迫於在國內上網困難的壓力,擁有自己的 VPS 在其搭建自己的 VPN 就顯得很有必要了。但在實際的搭建過程中走了很多的彎路掉進了很多的坑,所以寫了這篇博文爲了以 VPS 重裝系統的時候可以少走彎路,也給有意自己搭建 VPS 的朋友一點指示。

前期準備

需要購買一臺擁有 root 權限的 VPS ,我選擇的是 搬瓦工 ,當時購買的是 512 M 內存 5 G SSD,500 G 流量/月, 9.99 刀每年,但是好像現在這種低價套餐已經結束了。有意的朋友可以看一下其他的套餐或者別的公司的 VPS。有的朋友說 DigitalOcean 的速度非常快,看YouTube直接 1440p,但是我還沒測試過,目前搬瓦工的速度能滿足我的需求,而且 DO 的價格比較昂貴。

下圖是搭了 SS 和 IPsec VPN 服務的使用情況,僅供參考
使用情況

服務器購買後,安裝 CentOS7,因爲以下教程都是基於 CentOS7 的,安裝新的 OS 後,搬瓦工會告訴你 SSH 的端口和 root 的密碼,這些是自己無法自定義的,要記住瞭如果實在忘了也可以重置 root 密碼,或者直接使用搬瓦工提供的在線SSH登錄來操作也可,就是反應比較慢,所以我們以後還是常用 ssh 登錄來配置 VPS ,Mac 下直接使用終端就好,win 下自行尋找一個 ssh 工具就好。

登錄 ssh 的命令:

1
$ ssh -p vps 端口號 root@vpsIP 地址

登錄上以後就相當於在本地操作一樣了,你可以使用各種Linux命令來操作了

配置防火牆

如果 SSH 無法登錄,那說明防火牆關閉了 SSH 端口,需要通過在線 SSH 登錄進去關閉防火牆重新配置

清除防火牆配置
1
$ iptables -F

清除iptabels所有表項,同時nat設置也沒了,但是我們後續的腳本里會配置的,不用擔心.如果 SSH 登錄正常就不用管防火牆.

安裝 firewalld
1
2
$ yum install firewalld firewall-config
$ systemctl start firewalld

P.S. 我在安裝完 firewalld 之後然後啓動服務的時候一直顯示失敗,然後重啓了一遍服務器就可以正常的啓動firewalld 服務了,
有類似情況的朋友可以重啓一下服務器

修改 SSH 端口
1
$ vi /usr/lib/firewalld/services/ssh.xml

會出現一下的內容

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?><service>
   <short>SSH</short>
   <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
   <port protocol="tcp" port="22"/>
</service>

將 port=”22”,修改成搬瓦工提供給你的端口號,然後 reload firewalld 就 OK

vi 的命令: 按 “i” 是編輯模式,編輯後按 “esc” 退出編輯模式,然後按 Shift 輸入 “:wq” 保存退出vi.

1
2
$ firewall-cmd --permanent --add-service=ssh
$ firewall-cmd --reload

OK,現在準備工作都已就緒,安裝了源,安裝配置了防火牆,下一步開始搭建服務了。

搭建 Shadowsocks 服務

這個服務是最簡單也是最常用的

安裝組件

1
2
3
$ yum install m2crypto python-setuptools
$ easy_install pip
$ pip install shadowsocks

安裝時部分組件需要輸入 Y 確認。小內存 VPS 可以分別安裝組件。

安裝完成後配置服務器參數

1
$ vi  /etc/shadowsocks.json

寫入如下配置:

1
2
3
4
5
6
7
8
9
10
11
{
    "server":"0.0.0.0"
    "server_port":8388
    "local_address": "127.0.0.1"
    "local_port":1080
    "password":"mypassword"
    "timeout":300
    "method":"aes-256-cfb"
    "fast_open": false
    "workers": 1
}

將上面的 mypassword 替換成你的密碼, server_port 也是可以修改的,例如 443 是 Shadowsocks 客戶端默認的端口號

如果需要修改端口,需要在防火牆裏打開響應的端口,用 firewalld 操作就比較簡單了

1
$ vi /usr/lib/firewalld/services/ss.xml

下面代碼粘貼到裏面

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SS</short>
  <description>Shadowsocks port
  </description>
  <port protocol="tcp" port="自定義ss端口號"/>
</service>

保存退出,然後重啓 firewalld 服務

1
2
$ firewall-cmd --permanent --add-service=ss
$ firewall-cmd --reload

運行命令,啓動 Shadowsocks 服務

運行下面的命令

1
$ ssserver -c /etc/shadowsocks.json

至此shadowsocks搭建完成,shadowsocks已經可以使用,如果你沒有過高的要求,下面的步驟可以省略,下面是後臺運行 Shadowsocks 的步驟。

安裝 supervisor 實現後臺運行

運行一下命令下載 supervisor

1
2
$ yum install python-setuptools
$ easy_install supervisor

然後創建配置文件

1
$ echo_supervisord_conf > /etc/supervisord.conf

修改配置文件

1
$ vi /etc/supervisord.conf

在文件末尾添加

1
2
3
4
[program:ssserver]command = ssserver -c /etc/shadowsocks.json
autostart=true
autorestart=true
startsecs=3

設置 supervisord 開機啓動,編輯啓動文件

1
$ vi /etc/rc.local

在末尾另起一行添加

1
$ supervisord

保存退出(和上文類似)。另 centOS7 還需要爲 rc.local 添加執行權限

1
$ chmod +x /etc/rc.local

至此運用 supervisord 控制 Shadowsocks 開機自啓和後臺運行設置完成。重啓服務器即可

搭建 Strongswan 實現在 iOS 上連接 VPN

如果你只是需要在 Android, PC 上使用 VPN,那可以直接忽略此章內容, Shadowsocks 已經可以非常完美的幫助以上設備實現翻牆。 但是由於 iOS 上無法使用 Shadowsocks 所以需要使用 Strongswon 建立 IPsecVPN。

下載並編譯 Strongswan

首先我們來編譯 Strongswan, 因爲直接用 yum install 的不能用,原因不明,所以直接下載源碼和依賴包進行編譯

下載 Strongswan 的源碼

1
2
$ wget http://download.strongswan.org/strongswan.tar.gz && tar zxvf strongswan* 
$ cd strongswan*

下載編譯源碼所需要的依賴包(小內存請分批下載)

1
$ yum install -y make gcc gmp-devel openssl openssl-devel

因搬瓦工是 OpenVZ 的所以用下面的命令來進行配置

1
$ ./configure --sysconfdir=/etc --disable-sql --disable-mysql --disable-ldap --enable-dhcp --enable-eap-identity --enable-eap-mschapv2 --enable-md4 --enable-xauth-eap --enable-eap-peap --enable-eap-md5 --enable-openssl --enable-shared --enable-unity --enable-eap-tls   --enable-eap-ttls --enable-eap-tnc --enable-eap-dynamic --enable-addrblock --enable-radattr --enable-nat-transport --enable-kernel-netlink --enable-kernel-libipsec

非 OpenVZ 的請用下面的命令來進行配置

1
./configure --sysconfdir=/etc --disable-sql --disable-mysql --disable-ldap --enable-dhcp --enable-eap-identity --enable-eap-mschapv2 --enable-md4 --enable-xauth-eap --enable-eap-peap --enable-eap-md5 --enable-openssl --enable-shared --enable-unity --enable-eap-tls   --enable-eap-ttls --enable-eap-tnc --enable-eap-dynamic --enable-addrblock --enable-radattr --enable-nat-transport --enable-kernel-netlink

開始編譯源代碼

1
$ make && sudo make install

沒有錯誤出現後,可進行下一步

生成證書

建立個臨時目錄來生成證書

1
$ mkdir ~/ipsec_cert && cd ~/ipsec_cert

生成服務器證書

用的是 iOS8 不越獄翻牆方案中創建的腳本。SERVER 換成自己的域名或IP 都行

1
2
$ wget https://gist.githubusercontent.com/songchenwen/14c1c663ea65d5d4a28b/raw/cef8d8bafe6168388b105f780c442412e6f8ede7/server_key.sh
$ sh server_key.sh SERVER

生成客戶端證書

同樣是他的腳本,這個腳本還會生成一個 .p12 證書,這個證書需要導入到 iOS 裏,USER 換成你自己的用戶名 EMAIL 換成你自己的 email。

1
2
$ wget https://gist.githubusercontent.com/songchenwen/14c1c663ea65d5d4a28b/raw/54843ae2e5e6d1159134cd9a90a08c31ff5a253d/client_key.sh
$ sh client_key.sh USER EMAIL

複製證書到 /etc/ipsec.d/

Strongswan 需要的是 cacerts/strongswanCert.pem certs/vpnHostCert.pem private/vpnHostKey.pem 這三個文件

1
2
3
$ sudo cp cacerts/strongswanCert.pem /etc/ipsec.d/cacerts/strongswanCert.pem 
$ sudo cp certs/vpnHostCert.pem /etc/ipsec.d/certs/vpnHostCert.pem
$ sudo cp private/vpnHostKey.pem /etc/ipsec.d/private/vpnHostKey.pem

同步客戶端證書到本地

客戶端需要的是 .p12 證書和 cacerts/strongswanCert.pem 將這兩個證書同步到本地,然後通過郵件發送到 iOS 設備中並安裝

1
2
$ scp -P ssh端口 root@服務器ip:~/ipsec_cert/****.p12 ~/
$ scp -P ssh端口 root@服務器ip:~/ipsec_cert/cacerts strongswanCert.pem ~/

配置 Strongswan

編輯 /etc/ipsec.conf

1
$ vi /etc/ipsec.conf

將下面的代碼覆蓋原有內容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
config setup
    # strictcrlpolicy=yes
    #  uniqueids = replace
    # charondebug="cfg 2, dmn 2, ike 2, net 0" #要看Log時,取消註釋本行

conn %default
    keyexchange=ikev1
    dpdaction=hold
    dpddelay=600s
    dpdtimeout=5s
    lifetime=24h
    ikelifetime=240h
    rekey=no
    left=emptyzone.github.io #這裏換成你登錄 VPN 用的域名或 IP,與生成證書時相同 
    leftsubnet=0.0.0.0/0
    leftcert=vpnHostCert.pem
    leftsendcert=always
    right=%any
    rightdns=8.8.8.8
    rightsourceip=10.0.0.0/8

conn CiscoIPSec
    rightauth=pubkey
    rightauth2=xauth
    auto=add

編輯 /etc/ipsec.secrets, 創建用戶名及密碼

1
vi /etc/ipsec.secrets

將一下內容添加進去

1
2
3
4
#驗證用戶所需的信息
#用戶名 : EAP "密碼"
: RSA vpnHostKey.pem
你的用戶名 : EAP "你的密碼"

使用 firewalld 配置防火牆

用 firewalld 開放 4500、500 端口和 esp 協議

1
$ vi /usr/lib/firewalld/services/ipsec.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>IPsec</short>
  <description>Internet Protocol Security (IPsec) incorporates security for network transmissions directly into the Internet Protocol (IP). IPsec provides methods for both encrypting data and authentication for the host or network it sends to. If you plan to use a vpnc server or FreeS/WAN, do not disable this option.</description>
  <port protocol="ah" port=""/>
  <port protocol="esp" port=""/>
  <port protocol="udp" port="500"/>
  <port protocol="udp" port="4500"/>
</service>

然後輸入一下命令後,至此整個搭建過程就結束了。

1
2
3
$ firewall-cmd --permanent --add-service=ipsec
$ firewall-cmd --permanent --add-masquerade
$ firewall-cmd --reload

把下載的兩個證書用 email 發送到你的 iOS 上,安裝後建立個 VPN 連接,選 IPsec,使用證書,選擇你的用戶名的證書即可,登錄下試試吧。如有任何疑問請將您的疑問留在評論區。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章