ubuntu-install.sh 10.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
#/usr/bin/env


###
# @Author: ly525
 # @Date: 2019-11-30 13:24:57
 # @LastEditors: ly525
 # @LastEditTime: 2019-12-04 21:13:20
 # @FilePath: /luban-h5/deploy/ubuntu-install.sh
 # @Github: https://github.com/ly525/luban-h5
 # @Description:
 ## #!en: One-Click Setup Script to Install Luban-H5 on Ubuntu 18.04
 ## #!zh: 鲁班H5 Ubuntu18.04 一键安装脚本
 # @Copyright 2018 - 2019 luban-h5. All Rights Reserved
 ###

## !en:
######################################################
#    One-Click Setup Script to Install Luban-H5 on Ubuntu 18.04
######################################################


######################################################
#         Step1: Download && Run the Script
# docker pull ubuntu
# docker run -it -p 1234:80 -p 1235:1337  -v `pwd`:/app ubuntu
# wget -qO- https://raw.githubusercontent.com/ly525/luban-h5/dev/deploy/ubuntu-install.sh | bash
######################################################

######################################################
#          Step2: After the Script Runs
# 1. Luban H5 Admin
# 1.1 visit on your host machine:http://localhost:1234/admin,   custom the username and password, then login the admin
# 1.2 config the permission,see more here:https://ly525.github.io/luban-h5/zh/getting-started/quick-start.html#%E5%90%8E%E7%AB%AF%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA

# 2. Luban H5 Front-End
# 2.1visit on your host machine :http://localhost:1234, enjoy it!
######################################################

######################################################
#           Feedback

# GitHub:https://github.com/ly525/luban-h5/issues
######################################################


## #!zh
######################################################
#           [鲁班H5] Ubuntu18.04 一键安装脚本
######################################################


######################################################
#           [本地一键安装步骤]
# docker pull ubuntu
# docker run -it -p 1234:80 -p 1235:1337  -v `pwd`:/app ubuntu
# wget -qO- https://raw.githubusercontent.com/ly525/luban-h5/dev/deploy/ubuntu-install.sh | bash
######################################################

######################################################
#           [安装完成之后]
# 1. 鲁班后台
# 1.1 宿主机访问 [鲁班后台]:localhost:1234/admin,自定义配置账号密码,登录即可
# 1.2 配置相关权限,文档参见:https://ly525.github.io/luban-h5/zh/getting-started/quick-start.html#%E5%90%8E%E7%AB%AF%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA

# 2. 鲁班前端
# 2.1 宿主机访问 「鲁班前端」:localhost:1234, 即可看到鲁班的前端了
######################################################

######################################################
#           [问题反馈]

# GitHub:https://github.com/ly525/luban-h5
######################################################


#
#

## Variables
#

GREEN="\033[0;32m"
YELLOW="\033[0;33m"
RED="\033[0;31m"
NEUTRAL="\033[0m"
NGINX_CONFIG_DIR='/etc/nginx/'


#
## Displaying functions
#

function already {
  echo -e "${YELLOW}[-]$1 is already installed${NEUTRAL}"
}

function installing {
  echo -e "${GREEN}[~]Installing $1...${NEUTRAL}"
}

function success {
  echo -e "${GREEN}[+]$1 successfully installed${NEUTRAL}"
}

function exitBanner {
    echo "#"
    echo "#  Node.js, Strapi, PM2, MongoDB and nginx are now installed"
    echo "#"
}

function base_install {
  RUN $1 &>/dev/null
  if [ $? == "1" ]; then
    already wget
  else
    installing $1
    apt-get install -y $1
    success $1
  fi
}

function set_apt_mirror {
  # 准备工作:
  # 1. 更换 ubuntu 镜像
  #
  # 查看 ubuntu 版本: cat /etc/issue
  # 替换为清华大学镜像:https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/
  # 或替换为中科大镜像:https://lug.ustc.edu.cn/wiki/mirrors/help/ubuntu
  # sudo sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
  # sudo sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
  #
  sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
  apt update

}

#
## Logic
#

# function install_wget {
#   wget &>/dev/null
#   if [ $? == "1" ]; then
#     already wget
#   else
#     installing wget
#     apt-get update
#     apt-get install -y wget
#     success wget
#   fi
# }

function install_nvm {
  if [ -a $HOME/.nvm ]; then
    already nvm
  else
    installing nvm
    #   -q,  --quiet               安静模式(无信息输出)
    #   -O,  --output-document=FILE    将文档写入 FILE
    wget -qO- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
    export NVM_DIR=$HOME/.nvm;
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
    # source $HOME/.nvm/nvm.sh;
    # . ~/.nvm/nvm.sh;
    # source ~/.bashrc
    nvm --version
    success nvm
  fi
}

