阿里雲服務器又漲價了,感受已經負擔不起了,可是又想擁有一臺本身的私人服務器。某天,忽然發現了樹莓派,價格便宜、高可用。心血來潮!!說搞就搞!!javascript
下載樹莓派系統鏡像(www.raspberrypi.org/downloads/)
java
接着就是把鏡像燒入SD卡,windows下須要下載win32diskimager
(download.csdn.net/detail/long…python
解壓鏡像和win32diskimager(綠色版打開便可使用),插入SD卡 --> 打開win32diskimager --> 添加鏡像 --> 確認SD卡後點擊燒寫便可
json
燒寫結束後,在windows下SD卡會顯示只有不到100M,不用擔憂,由於這個分區只是Linux 的boot分區,而其餘內容做爲Linux 的文件系統被掛載爲ext4 文件系統,Windows 下識別不了而已windows
把燒好的SD卡直接插入樹莓派SD卡槽中,接上電源等待系統安裝完成服務器
系統配置過程仍是碰到不少坑,你們有什麼問題能夠直接聯繫我,我會補充出來dom
家裏辦理的聯通寬帶有公網IP,決定好好利用,可是公網IP不固定,須要動態修改域名解析。ssh
在阿里雲上購買本身喜歡的域名(抓緊時間備案,否則網站會被封)
(wanwang.aliyun.com/domain/com?…)網站
若是有公網IP配置一條A記錄,若是你使用的是花生殼配置一條CNAME記錄
阿里雲
下面給出的是python主要的核心代碼,若是須要完整的環境代碼請留言聯繫我。樹莓派系統鏡像中自帶Python,仍是很方便的
獲取本身的公網出口IP
import urllib2
def getIp():
try:
ip = visit("http://www.ip138.com/ip2city.asp")
except:
ip = "failed to get internet ip"
return ip
def visit(url):
req = urllib2.Request(url)
opener = urllib2.urlopen(req)
result = opener.read()
return result[result.find('[') + 1: result.find(']')]複製代碼
下載alidns python SDK
(develop.aliyun.com/sdk/java?sp…)
解壓安裝
sudo python setup.py install
安裝alidns python SDK
pip install aliyun-python-sdk-alidns
第1步獲取到本身的公網IP後,調用API設置DNS解析
import json
from aliyunsdkalidns.request.v20150109 import UpdateDomainRecordRequest,DescribeDomainRecordsRequest, \
DescribeDomainRecordInfoRequest, AddDomainRecordRequest
from aliyunsdkcore import client
# 更新域名解析
def updateDns(accessKey, accessKeySecret, hostRecord, dnsType, dnsValue, dnsRecordid, dnsTtl, returnFormat):
print hostRecord, dnsType, dnsValue, dnsRecordid, dnsTtl, returnFormat
clt = client.AcsClient(accessKey, accessKeySecret, 'cn-hangzhou')
request = UpdateDomainRecordRequest.UpdateDomainRecordRequest()
request.set_RR(hostRecord)
request.set_Type(dnsType)
request.set_Value(dnsValue)
request.set_RecordId(dnsRecordid)
request.set_TTL(dnsTtl)
request.set_accept_format(returnFormat)
result = clt.do_action(request)
return result
# 獲取當前的解析IP
def getDnsIp(accessKey, accessKeySecret, dnsRecordid, returnFormat):
clt = client.AcsClient(accessKey, accessKeySecret, 'cn-hangzhou')
request = DescribeDomainRecordInfoRequest.DescribeDomainRecordInfoRequest()
request.set_accept_format(returnFormat)
request.set_RecordId(dnsRecordid)
result = clt.do_action(request)
result = json.JSONDecoder().decode(result)
result = result['Value']
return result複製代碼
路由器端口映射,配置完成記得點擊保存而且生效
accessKey、accessKeySecret如何獲取
登陸阿里雲控制檯(ak-console.aliyun.com/#/accesskey)
域名解析RecoreId如何獲取
# dns_domain 域名 如uthinks.com
def check_records(dnsDomain):
clt = client.AcsClient(accessKeyId, accessKeySecret, 'cn-hangzhou')
request = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest()
request.set_DomainName(dnsDomain)
request.set_accept_format('json')
result = clt.do_action(request)
print result
return result
返回值:
{
"PageNumber": 1,
"TotalCount": 2,
"PageSize": 20,
"RequestId": "***",
"DomainRecords": {
"Record": [
{
"RR": "*",
"Status": "ENABLE",
"Value": "****",
"RecordId": "****",
"Type": "A",
"DomainName": "uthinks.com",
"Locked": false,
"Line": "default",
"TTL": "600"
},
]
}
}複製代碼
注: