lib.sh 4.2 KB

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