INSTALL.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 -y
  95. yum install python2-pip git -y
  96. pip install --upgrade pip -i ${PYPI}
  97. pip install virtualenv -i ${PYPI}
  98. }
  99. function install_MariaDB() {
  100. # 安装 MariaDB
  101. yum install mariadb mariadb-server -y
  102. # 配置 MariaDB
  103. mkdir -p /etc/systemd/system/mariadb.service.d
  104. echo '[Service]' > /etc/systemd/system/mariadb.service.d/limits.conf
  105. echo 'LimitNOFILE=65535' >> /etc/systemd/system/mariadb.service.d/limits.conf
  106. systemctl --system daemon-reload
  107. sed -i '/\[mysqld\]/a\bind-address = 127.0.0.1' /etc/my.cnf
  108. sed -i '/\[mysqld\]/a\log-bin' /etc/my.cnf
  109. sed -i '/\[mysqld\]/a\expire_logs_days = 1' /etc/my.cnf
  110. sed -i '/\[mysqld\]/a\innodb_file_per_table' /etc/my.cnf
  111. sed -i '/\[mysqld\]/a\max_connections = 1000' /etc/my.cnf
  112. # 启动并使其随机启动
  113. systemctl enable mariadb.service
  114. systemctl start mariadb.service
  115. # 初始化 MariaDB
  116. mysql_secure_installation << EOF
  117. Y
  118. ${RDB_ROOT_PSWD}
  119. ${RDB_ROOT_PSWD}
  120. Y
  121. Y
  122. Y
  123. Y
  124. EOF
  125. # 测试是否部署成功
  126. mysql -u root -p${RDB_ROOT_PSWD} -e 'show databases'
  127. }
  128. function install_Redis() {
  129. # 安装 Redis
  130. yum install redis -y
  131. # 配置 Redis
  132. echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
  133. sysctl -p
  134. sed -i 's@^daemonize no@daemonize yes@g' /etc/redis.conf
  135. sed -i 's@^bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf
  136. sed -i 's@^appendonly no@appendonly yes@g' /etc/redis.conf
  137. echo "requirepass ${REDIS_PSWD}" >> /etc/redis.conf
  138. # 启动并使其随机启动
  139. systemctl enable redis.service
  140. systemctl start redis.service
  141. }
  142. function install_Nginx() {
  143. # 安装 Nginx
  144. yum install nginx -y
  145. # 配置 Nginx
  146. mkdir -p /etc/jimv/keys
  147. chown -R www.www /var/lib/nginx
  148. sed -i 's@user nginx.*$@user www;@' /etc/nginx/nginx.conf
  149. sed -i '/^.*server {/,$d' /etc/nginx/nginx.conf
  150. curl ${NGINX_JIMV_URL} >> /etc/nginx/nginx.conf
  151. # 启动并使其随机启动
  152. systemctl enable nginx.service
  153. systemctl start nginx.service
  154. }
  155. function create_web_user() {
  156. useradd -m www
  157. }
  158. function create_web_sites_directory() {
  159. su - www -c "mkdir ~/sites"
  160. }
  161. function clone_and_checkout_JimV-C() {
  162. su - www -c "git clone https://github.com/jamesiter/JimV-C.git ~/sites/JimV-C"
  163. }
  164. function install_dependencies_library() {
  165. # 创建 python 虚拟环境
  166. su - www -c "virtualenv --system-site-packages ~/venv"
  167. # 导入 python 虚拟环境
  168. su - www -c "source ~/venv/bin/activate"
  169. # 使切入 www 用户时自动导入 python 虚拟环境
  170. su - www -c "echo '. ~/venv/bin/activate' >> .bashrc"
  171. # 安装 JimV-C 所需扩展库
  172. su - www -c "pip install -r ~/sites/JimV-C/requirements.txt -i ${PYPI}"
  173. }
  174. function fit_www_user_permission() {
  175. mkdir -p /var/log/jimv
  176. chown www.www /var/log/jimv
  177. mkdir -p /run/jimv
  178. chown www.www /run/jimv
  179. }
  180. function initialization_db() {
  181. # 建立 JimV 数据库专属用户
  182. mysql -u root -p${RDB_ROOT_PSWD} -e "grant all on jimv.* to jimv@localhost identified by \"${RDB_JIMV_PSWD}\"; flush privileges"
  183. # 初始化数据库
  184. su - www -c "mysql -u jimv -p${RDB_JIMV_PSWD} < ~/sites/JimV-C/misc/init.sql"
  185. # 确认是否初始化成功
  186. mysql -u jimv -p${RDB_JIMV_PSWD} -e 'show databases'
  187. }
  188. function generate_config_file() {
  189. cp -v /home/www/sites/JimV-C/jimvc.conf /etc/jimvc.conf
  190. sed -i "s/\"db_password\".*$/\"db_password\": \"${RDB_JIMV_PSWD}\",/" /etc/jimvc.conf
  191. sed -i "s/\"redis_password\".*$/\"redis_password\": \"${REDIS_PSWD}\",/" /etc/jimvc.conf
  192. sed -i "s/\"jwt_secret\".*$/\"jwt_secret\": \"${JWT_SECRET}\",/" /etc/jimvc.conf
  193. sed -i "s/\"SECRET_KEY\".*$/\"SECRET_KEY\": \"${SECRET_KEY}\",/" /etc/jimvc.conf
  194. sed -i "s/\"smtp_host\".*$/\"smtp_host\": \"${SMTP_HOST}\",/" /etc/jimvc.conf
  195. sed -i "s/\"smtp_user\".*$/\"smtp_user\": \"${SMTP_USER}\",/" /etc/jimvc.conf
  196. sed -i "s/\"smtp_password\".*$/\"smtp_password\": \"${SMTP_PASSWORD}\",/" /etc/jimvc.conf
  197. }
  198. function display_summary_information() {
  199. echo
  200. echo "=== Summary information"
  201. echo "RDB root password: [${RDB_ROOT_PSWD}]"
  202. echo "RDB jimv password: [${RDB_JIMV_PSWD}]"
  203. echo "Redis password: [${REDIS_PSWD}]"
  204. echo "======================="
  205. echo
  206. echo "Now, you can run JimV-C use command '/home/www/sites/JimV-C/startup.sh'."
  207. }
  208. function deploy() {
  209. check_precondition
  210. prepare
  211. create_web_user
  212. create_web_sites_directory
  213. clone_and_checkout_JimV-C
  214. fit_www_user_permission
  215. install_MariaDB
  216. install_Redis
  217. install_Nginx
  218. install_dependencies_library
  219. initialization_db
  220. generate_config_file
  221. display_summary_information
  222. }
  223. deploy