INSTALL.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/env bash
  2. #
  3. # JimV-C
  4. #
  5. # Copyright (C) 2017 JimV <james.iter.cn@gmail.com>
  6. #
  7. # Author: James Iter <james.iter.cn@gmail.com>
  8. #
  9. # This script will help you to automation install JimV-C.
  10. #
  11. export PYPI='https://mirrors.aliyun.com/pypi/simple/'
  12. export NGINX_JIMV='curl https://raw.githubusercontent.com/jamesiter/JimV-C/master/misc/nginx_jimv.conf >> /etc/nginx/nginx.conf'
  13. export RDB_PSWD='your_rdb_root_password'
  14. export RDB_JIMV_PSWD='your_rdb_jimv_password'
  15. export REDIS_PSWD='your_jimv_redis_passwordddddddddddddddddddddddddddddddddddddddddddddddddddddddd'
  16. export JWT_SECRET='kcMsj4qj0EAvAvj3nlFlPZd2x7P9kBBQ$deae64883a03982913e6c5f0ba265c2c5dfd0cfx'
  17. export SECRET_KEY='QSYI73re6x553wmcNwT9tk4OCNK9OUS9xNUulDShEcvRw00YhCKaqHhEGYOGKOSB'
  18. export SMTP_HOST=''
  19. export SMTP_USER=''
  20. export SMTP_PASSWORD=''
  21. function check_precondition() {
  22. source /etc/os-release
  23. case ${ID} in
  24. centos|fedora|rhel)
  25. if [ ${VERSION_ID} -lt 7 ]; then
  26. echo "System version must greater than or equal to 7, We found ${VERSION_ID}."
  27. exit 1
  28. fi
  29. ;;
  30. *)
  31. echo "${ID} is unknown, Please to be installed manually."
  32. exit 1
  33. ;;
  34. esac
  35. }
  36. function prepare() {
  37. yum install epel-release python2-pip git -y
  38. pip install --upgrade pip -i ${PYPI}
  39. pip install virtualenv -i ${PYPI}
  40. }
  41. function install_MariaDB() {
  42. # 安装 MariaDB
  43. yum install mariadb mariadb-server -y
  44. # 配置 MariaDB
  45. mkdir -p /etc/systemd/system/mariadb.service.d
  46. echo '[Service]' > /etc/systemd/system/mariadb.service.d/limits.conf
  47. echo 'LimitNOFILE=65535' >> /etc/systemd/system/mariadb.service.d/limits.conf
  48. systemctl --system daemon-reload
  49. sed -i '/\[mysqld\]/a\bind-address = 127.0.0.1' /etc/my.cnf
  50. sed -i '/\[mysqld\]/a\log-bin' /etc/my.cnf
  51. sed -i '/\[mysqld\]/a\expire_logs_days = 1' /etc/my.cnf
  52. sed -i '/\[mysqld\]/a\innodb_file_per_table' /etc/my.cnf
  53. sed -i '/\[mysqld\]/a\max_connections = 1000' /etc/my.cnf
  54. # 启动并使其随机启动
  55. systemctl enable mariadb.service
  56. systemctl start mariadb.service
  57. # 初始化 MariaDB
  58. mysql_secure_installation << EOF
  59. Y
  60. ${RDB_PSWD}
  61. ${RDB_PSWD}
  62. Y
  63. Y
  64. Y
  65. Y
  66. EOF
  67. # 测试是否部署成功
  68. mysql -u root -p${RDB_PSWD} -e 'show databases'
  69. }
  70. function install_Redis() {
  71. # 安装 Redis
  72. yum install redis -y
  73. # 配置 Redis
  74. echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
  75. sysctl -p
  76. sed -i '@^daemonize no@daemonize yes@g' /etc/redis.conf
  77. sed -i 's@^bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf
  78. sed -i 's@^appendonly no@appendonly yes@g' /etc/redis.conf
  79. echo 'requirepass ${REDIS_PSWD}' >> /etc/redis.conf
  80. # 启动并使其随机启动
  81. systemctl enable redis.service
  82. systemctl start redis.service
  83. }
  84. function install_Nginx() {
  85. # 安装 Nginx
  86. yum install nginx -y
  87. # 配置 Nginx
  88. mkdir -p /etc/jimv/keys
  89. chown -R www.www /var/lib/nginx
  90. sed -i 's@user nginx.*$@user www;@' /etc/nginx/nginx.conf
  91. sed -i '/^.*server {/,$d' /etc/nginx/nginx.conf
  92. curl ${NGINX_JIMV} >> /etc/nginx/nginx.conf
  93. # 启动并使其随机启动
  94. systemctl enable nginx.service
  95. systemctl start nginx.service
  96. }
  97. function create_web_user() {
  98. useradd -m www
  99. }
  100. function create_web_sites_directory() {
  101. su - www -c "mkdir ~/sites"
  102. }
  103. function clone_and_checkout_JimV-C() {
  104. su - www -c "git clone https://github.com/jamesiter/JimV-C.git ~/sites/JimV-C"
  105. }
  106. function install_dependencies_library() {
  107. # 创建 python 虚拟环境
  108. su - www -c "virtualenv --system-site-packages ~/venv"
  109. # 导入 python 虚拟环境
  110. su - www -c "source ~/venv/bin/activate"
  111. # 使切入 www 用户时自动导入 python 虚拟环境
  112. su - www -c "echo '. ~/venv/bin/activate' >> .bashrc"
  113. # 安装JimV-C所需扩展库
  114. su - www -c "pip install -r ~/sites/JimV-C/requirements.txt -i ${PYPI}"
  115. }
  116. function fit_www_user_permission() {
  117. mkdir -p /var/log/jimv
  118. chown www.www /var/log/jimv
  119. mkdir -p /run/jimv
  120. chown www.www /run/jimv
  121. }
  122. function initialization_db() {
  123. # 建立 JimV 数据库专属用户
  124. mysql -u root -p${RDB_PSWD} -e "grant all on jimv.* to jimv@localhost identified by \"${RDB_JIMV_PSWD}\"; flush privileges"
  125. # 初始化数据库
  126. su - www -c "mysql -u jimv -p${RDB_JIMV_PSWD} < ~/sites/JimV-C/misc/init.sql"
  127. # 确认是否初始化成功
  128. mysql -u jimv -p${RDB_JIMV_PSWD} -e 'show databases'
  129. }
  130. function generate_config_file() {
  131. cp -v /home/www/sites/JimV-C/jimvc.conf /etc/jimvc.conf
  132. sed -i "s/\"db_password\".*$/\"db_password\": \"${RDB_JIMV_PSWD}\",/" /etc/jimvc.conf
  133. sed -i "s/\"redis_password\".*$/\"redis_password\": \"${REDIS_PSWD}\",/" /etc/jimvc.conf
  134. sed -i "s/\"jwt_secret\".*$/\"jwt_secret\": \"${JWT_SECRET}\",/" /etc/jimvc.conf
  135. sed -i "s/\"SECRET_KEY\".*$/\"SECRET_KEY\": \"${SECRET_KEY}\",/" /etc/jimvc.conf
  136. sed -i "s/\"smtp_host\".*$/\"smtp_host\": \"${SMTP_HOST}\",/" /etc/jimvc.conf
  137. sed -i "s/\"smtp_user\".*$/\"smtp_user\": \"${SMTP_USER}\",/" /etc/jimvc.conf
  138. sed -i "s/\"smtp_password\".*$/\"smtp_password\": \"${SMTP_PASSWORD}\",/" /etc/jimvc.conf
  139. }
  140. function start() {
  141. check_precondition
  142. prepare
  143. create_web_user
  144. create_web_sites_directory
  145. clone_and_checkout_JimV-C
  146. fit_www_user_permission
  147. install_MariaDB
  148. install_Redis
  149. install_Nginx
  150. install_dependencies_library
  151. initialization_db
  152. generate_config_file
  153. }
  154. start