1
0

lib.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/bash
  2. # 安装依赖
  3. install_dependencies() {
  4. # 判断系统
  5. if [[ -f /etc/debian_version ]]; then
  6. PM=apt-get
  7. elif [[ -f /etc/redhat-release ]]; then
  8. PM=yum
  9. elif [[ -f /etc/SuSE-release ]]; then
  10. PM=zypper
  11. elif [[ -f /etc/arch-release ]]; then
  12. PM=pacman
  13. elif [[ -f /etc/alpine-release ]]; then
  14. PM=apk
  15. else
  16. echo -e "\e[31m不支持的Linux发行版。\e[0m"
  17. exit 1
  18. fi
  19. if command -v supervisorctl >/dev/null; then
  20. echo -e "\e[32mSupervisor installed! | Supervisor 已完成!\e[0m"
  21. else
  22. echo -e "\e[31mSupervisor did not installed! | Supervisor 未安装!\e[0m"
  23. # 安装 Supervisor
  24. case $PM in
  25. apt-get)
  26. sudo apt-get update
  27. sudo apt-get install -y supervisor
  28. ;;
  29. yum)
  30. sudo yum install -y epel-release
  31. sudo yum install -y supervisor
  32. ;;
  33. zypper)
  34. sudo zypper install -y supervisor
  35. ;;
  36. apk)
  37. sudo apk add supervisor
  38. ;;
  39. pacman)
  40. sudo pacman -S supervisor
  41. ;;
  42. esac
  43. # 激活
  44. case $PM in
  45. yum)
  46. sudo service supervisord start
  47. sudo chkconfig supervisord on
  48. ;;
  49. *)
  50. sudo systemctl start supervisor.service
  51. sudo systemctl enable supervisor.service
  52. ;;
  53. esac
  54. echo -e "\e[32mSupervisor installation completed! | Supervisor 安装完成!\e[0m"
  55. fi
  56. }
  57. # 清理不需要的文件
  58. clean_files() {
  59. rm -rf .htaccess 404.html index.html
  60. if [ -f .user.ini ]; then
  61. chattr -i .user.ini
  62. rm -f .user.ini
  63. fi
  64. }
  65. # 检查软件是否安装
  66. check_available() {
  67. tools=$1
  68. if command -v "$tools" >/dev/null 2>&1; then
  69. echo -e "\e[32m$tools Installed! | $tools 已安装!\e[0m"
  70. else
  71. echo -e "\e[31m$tools did not installed! | $tools 未安装!\e[0m"
  72. fi
  73. }
  74. # 检查环境
  75. check_env() {
  76. check_available php
  77. check_available php-fpm
  78. check_available nginx
  79. check_available mysql
  80. check_available redis-cli
  81. }
  82. # 检查composer是否安装
  83. check_composer() {
  84. if [ ! -f "/usr/bin/composer" ]; then
  85. curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
  86. else
  87. if [[ $(composer -n --version --no-ansi 2>/dev/null | cut -d" " -f3) < 2.2.0 ]]; then
  88. composer self-update
  89. fi
  90. fi
  91. }
  92. # 设置权限
  93. set_permissions() {
  94. if [ ! -d "/home/www" ]; then
  95. mkdir -p /home/www
  96. chown www:www /home/www
  97. fi
  98. chown -R www:www ./
  99. chmod -R 755 ./
  100. chmod -R 777 storage/
  101. }
  102. # 设置定时任务
  103. set_schedule() {
  104. cmd="php $PWD/artisan schedule:run >> /dev/null 2>&1"
  105. cronjob="* * * * * $cmd"
  106. if (crontab -u www -l | grep -q -F "$cmd"); then
  107. echo -e "\e[36m定时任务已存在,无需重复设置。\e[0m"
  108. else
  109. (
  110. crontab -u www -l
  111. echo "$cronjob"
  112. ) | crontab -u www -
  113. echo -e "\e[32m定时任务设置完成!\e[0m"
  114. fi
  115. }
  116. # 设置Horizon
  117. set_horizon() {
  118. if [ ! -f /etc/supervisor/conf.d/horizon.conf ]; then
  119. cat <<EOF | sudo tee -a /etc/supervisor/conf.d/horizon.conf >/dev/null
  120. [program:horizon]
  121. process_name=%(program_name)s
  122. command=php $PWD/artisan horizon
  123. autostart=true
  124. autorestart=true
  125. user=www
  126. redirect_stderr=true
  127. stdout_logfile=$PWD/storage/logs/horizon.log
  128. stopwaitsecs=3600
  129. EOF
  130. sudo supervisorctl restart horizon
  131. echo -e "\e[32mHorizon configuration completed! | Horizon 配置完成!\e[0m"
  132. else
  133. echo -e "\e[36mHorizon already configured! | Horizon 已配置!\e[0m"
  134. fi
  135. }
  136. # 更新旧的队列设置
  137. update_old_queue() {
  138. if crontab -l | grep -q "queue.sh"; then
  139. crontab_content=$(crontab -l | grep -v "queue.sh")
  140. echo "$crontab_content" | crontab -
  141. echo -e "\e[32mOld queue.sh cron job removed! | 旧的 queue.sh 定时任务已移除!\e[0m"
  142. fi
  143. set_horizon
  144. }