package appstore import ( "context" "errors" "fmt" "os" "os/user" "path/filepath" "strings" "github.com/miaopanel/miaopanel/internal/site" "github.com/miaopanel/miaopanel/internal/store" "github.com/miaopanel/miaopanel/internal/system" ) // phpMyAdminPort is the dedicated public port of the companion site. const phpMyAdminPort = 16520 // removePhpMyAdminSite drops the companion site created by the installer so an // uninstall does not leave a vhost pointing at a deleted directory. func (s *Service) removePhpMyAdminSite(ctx context.Context, t *task) error { if s.sites == nil { return nil } st, err := s.sites.BySlug(ctx, "phpmyadmin") if err != nil || st == nil { return nil // nothing to clean up } if err := s.sites.Delete(ctx, st.ID, true); err != nil { return fmt.Errorf("删除 phpMyAdmin 站点失败: %w", err) } t.append("==> 已删除 phpMyAdmin 自动站点(端口 16520)") return nil } // ensurePhpMyAdminSite creates a PHP site that exposes phpMyAdmin on // http://:16520/ right after the archive has been unpacked. The // site is only created once; a reinstall keeps the existing site. func (s *Service) ensurePhpMyAdminSite(ctx context.Context, t *task) error { if s.sites == nil { t.append("站点服务未初始化,跳过自动建站") return nil } root := filepath.Join(s.cfg.WebRoot, "phpmyadmin", "current") if _, err := os.Stat(filepath.Join(root, "index.php")); err != nil { return fmt.Errorf("phpMyAdmin 文件不完整: %w", err) } if existing, err := s.sites.BySlug(ctx, "phpmyadmin"); err == nil && existing != nil { t.append(fmt.Sprintf("phpMyAdmin 站点已存在(端口 %d),未做修改", existing.Options.ListenPort)) return nil } else if err != nil && !errors.Is(err, store.ErrNotFound) { return fmt.Errorf("检查 phpMyAdmin 站点失败: %w", err) } php := system.DetectRuntimes().PHP if len(php) == 0 { return errors.New("未检测到 PHP-FPM,请先在应用商店安装 PHP 后再安装 phpMyAdmin") } version := php[0] options := store.SiteOptions{ SecurityHead: true, WAF: true, WAFRate: 20, WAFBurst: 40, ListenPort: phpMyAdminPort, } var st *store.Site var err error // The domain is a placeholder: nginx uses this block as the default server // on port 16520, so http://:16520/ reaches it without DNS. for _, domain := range []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"} { st, err = s.sites.Create(ctx, site.Input{ Name: "phpmyadmin", Kind: "php", Domains: []string{domain}, Root: root, PHPVersion: version, RunUser: phpFPMUser(), Remark: "phpMyAdmin 自动站点(端口 16520)", Options: options, }) if err == nil { break } if !strings.Contains(err.Error(), "已被其他站点使用") { break } } if err != nil { return fmt.Errorf("自动创建 phpMyAdmin 站点失败: %w", err) } t.append(fmt.Sprintf("已创建 phpMyAdmin 站点:http://<服务器IP>:%d/(PHP %s)", phpMyAdminPort, version)) if fw := system.OpenTCPPort(ctx, s.runner, phpMyAdminPort); fw != "" { t.append(fmt.Sprintf("已通过 %s 放行 TCP %d", fw, phpMyAdminPort)) } else { t.append(fmt.Sprintf("提示:如云安全组未放行,请手动放行 TCP %d", phpMyAdminPort)) } _ = st return nil } // phpFPMUser mirrors the runtime user of the packaged FPM pool. func phpFPMUser() string { for _, name := range []string{"www-data", "apache", "nginx"} { if _, err := user.Lookup(name); err == nil { return name } } return "www" }