INSTALL.sh 8.1 KB

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