nginx.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package nginx
  2. import (
  3. "os"
  4. "os/exec"
  5. "strings"
  6. "sync"
  7. "time"
  8. "github.com/0xJacky/Nginx-UI/settings"
  9. )
  10. var (
  11. mutex sync.Mutex
  12. lastOutput string
  13. )
  14. func TestConf() (out string) {
  15. mutex.Lock()
  16. defer mutex.Unlock()
  17. if settings.NginxSettings.TestConfigCmd != "" {
  18. out = execShell(settings.NginxSettings.TestConfigCmd)
  19. return
  20. }
  21. out = execCommand("nginx", "-t")
  22. return
  23. }
  24. func Reload() (out string) {
  25. mutex.Lock()
  26. defer mutex.Unlock()
  27. if settings.NginxSettings.ReloadCmd != "" {
  28. out = execShell(settings.NginxSettings.ReloadCmd)
  29. return
  30. }
  31. out = execCommand("nginx", "-s", "reload")
  32. return
  33. }
  34. func Restart() {
  35. mutex.Lock()
  36. defer mutex.Unlock()
  37. // fix(docker): nginx restart always output network error
  38. time.Sleep(500 * time.Millisecond)
  39. if settings.NginxSettings.RestartCmd != "" {
  40. lastOutput = execShell(settings.NginxSettings.RestartCmd)
  41. return
  42. }
  43. pidPath := GetPIDPath()
  44. daemon := GetSbinPath()
  45. lastOutput = execCommand("start-stop-daemon", "--stop", "--quiet", "--oknodo", "--retry=TERM/30/KILL/5", "--pidfile", pidPath)
  46. if daemon == "" {
  47. lastOutput += execCommand("nginx")
  48. return
  49. }
  50. lastOutput += execCommand("start-stop-daemon", "--start", "--quiet", "--pidfile", pidPath, "--exec", daemon)
  51. return
  52. }
  53. func GetLastOutput() string {
  54. mutex.Lock()
  55. defer mutex.Unlock()
  56. return lastOutput
  57. }
  58. // GetModulesPath returns the nginx modules path
  59. func GetModulesPath() string {
  60. // First try to get from nginx -V output
  61. output := execCommand("nginx", "-V")
  62. if output != "" {
  63. // Look for --modules-path in the output
  64. if strings.Contains(output, "--modules-path=") {
  65. parts := strings.Split(output, "--modules-path=")
  66. if len(parts) > 1 {
  67. // Extract the path
  68. path := strings.Split(parts[1], " ")[0]
  69. // Remove quotes if present
  70. path = strings.Trim(path, "\"")
  71. return path
  72. }
  73. }
  74. }
  75. // Default path if not found
  76. return "/usr/lib/nginx/modules"
  77. }
  78. func execShell(cmd string) (out string) {
  79. bytes, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
  80. out = string(bytes)
  81. if err != nil {
  82. out += " " + err.Error()
  83. }
  84. return
  85. }
  86. func execCommand(name string, cmd ...string) (out string) {
  87. bytes, err := exec.Command(name, cmd...).CombinedOutput()
  88. out = string(bytes)
  89. if err != nil {
  90. out += " " + err.Error()
  91. }
  92. return
  93. }
  94. func IsNginxRunning() bool {
  95. pidPath := GetPIDPath()
  96. if fileInfo, err := os.Stat(pidPath); err != nil || fileInfo.Size() == 0 {
  97. return false
  98. }
  99. return true
  100. }