INSTALL.sh 9.1 KB

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