矿大计算机学院资源共享计划·博客

0%

在树莓派搭建博客平台

本文首发于作者xzlxr的个人网站,本站获授权转载。

点击访问原文


基本

镜像下载

烧录

选择镜像
使用win32DiskImager烧录
批注 2020-03-05 151401.png

当win10弹出请求格式化窗口时,取消。
烧录成功的分区

批注 2020-03-05 151758.png

使用ssh连接控制 启动并更改root密码

注意 打开daspbian ssh需要 添加 SSH 文件(无后续)
批注 2020-03-05 152449.png

  • 查看 ip

登录路由器管理员页面
或使用ip 扫描器 192.168.1.1-192.168.1.254
我这边是192.168.1.115(有线连接)

  • 使用ssh.exe连接
1
ssh pi@192.168.1.115

需注意raspbian一开始只能使用 pi 用户 且密码为raspberry

  • raspbian
1
2
sudo passwd root #设置密码
sudo passwd --unlock root #开启root账户
  • kali
    默认root 密码为 toor

更新源

  • kali
1
2
3
4
5
6
7
8
9
10
sudo nano /etc/apt/sources.list
#中科大
deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
deb-src http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
#aliyun
deb http://mirrors.aliyun.com/kali kali-rolling main non-free contrib
deb-src http://mirrors.aliyun.com/kali kali-rolling main non-free contrib
#清华
deb http://mirrors.tuna.tsinghua.edu.cn/kali kali-rolling main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/kali kali-rolling main contrib non-free

修改apt软件源 和 系统源

添加进去 ,最好先备份

1
2
3
sudo nano /etc/apt/sources.list 
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi

完成源的更新软件包索引

1
sudo apt-get update

接着更新系统源

1
2
3
4
sudo nano /etc/apt/sources.list.d/raspi.list

deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui

最后sudo apt-get upgrade

配置pip源

1
2
3
4
~/.pip/pip.conf

[global]
index-url = http://pypi.douban.com/simple/

打开 设置wifi

1
2
3
sudo raspi-config
SSID:WIFI_NAME
password:WIFI_Password

在win10通过查看wifi的属性来获取SSID
SSID配置

扩展分区

1
sudo raspi-config #选择7 -> A1

df -h查看

win远程🔗

1.安装相关服务
卸载原tightvnc
sudo apt-get purge tightvnc xrdp
安装tighyvnc xrdp
sudo apt-get install tightvncserver xrdp

2.开启连接
win+r > mstsc
输入用户名和密码就行

查看树莓派 信息

  • CPU温度、CPU占用情况、内存占用情况和硬盘占用

来源
get-raspberry-the-current-status-and-data.html

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
# Return CPU temperature as a character string
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))

# Return RAM information (unit=kb) in a list
# Index 0: total RAM
# Index 1: used RAM
# Index 2: free RAM
def getRAMinfo():
p = os.popen('free')
i = 0
while 1:
i = i + 1
line = p.readline()
if i==2:
return(line.split()[1:4])

# Return % of CPU used by user as a character string
def getCPUuse():
return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))

# Return information about disk space as a list (unit included) # Index 0: total disk space # Index 1: used disk space # Index 2: remaining disk space # Index 3: percentage of disk used def getDiskSpace():
p = os.popen("df -h /")
i = 0
while 1:
i = i +1
line = p.readline()
if i==2:
return(line.split()[1:5])
# CPU informatiom
CPU_temp = getCPUtemperature()
CPU_usage = getCPUuse()

# RAM information
# Output is in kb, here I convert it in Mb for readability
RAM_stats = getRAMinfo()
RAM_total = round(int(RAM_stats[0]) / 1000,1)
RAM_used = round(int(RAM_stats[1]) / 1000,1)
RAM_free = round(int(RAM_stats[2]) / 1000,1)

# Disk information
DISK_stats = getDiskSpace()
DISK_total = DISK_stats[0]
DISK_used = DISK_stats[1]
DISK_perc = DISK_stats[3]

if __name__ == '__main__':
print('')
print('CPU Temperature = '+CPU_temp)
print('CPU Use = '+CPU_usage)
print('')
print('RAM Total = '+str(RAM_total)+' MB')
print('RAM Used = '+str(RAM_used)+' MB')
print('RAM Free = '+str(RAM_free)+' MB')
print('')
print('DISK Total Space = '+str(DISK_total)+'B')
print('DISK Used Space = '+str(DISK_used)+'B')
print('DISK Used Percentage = '+str(DISK_perc))

添加执行权限并执行

1
2
chmod +x get.py
python3 get.py

网站搭建

安装Nginx + php 7.3 + MariaDB

Nginx

1
sudo apt-get install -y nginx

php

1
sudo apt install -y -t buster php7.3-fpm php7.3-curl php7.3-gd php7.3-intl php7.3-mbstring php7.3-mysql php7.3-imap php7.3-opcache php7.3-sqlite3 php7.3-xml php7.3-xmlrpc php7.3-zip

MariaDB

1
sudo apt-get install mariadb-server
  • 设置初始密码及修改密码

https://www.cnblogs.com/messhair/p/11782850.html

