miaopanel
1package appstore
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "os"
8 "os/user"
9 "path/filepath"
10 "strings"
11
12 "github.com/miaopanel/miaopanel/internal/site"
13 "github.com/miaopanel/miaopanel/internal/store"
14 "github.com/miaopanel/miaopanel/internal/system"
15)
16
17// phpMyAdminPort is the dedicated public port of the companion site.
18const phpMyAdminPort = 16520
19
20// removePhpMyAdminSite drops the companion site created by the installer so an
21// uninstall does not leave a vhost pointing at a deleted directory.
22func (s *Service) removePhpMyAdminSite(ctx context.Context, t *task) error {
23 if s.sites == nil {
24 return nil
25 }
26 st, err := s.sites.BySlug(ctx, "phpmyadmin")
27 if err != nil || st == nil {
28 return nil // nothing to clean up
29 }
30 if err := s.sites.Delete(ctx, st.ID, true); err != nil {
31 return fmt.Errorf("删除 phpMyAdmin 站点失败: %w", err)
32 }
33 t.append("==> 已删除 phpMyAdmin 自动站点(端口 16520)")
34 return nil
35}
36
37// ensurePhpMyAdminSite creates a PHP site that exposes phpMyAdmin on
38// http://<server-ip>:16520/ right after the archive has been unpacked. The
39// site is only created once; a reinstall keeps the existing site.
40func (s *Service) ensurePhpMyAdminSite(ctx context.Context, t *task) error {
41 if s.sites == nil {
42 t.append("站点服务未初始化,跳过自动建站")
43 return nil
44 }
45 root := filepath.Join(s.cfg.WebRoot, "phpmyadmin", "current")
46 if _, err := os.Stat(filepath.Join(root, "index.php")); err != nil {
47 return fmt.Errorf("phpMyAdmin 文件不完整: %w", err)
48 }
49 if existing, err := s.sites.BySlug(ctx, "phpmyadmin"); err == nil && existing != nil {
50 t.append(fmt.Sprintf("phpMyAdmin 站点已存在(端口 %d),未做修改", existing.Options.ListenPort))
51 return nil
52 } else if err != nil && !errors.Is(err, store.ErrNotFound) {
53 return fmt.Errorf("检查 phpMyAdmin 站点失败: %w", err)
54 }
55 php := system.DetectRuntimes().PHP
56 if len(php) == 0 {
57 return errors.New("未检测到 PHP-FPM,请先在应用商店安装 PHP 后再安装 phpMyAdmin")
58 }
59 version := php[0]
60 options := store.SiteOptions{
61 SecurityHead: true,
62 WAF: true,
63 WAFRate: 20,
64 WAFBurst: 40,
65 ListenPort: phpMyAdminPort,
66 }
67 var st *store.Site
68 var err error
69 // The domain is a placeholder: nginx uses this block as the default server
70 // on port 16520, so http://<server-ip>:16520/ reaches it without DNS.
71 for _, domain := range []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"} {
72 st, err = s.sites.Create(ctx, site.Input{
73 Name: "phpmyadmin",
74 Kind: "php",
75 Domains: []string{domain},
76 Root: root,
77 PHPVersion: version,
78 RunUser: phpFPMUser(),
79 Remark: "phpMyAdmin 自动站点(端口 16520)",
80 Options: options,
81 })
82 if err == nil {
83 break
84 }
85 if !strings.Contains(err.Error(), "已被其他站点使用") {
86 break
87 }
88 }
89 if err != nil {
90 return fmt.Errorf("自动创建 phpMyAdmin 站点失败: %w", err)
91 }
92 t.append(fmt.Sprintf("已创建 phpMyAdmin 站点:http://<服务器IP>:%d/(PHP %s)", phpMyAdminPort, version))
93 if fw := system.OpenTCPPort(ctx, s.runner, phpMyAdminPort); fw != "" {
94 t.append(fmt.Sprintf("已通过 %s 放行 TCP %d", fw, phpMyAdminPort))
95 } else {
96 t.append(fmt.Sprintf("提示:如云安全组未放行,请手动放行 TCP %d", phpMyAdminPort))
97 }
98 _ = st
99 return nil
100}
101
102// phpFPMUser mirrors the runtime user of the packaged FPM pool.
103func phpFPMUser() string {
104 for _, name := range []string{"www-data", "apache", "nginx"} {
105 if _, err := user.Lookup(name); err == nil {
106 return name
107 }
108 }
109 return "www"
110}