function install_node {
  node -v &>/dev/null
  if [ $? == "0" ]; then
    already node
  else
    installing node
    # source $HOME/.nvm/nvm.sh;
    export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node/;
    # gyp WARN download NVM_NODEJS_ORG_MIRROR is deprecated and will be removed in node-gyp v4, please use NODEJS_ORG_MIRROR
    export NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node/;
    nvm install stable
    nvm use stable
    nvm alias default stable
    success node
  fi
}

function install_yarn {
  yarn -v &>/dev/null
  if [ $? == "0" ]; then
    already yarn
  else
    installing yarn
    apt install gnupg -y
    apt install gnupg1  -y
    apt install gnupg2  -y
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
    # https://yarnpkg.com/en/docs/install#debian-stable
    apt-get remove -y cmdtest
    apt-get update
    apt-get install -y yarn
    success yarn
  fi
}

function set_npm_mirror {
  if [ -a $HOME/.npmrc ]; then
    already set_npm_mirror
  else
    installing set_npm_mirror
    wget -qO- https://raw.githubusercontent.com/ly525/luban-h5/dev/deploy/mirror.sh | bash
    success set_npm_mirror
  fi
}



function install_strapi {
  strapi &>/dev/null
  if [ $? == "0" ]; then
    already Strapi
  else
    installing Strapi
    # 这里将mac怀疑安全性的包采取信任
    # https://github.com/strapi/strapi/issues/2787
    npm install --unsafe-perm=true --allow-root -g strapi@alpha
    # . ~/.nvm/nvm.sh
    # source $HOME/.nvm/nvm.sh;
    strapi -v
    success Strapi
  fi
}

function install_pm2 {
  pm2 &>/dev/null
  if [ $? == "1" ]; then
    already PM2
  else
    installing PM2
    npm install -g pm2@latest
    . ~/.nvm/nvm.sh
    pm2 update
    success PM2
  fi
}

function install_nginx {
  nginx &>/dev/null
  if [ $? == "1" ]; then
    already nginx
  else
    installing nginx
    apt-get update
    apt-get install -y nginx

    # Relpace nginx config file
    sed -i 's/www-data/root/g' /etc/nginx/nginx.conf
    cat > /etc/nginx/sites-available/default <<EOF
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##


# ----- luban nginx config start
server {

  listen 80 default_server;
  listen [::]:80 default_server;
  server_name _;

  client_body_buffer_size 4096M;
  client_max_body_size 4096M;
  proxy_buffer_size 128M;
  proxy_buffers 32 128M;
  proxy_busy_buffers_size 128M;

  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_disable "MSIE [1-6]\.";
  gzip_comp_level 3;
  gzip_types image/png application/json text/plain application/x-javascript text/css application/xml text/javascript application/javascript;

  location ~ ^/(upload|content-manager|users-permissions|works|admin|psd-files|workforms|third-libs|engine-assets) {

    proxy_set_header Host \$host;
    proxy_set_header X-Real-IP \$remote_addr;
    proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
    proxy_pass http://localhost:1337;
  }

  # location /index {
  #   root /home/centos/codebase/luban/luban-h5-dist/front-end/;
  #   #try_files \$uri \$uri/ /index.html;
  # }
  # location / {

  #   root /home/centos/codebase/luban/luban-h5-dist/landing-page/;
  #   #try_files \$uri \$uri/ /index.html;
  # }


  location / {
    root /root/luban-h5/front-end/h5/dist/;
    # index index.html;
    #try_files \$uri \$uri/ /index.html;
  }

}

# ----- luban nginx config end
EOF
    service nginx restart
    success nginx
  fi
}

function install_mongodb {
  mongo &>/dev/null
  if [ $? == "1" ]; then
    already MongoDB
  else
    installing MongoDB
    apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
    echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.4.list
    apt-get update
    apt-get install -y mongodb-org
    service mongod start
    success MongoDB
  fi
}


function install_luban-h5 {
  if [ -a $HOME/luban-h5 ]; then
    already luban-h5
  else
    installing luban-h5
    cd ~/
    git clone https://github.com/ly525/luban-h5

    # 鲁班前端
    cd luban-h5/front-end/h5 && yarn && yarn build && yarn engine:build

    cd -

    # 鲁班后端
    cd luban-h5/back-end/h5-api/
    yarn
    yarn build # build strapi admin
    pm2 start server.js
    pwd
    success luban-h5
  fi
}

#
## Main
#

set_apt_mirror


base_install vim
base_install wget
base_install curl
base_install git
base_install make
base_install gcc
base_install python
## for `yarn add sqlite3`
## https://packages.ubuntu.com/xenial/build-essential
## https://linuxconfig.org/how-to-install-g-the-c-compiler-on-ubuntu-18-04-bionic-beaver-linux
base_install build-essential


install_nvm
install_node
install_yarn
set_npm_mirror
install_strapi
install_pm2
install_nginx
install_luban-h5
exitBanner

echo
echo -e "${GREEN}[+]Done.${NEUTRAL}"

exec $SHELL -l