INSTALL.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 JIMVC_REPOSITORY_URL='https://raw.githubusercontent.com/jamesiter/JimV-C'
  13. export EDITION='master'
  14. export NGINX_JIMV_URL=${JIMVC_REPOSITORY_URL}'/'${EDITION}'/misc/nginx_jimv.conf'
  15. export GENERATE_PASSWORD_SCRIPT_TMP_PATH='/tmp/gen_pswd.sh'
  16. export SMTP_HOST=''
  17. export SMTP_USER=''
  18. export SMTP_PASSWORD=''
  19. ARGS=`getopt -o h --long rdb_root_password:,rdb_jimv_password:,redis_password:,jwt_secret:,secret_key:,help -n 'INSTALL.sh' -- "$@"`
  20. eval set -- "${ARGS}"
  21. while true
  22. do
  23. case "$1" in
  24. --rdb_root_password)
  25. export RDB_ROOT_PSWD=$2
  26. shift 2
  27. ;;
  28. --rdb_jimv_password)
  29. export RDB_JIMV_PSWD=$2
  30. shift 2
  31. ;;
  32. --redis_password)
  33. export REDIS_PSWD=$2
  34. shift 2
  35. ;;
  36. --jwt_secret)
  37. export JWT_SECRET=$2
  38. shift 2
  39. ;;
  40. --secret_key)
  41. export SECRET_KEY=$2
  42. shift 2
  43. ;;
  44. -h|--help)
  45. echo 'INSTALL.sh [-h|--help|--rdb_root_password|--rdb_jimv_password|--redis_password|--jwt_secret|--secret_key]'
  46. exit 0
  47. ;;
  48. --)
  49. shift
  50. break
  51. ;;
  52. *)
  53. echo "Internal error!"
  54. exit 1
  55. ;;
  56. esac
  57. done
  58. function check_precondition() {
  59. source /etc/os-release
  60. case ${ID} in
  61. centos|fedora|rhel)
  62. if [ ${VERSION_ID} -lt 7 ]; then
  63. echo "System version must greater than or equal to 7, We found ${VERSION_ID}."
  64. exit 1
  65. fi
  66. ;;
  67. *)
  68. echo "${ID} is unknown, Please to be installed manually."
  69. exit 1
  70. ;;
  71. esac
  72. }
  73. function prepare() {
  74. if [ ! -e ${GENERATE_PASSWORD_SCRIPT_TMP_PATH} ]; then
  75. curl ${JIMVC_REPOSITORY_URL}'/'${EDITION}'/misc/gen_pswd.sh' -o ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
  76. chmod +x ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
  77. fi
  78. if [ ! ${RDB_ROOT_PSWD} ] || [ ${#RDB_ROOT_PSWD} -eq 0 ]; then
  79. export RDB_ROOT_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH}`
  80. fi
  81. if [ ! ${RDB_JIMV_PSWD} ] || [ ${#RDB_JIMV_PSWD} -eq 0 ]; then
  82. export RDB_JIMV_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH}`
  83. fi
  84. if [ ! ${REDIS_PSWD} ] || [ ${#REDIS_PSWD} -eq 0 ]; then
  85. export REDIS_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
  86. fi
  87. if [ ! ${JWT_SECRET} ] || [ ${#JWT_SECRET} -eq 0 ]; then
  88. export JWT_SECRET=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
  89. fi
  90. if [ ! ${SECRET_KEY} ] || [ ${#SECRET_KEY} -eq 0 ]; then
  91. export SECRET_KEY=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
  92. fi
  93. rm -f ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
  94. yum install epel-release python2-pip git -y
  95. pip install --upgrade pip -i ${PYPI}
  96. pip install virtualenv -i ${PYPI}
  97. }
  98. function install_MariaDB() {
  99. # 安装 MariaDB
  100. yum install mariadb mariadb-server -y
  101. # 配置 MariaDB
  102. mkdir -p /etc/systemd/system/mariadb.service.d
  103. echo '[Service]' > /etc/systemd/system/mariadb.service.d/limits.conf
  104. echo 'LimitNOFILE=65535' >> /etc/systemd/system/mariadb.service.d/limits.conf
  105. systemctl --system daemon-reload
  106. sed -i '/\[mysqld\]/a\bind-address = 127.0.0.1' /etc/my.cnf
  107. sed -i '/\[mysqld\]/a\log-bin' /etc/my.cnf
  108. sed -i '/\[mysqld\]/a\expire_logs_days = 1' /etc/my.cnf
  109. sed -i '/\[mysqld\]/a\innodb_file_per_table' /etc/my.cnf
  110. sed -i '/\[mysqld\]/a\max_connections = 1000' /etc/my.cnf
  111. # 启动并使其随机启动
  112. systemctl enable mariadb.service
  113. systemctl start mariadb.service
  114. # 初始化 MariaDB
  115. mysql_secure_installation << EOF
  116. Y
  117. ${RDB_ROOT_PSWD}
  118. ${RDB_ROOT_PSWD}
  119. Y
  120. Y
  121. Y
  122. Y
  123. EOF
  124. # 测试是否部署成功
  125. mysql -u root -p${RDB_ROOT_PSWD} -e 'show databases'
  126. }
  127. function install_Redis() {
  128. # 安装 Redis
  129. yum install redis -y
  130. # 配置 Redis
  131. echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
  132. sysctl -p
  133. sed -i '@^daemonize no@daemonize yes@g' /etc/redis.conf
  134. sed -i 's@^bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf
  135. sed -i 's@^appendonly no@appendonly yes@g' /etc/redis.conf
  136. echo "requirepass ${REDIS_PSWD}" >> /etc/redis.conf
  137. # 启动并使其随机启动
  138. systemctl enable redis.service
  139. systemctl start redis.service
  140. }
  141. function install_Nginx() {
  142. # 安装 Nginx
  143. yum install nginx -y
  144. # 配置 Nginx
  145. mkdir -p /etc/jimv/keys
  146. chown -R www.www /var/lib/nginx
  147. sed -i 's@user nginx.*$@user www;@' /etc/nginx/nginx.conf
  148. sed -i '/^.*server {/,$d' /etc/nginx/nginx.conf
  149. curl ${NGINX_JIMV_URL} >> /etc/nginx/nginx.conf
  150. # 启动并使其随机启动
  151. systemctl enable nginx.service
  152. systemctl start nginx.service
  153. }
  154. function create_web_user() {
  155. useradd -m www
  156. }
  157. function create_web_sites_directory() {
  158. su - www -c "mkdir ~/sites"
  159. }
  160. function clone_and_checkout_JimV-C() {
  161. su - www -c "git clone https://github.com/jamesiter/JimV-C.git ~/sites/JimV-C"
  162. }
  163. function install_dependencies_library() {
  164. # 创建 python 虚拟环境
  165. su - www -c "virtualenv --system-site-packages ~/venv"
  166. # 导入 python 虚拟环境
  167. su - www -c "source ~/venv/bin/activate"
  168. # 使切入 www 用户时自动导入 python 虚拟环境
  169. su - www -c "echo '. ~/venv/bin/activate' >> .bashrc"
  170. # 安装 JimV-C 所需扩展库
  171. su - www -c "pip install -r ~/sites/JimV-C/requirements.txt -i ${PYPI}"
  172. }
  173. function fit_www_user_permission() {
  174. mkdir -p /var/log/jimv
  175. chown www.www /var/log/jimv
  176. mkdir -p /run/jimv
  177. chown www.www /run/jimv
  178. }
  179. function initialization_db() {
  180. # 建立 JimV 数据库专属用户
  181. mysql -u root -p${RDB_ROOT_PSWD} -e "grant all on jimv.* to jimv@localhost identified by \"${RDB_JIMV_PSWD}\"; flush privileges"
  182. # 初始化数据库
  183. su - www -c "mysql -u jimv -p${RDB_JIMV_PSWD} < ~/sites/JimV-C/misc/init.sql"
  184. # 确认是否初始化成功
  185. mysql -u jimv -p${RDB_JIMV_PSWD} -e 'show databases'
  186. }
  187. function generate_config_file() {
  188. cp -v /home/www/sites/JimV-C/jimvc.conf /etc/jimvc.conf
  189. sed -i "s/\"db_password\".*$/\"db_password\": \"${RDB_JIMV_PSWD}\",/" /etc/jimvc.conf
  190. sed -i "s/\"redis_password\".*$/\"redis_password\": \"${REDIS_PSWD}\",/" /etc/jimvc.conf
  191. sed -i "s/\"jwt_secret\".*$/\"jwt_secret\": \"${JWT_SECRET}\",/" /etc/jimvc.conf
  192. sed -i "s/\"SECRET_KEY\".*$/\"SECRET_KEY\": \"${SECRET_KEY}\",/" /etc/jimvc.conf
  193. sed -i "s/\"smtp_host\".*$/\"smtp_host\": \"${SMTP_HOST}\",/" /etc/jimvc.conf
  194. sed -i "s/\"smtp_user\".*$/\"smtp_user\": \"${SMTP_USER}\",/" /etc/jimvc.conf
  195. sed -i "s/\"smtp_password\".*$/\"smtp_password\": \"${SMTP_PASSWORD}\",/" /etc/jimvc.conf
  196. }
  197. function display_summary_information() {
  198. echo
  199. echo "=== Summary information"
  200. echo "RDB root password: [${RDB_ROOT_PSWD}]"
  201. echo "RDB jimv password: [${RDB_JIMV_PSWD}]"
  202. echo "Redis password: [${REDIS_PSWD}]"
  203. echo "======================="
  204. echo
  205. echo "Now, you can run JimV-C use command '/home/www/sites/JimV-C/startup.sh'."
  206. }
  207. function deploy() {
  208. check_precondition
  209. prepare
  210. create_web_user
  211. create_web_sites_directory
  212. clone_and_checkout_JimV-C
  213. fit_www_user_permission
  214. install_MariaDB
  215. install_Redis
  216. install_Nginx
  217. install_dependencies_library
  218. initialization_db
  219. generate_config_file
  220. display_summary_information
  221. }
  222. deploy