termux搭建kod(可道云)教程

termux搭建kod(可道云)教程

脚本功能

安装相关命令,修改php配置:listen = 127.0.0.1:9000,正则匹配修改,多次追加,nginx配置添加php解析,并且配置如果存在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
pkg install -y nginx php php-fpm expect

if [ ! -f "$PREFIX/etc/php-fpm.d/www.conf_bak" ]; then
cp $PREFIX/etc/php-fpm.d/www.conf $PREFIX/etc/php-fpm.d/www.conf_bak
fi
if [ ! -f "$PREFIX/etc/nginx/nginx.conf_bak" ]; then
cp $PREFIX/etc/nginx/nginx.conf $PREFIX/etc/nginx/nginx.conf_bak
fi
# 修改
# sed -i 's|^\(listen =\).*|\1 0.0.0.0:9000|g' $PREFIX/etc/php-fpm.d/www.conf
sed -i 's|^\(listen =\).*|\1 127.0.0.1:9000|g' $PREFIX/etc/php-fpm.d/www.conf
# 检查修改结果
grep -n "^listen" $PREFIX/etc/php-fpm.d/www.conf

# 修改,index行追加index.php
sed -i 's|\([[:space:]]*index  index.html index.htm\).*|\1 index.php|g' $PREFIX/etc/nginx/nginx.conf
# 添加php解析配置,这段配置只需要执行一次就可以
temp_nginx_conf=$(mktemp) && awk '
BEGIN { depth = 0; target_depth = 2; inserted = 0; already_exists = 0 }
# 先扫描一遍,检查是否已存在该 location 块(简单特征匹配)
NR == FNR {
    if ($0 ~ /location ~ \\\.php\$/) already_exists = 1
    next
}
# 正常处理
{
    if (!inserted && !already_exists && depth == target_depth && $0 ~ /^[[:space:]]*}/) {
        print ""
        print "        location ~ \\.php$ {"
        print "            root           html;"
        print "            fastcgi_pass   127.0.0.1:9000;"
        print "            # fastcgi_pass   0.0.0.0:9000;"
        print "            fastcgi_index  index.php;"
        print "            fastcgi_param  SCRIPT_FILENAME  /data/data/com.termux/files/usr/share/nginx/html$fastcgi_script_name;"
        print "            include        fastcgi_params;"
        print "        }"
        inserted = 1
    }
    if ($0 ~ /\{/) depth++
    if ($0 ~ /\}/) depth--
    print
}
END {
    if (!inserted && !already_exists && depth == target_depth) {
        print "    location ~ \\.php$ { ... }"
    }
}' "$PREFIX/etc/nginx/nginx.conf" "$PREFIX/etc/nginx/nginx.conf" > ${temp_nginx_conf} && mv ${temp_nginx_conf} "$PREFIX/etc/nginx/nginx.conf"

# 下载kodexplorer
if [ ! -f "kodexplorer4.40.zip" ]; then
  wget http://static.kodcloud.com/update/download/kodexplorer4.40.zip
fi

# 解压 到 Nginx 的 kod 目录下
if [ ! -d "$PREFIX/share/nginx/html/kod" ]; then
  unzip kodexplorer4.40.zip -d $PREFIX/share/nginx/html/kod
fi
 
# 挂载termux家目录与手机内部存储目录
# termux-setup-storage
# 自动运行termux-setup-storage,避免重复运行时需要手动确认
expect -c 'spawn termux-setup-storage; expect "*continue? (y/n)*" { send "n\r" }; interact'

if [ ! -d "$PREFIX/share/nginx/html/kod/data/files/$(basename $HOME)" ]; then
  ln -s $HOME/ $PREFIX/share/nginx/html/kod/data/files
fi

if [ ! -d "$PREFIX/share/nginx/html/kod/data/files/$(basename $HOME/storage)" ]; then
  ln -s $HOME/storage $PREFIX/share/nginx/html/kod/data/files
fi

# 如果忘记管理员密码,删除install.lock后重新访问 ip:8080/kod/ 即可重新设置密码,kod数据不会受影响
read -p "是否重置密码,y:重置,n:不重置,请输入y/n: " delete
if [ "$delete" == "y" ]; then
  rm $PREFIX/share/nginx/html/kod/data/system/install.lock
fi
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计