INSTALL.sh 7.8 KB

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