1
2
3
4
MariaDB [(none)]> use mysql;
MariaDB [mysql]> UPDATE mysql.user SET password = PASSWORD('xzlxr@mysql') WHERE user = 'root';
MariaDB [mysql]> UPDATE user SET plugin='mysql_native_password' WHERE user = 'root';
MariaDB [mysql]> FLUSH PRIVILEGES; #刷新权限相关表格

restart

1
sudo service mariadb restart

登录

1
mysql -u root -p

输入新密码即可

apache2

1
2
sudo apt install apache2
sudo apt-get install libapache2-mod-php

配置(暂时无安全加固)

Nginx 配置

打开配置文件

1
sudo  nano /etc/nginx/sites-available/default

连接php以及添加 index.php

1
2
sudo service nginx restart 
sudo service php7.3-fpm restart

安装typecho

官网

1
2
sudo wget http://typecho.org/downloads/1.1-17.10.30-release.tar.gz
sudo tar zxvf 1.1-17.10.30-release.tar.gz

wordpass

官网下载

使用 Frp 实现内网穿透,公网访问web服务器,设置自动启动,后台运行

参考:

使用 Frp 实现内网穿透,公网访问web服务器,设置自动启动,后台运行
通过frp穿透内网ssh访问树莓派

https://github.com/fatedier/frp/releases

  • 客户端配置

在树莓派上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
wget https://github.com/fatedier/frp/releases/download/v0.32.0/frp_0.32_linux_arm.tar.gz
tar -zxvf frp_0.32_linux_arm.tar.gz
cd frp_0.32_linux_arm
sudo nano frpc.ini

[common]
token = XXXXXXXX #your_token
server_addr = xxx.xxx.xxx.xxx#服务器ip
server_port = XXXX#服务器端口

[web]
type = http
local_ip = xxx.xxx.xxx.xxx #本地ip
local_port = 80
remote_port = 80
custom_domains = #你的域名

在树莓派上添加自启

1
2
3
4
5
6
7
8
9
10
11
12
13
sudo nano /lib/systemd/system/frpc.service

[Unit]
Description=frpc service
After=network.target syslog.target
Wants=network.target

[Service]
Type=simple
ExecStart=/home/pi/frp/frpc -c /home/pi/frp/frpc.ini

[Install]
WantedBy=multi-user.target

重启Frpc,可以 sudo systemctl restart frpc
停止Frpc,可以 sudo systemctl stop frpc

v2ray

v2ray

SSR

https://suool.net/2018/01/09/%E6%A0%91%E8%8E%93%E6%B4%BE%E6%8A%98%E8%85%BE%E6%8C%87%E5%8D%97%E4%B9%8B%E4%BD%BF%E7%94%A8-SSR-%E5%AE%9E%E7%8E%B0%E7%A7%91%E5%AD%A6%E4%B8%8A%E7%BD%91/

添加xml

一开始想直接用插件的(插件,他不香吗)

但问题是,我的vps是代理转发的,而实际的博客vps地址。。。只有内网地址(192.168.1.xxx),试了google-sitemap(不行),改了wp上的url设置还是不行。。。

最后的方案:

  • 编写sitemap.php

来源

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php

require('./wp-blog-header.php');

header("Content-type: text/xml");

header('HTTP/1.1 200 OK');

$posts_to_show = 1000;

echo '<?xml version="1.0" encoding="UTF-8"?>';

echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'

?>

<!-- generated-on=<?php echo get_lastpostdate('blog'); ?> -->

<url>

<loc><?php echo get_home_url(); ?></loc>

<lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>

<changefreq>daily</changefreq>

<priority>1.0</priority>

</url>

<?php

/* 文章页面 */

$myposts = get_posts( "numberposts=" . $posts_to_show );

foreach( $myposts as $post ) { ?>

<url>

<loc><?php the_permalink(); ?></loc>

<lastmod><?php the_time('c') ?></lastmod>

<changefreq>monthly</changefreq>

<priority>0.6</priority>

</url>

<?php } /* 文章循环结束 */ ?>

<?php

/* 单页面 */

$mypages = get_pages();

if(count($mypages) > 0) {

foreach($mypages as $page) { ?>

<url>

<loc><?php echo get_page_link($page->ID); ?></loc>

<lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00</lastmod>

<changefreq>weekly</changefreq>

<priority>0.6</priority>

</url>

<?php }} /* 单页面循环结束 */ ?>

<?php

/* 博客分类 */

$terms = get_terms('category', 'orderby=name&hide_empty=0' );

$count = count($terms);

if($count > 0){

foreach ($terms as $term) { ?>

<url>

<loc><?php echo get_term_link($term, $term->slug); ?></loc>

<changefreq>weekly</changefreq>

<priority>0.8</priority>

</url>

<?php }} /* 分类循环结束 */?>

<?php

/* 标签(可选) */

$tags = get_terms("post_tag");

foreach ( $tags as $key => $tag ) {

$link = get_term_link( intval($tag->term_id), "post_tag" );

if ( is_wp_error( $link ) )

return false;

$tags[ $key ]->link = $link;

?>

<url>

<loc><?php echo $link ?></loc>

<changefreq>monthly</changefreq>

<priority>0.4</priority>

</url>

<?php } /* 标签循环结束 */ ?>

</urlset>
  • Yoast SEO  #目前来看最好用的SEO插件