install.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/bin/bash
  2. # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
  3. # Copyright: (c) <spug.dev@gmail.com>
  4. # Released under the AGPL-3.0 License.
  5. set -e
  6. function spuy_banner() {
  7. echo " ";
  8. echo " #### ##### # # #### ";
  9. echo "# # # # # # #";
  10. echo " #### # # # # # ";
  11. echo " # ##### # # # ###";
  12. echo "# # # # # # #";
  13. echo " #### # #### #### ";
  14. echo " ";
  15. }
  16. function init_system_lib() {
  17. source /etc/os-release
  18. case $ID in
  19. centos|fedora|rhel)
  20. echo "开始安装/更新可能缺少的依赖: git mariadb-server mariadb-devel python3-devel gcc openldap-devel redis nginx supervisor python36"
  21. yum install -y epel-release
  22. yum install -y git mariadb-server mariadb-devel python3-devel gcc openldap-devel redis nginx supervisor python36
  23. sed -i 's/ default_server//g' /etc/nginx/nginx.conf
  24. MYSQL_CONF=/etc/my.cnf.d/spug.cnf
  25. SUPERVISOR_CONF=/etc/supervisord.d/spug.ini
  26. REDIS_SRV=redis
  27. SUPERVISOR_SRV=supervisord
  28. ;;
  29. debian|ubuntu|devuan)
  30. echo "开始安装/更新可能缺少的依赖: git mariadb-server libmariadbd-dev python3-venv libsasl2-dev libldap2-dev redis-server nginx supervisor"
  31. apt update
  32. apt install -y git mariadb-server libmariadbd-dev python3-dev python3-venv libsasl2-dev libldap2-dev redis-server nginx supervisor
  33. rm -f /etc/nginx/sites-enabled/default
  34. MYSQL_CONF=/etc/mysql/conf.d/spug.cnf
  35. SUPERVISOR_CONF=/etc/supervisor/conf.d/spug.conf
  36. REDIS_SRV=redis-server
  37. SUPERVISOR_SRV=supervisor
  38. ;;
  39. *)
  40. exit 1
  41. ;;
  42. esac
  43. }
  44. function install_spug() {
  45. echo "开始安装Spug..."
  46. mkdir -p /data
  47. cd /data
  48. git clone --depth=1 https://gitee.com/openspug/spug.git
  49. curl -o /tmp/web_latest.tar.gz https://spug.dev/installer/web_latest.tar.gz
  50. tar xf /tmp/web_latest.tar.gz -C spug/spug_web/
  51. cd spug/spug_api
  52. python3 -m venv venv
  53. source venv/bin/activate
  54. pip install wheel -i https://pypi.doubanio.com/simple/
  55. pip install gunicorn mysqlclient -i https://pypi.doubanio.com/simple/
  56. pip install -r requirements.txt -i https://pypi.doubanio.com/simple/
  57. }
  58. function setup_conf() {
  59. echo "开始配置Spug配置..."
  60. # mysql conf
  61. cat << EOF > $MYSQL_CONF
  62. [mysqld]
  63. bind-address=127.0.0.1
  64. EOF
  65. # spug conf
  66. cat << EOF > spug/overrides.py
  67. DEBUG = False
  68. ALLOWED_HOSTS = ['127.0.0.1']
  69. DATABASES = {
  70. 'default': {
  71. 'ATOMIC_REQUESTS': True,
  72. 'ENGINE': 'django.db.backends.mysql',
  73. 'NAME': 'spug',
  74. 'USER': 'spug',
  75. 'PASSWORD': 'spug.dev',
  76. 'HOST': '127.0.0.1',
  77. 'OPTIONS': {
  78. 'charset': 'utf8mb4',
  79. 'sql_mode': 'STRICT_TRANS_TABLES',
  80. }
  81. }
  82. }
  83. EOF
  84. cat << EOF > $SUPERVISOR_CONF
  85. [program:spug-api]
  86. command = bash /data/spug/spug_api/tools/start-api.sh
  87. autostart = true
  88. stdout_logfile = /data/spug/spug_api/logs/api.log
  89. redirect_stderr = true
  90. [program:spug-ws]
  91. command = bash /data/spug/spug_api/tools/start-ws.sh
  92. autostart = true
  93. stdout_logfile = /data/spug/spug_api/logs/ws.log
  94. redirect_stderr = true
  95. [program:spug-worker]
  96. command = bash /data/spug/spug_api/tools/start-worker.sh
  97. autostart = true
  98. stdout_logfile = /data/spug/spug_api/logs/worker.log
  99. redirect_stderr = true
  100. [program:spug-monitor]
  101. command = bash /data/spug/spug_api/tools/start-monitor.sh
  102. autostart = true
  103. stdout_logfile = /data/spug/spug_api/logs/monitor.log
  104. redirect_stderr = true
  105. [program:spug-scheduler]
  106. command = bash /data/spug/spug_api/tools/start-scheduler.sh
  107. autostart = true
  108. stdout_logfile = /data/spug/spug_api/logs/scheduler.log
  109. redirect_stderr = true
  110. EOF
  111. cat << EOF > /etc/nginx/conf.d/spug.conf
  112. server {
  113. listen 80 default_server;
  114. root /data/spug/spug_web/build/;
  115. location ^~ /api/ {
  116. rewrite ^/api(.*) \$1 break;
  117. proxy_pass http://127.0.0.1:9001;
  118. proxy_redirect off;
  119. proxy_set_header X-Real-IP \$remote_addr;
  120. }
  121. location ^~ /api/ws/ {
  122. rewrite ^/api(.*) \$1 break;
  123. proxy_pass http://127.0.0.1:9002;
  124. proxy_http_version 1.1;
  125. proxy_set_header Upgrade \$http_upgrade;
  126. proxy_set_header Connection "Upgrade";
  127. proxy_set_header X-Real-IP \$remote_addr;
  128. }
  129. error_page 404 /index.html;
  130. }
  131. EOF
  132. systemctl start mariadb
  133. systemctl enable mariadb
  134. mysql -e "create database spug default character set utf8mb4 collate utf8mb4_unicode_ci;"
  135. mysql -e "grant all on spug.* to spug@127.0.0.1 identified by 'spug.dev'"
  136. mysql -e "flush privileges"
  137. python manage.py initdb
  138. python manage.py useradd -u admin -p spug.dev -s -n 管理员
  139. systemctl enable nginx
  140. systemctl enable $REDIS_SRV
  141. systemctl enable $SUPERVISOR_SRV
  142. systemctl restart nginx
  143. systemctl start $REDIS_SRV
  144. systemctl restart $SUPERVISOR_SRV
  145. }
  146. spuy_banner
  147. init_system_lib
  148. install_spug
  149. setup_conf
  150. echo -e "\n\n\033[33m安全警告:默认的数据库和Redis服务并不安全,请确保其仅监听在127.0.0.1,推荐参考官网文档自行加固安全配置!\033[0m"
  151. echo -e "\033[32m安装成功!\033[0m"
  152. echo "默认管理员账户:admin 密码:spug.dev"
  153. echo "默认数据库用户:spug 密码:spug.dev"