lib.sh 3.7 KB

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