阿里雲服務器又漲價了,感受已經負擔不起了,可是又想擁有一臺本身的私人服務器。某天,忽然發現了樹莓派,價格便宜、高可用。心血來潮!!說搞就搞!!java
原文地址:http://www.uthinks.com/wordpress/2017/12/30/raspberrypi-init/
相關文章:
1.《基於Hexo + Git + Nginx的博客發佈》http://www.uthinks.com/wordpress/2017/12/31/%E5%9F%BA%E4%BA%8Ehexo-git-nginx%E7%9A%84%E5%8D%9A%E5%AE%A2%E5%8F%91%E5%B8%83/python
下載樹莓派系統鏡像(https://www.raspberrypi.org/downloads/)
nginx
接着就是把鏡像燒入SD卡,windows下須要下載win32diskimager
(http://download.csdn.net/detail/longerzone/7574047)。git
解壓鏡像和win32diskimager(綠色版打開便可使用),插入SD卡 --> 打開win32diskimager --> 添加鏡像 --> 確認SD卡後點擊燒寫便可
json
燒寫結束後,在windows下SD卡會顯示只有不到100M,不用擔憂,由於這個分區只是Linux 的boot分區,而其餘內容做爲Linux 的文件系統被掛載爲ext4 文件系統,Windows 下識別不了而已windows
把燒好的SD卡直接插入樹莓派SD卡槽中,接上電源等待系統安裝完成服務器
系統配置過程仍是碰到不少坑,你們有什麼問題能夠直接聯繫我,我會補充出來hexo
家裏辦理的聯通寬帶有公網IP,決定好好利用,可是公網IP不固定,須要動態修改域名解析。dom
在阿里雲上購買本身喜歡的域名(抓緊時間備案,否則網站會被封)
(https://wanwang.aliyun.com/domain/com?spm=5176.8142029.388261.128.anTrkC)ssh
若是有公網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
(https://develop.aliyun.com/sdk/java?spm=5176.doc29772.416540.246.rjauTQ)
解壓安裝
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
路由器端口映射,配置完成記得點擊保存而且生效
最後一步把動態解析腳本配置到crontab中定時執行
/1 * * * /usr/bin/python /home/bill/basic/BasicTask.py
accessKey、accessKeySecret如何獲取
登陸阿里雲控制檯(https://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" }, ] } }