INSTALL.sh 8.6 KB

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