Home Operation and Maintenance Linux Operation and Maintenance Teach you how to use shell scripts to quickly set up the server (with code)

Teach you how to use shell scripts to quickly set up the server (with code)

Sep 16, 2021 am 11:43 AM
linux

之前的文章《一文讲解css3实现椭圆轨迹旋转(总结)》中,给大家介绍了如何用css3实现椭圆轨迹旋转的方法。下面本篇文章给大家怎么使用shell脚本实现服务器快速设置,有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所助。

Teach you how to use shell scripts to quickly set up the server (with code)

通过shell脚本实现服务器快速设置

现在企业出海已经是常态,我们面临更多的国家地区服务器安装的工作。虽然云服务已经普及,还有各种SaaS可以快速使用,但定制化需求会一直存在,那么快速部署的方法就很有必要。

目前与那些快速部署方法?

  • 服务器镜像——最快,但必须在同一个供应商内,出海可能会不同地区用不同的云服务商,就无法实现。

  • docker——可以通过镜像方式快速部署,应对复杂环境;

  • shell脚本——可以简化大多数复杂应用的安装,适合系统环境变化不大的情况,例如可以用同一个版本系统。

虽然shell脚本的方式不如docker和镜像好使,但如果你还没有尝试过,可以试一下,作为一个工具,可以极大提高效率,因为我们也看到,很多的linux安装包也提供了类似的一键安装脚本。

在上脚本之前,先要说明一下我的服务器软件内容:

  • gitlab-runner集成部署

  • nodeJS环境

  • nodeJS标准的跑在gitlab-runner用户下

  • 使用nvm管理nodejs版本

  • 使用pm2管理进程

下面是以我在游戏出海时使用的shell脚本,简单说明如何通过shell脚本快速部署:

#!/bin/sh #第一句,基本是所有sh脚本的标准开头
# Centos 7.8  #说明这个脚本适用的系统和版本,给人看的

#硬盘目录:#变量声明
DISK_PATH="/data" #之前已经mount好的硬盘路径
#hostname:
HOSTNAME="my_server_name" #服务器名字,方便登录后区分
#gitlab-runner:
GITLAB_RUNNER="https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh"
#nodejs version
NODE_VERSION="v8.17.0"

#NVM #nvm安装脚本
NVM_SCRIPT="https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh"
#PM2 #pm2版本
PM2="pm2"
#pm2 data path: #pm2日志路径,我们不希望放到系统盘中
PM2_DATA_DIR="pm2Data" # 下方会组合成完整的路径 DISK_PATH+/PM2_DATA_DIR

echo "开始安装SDK channel套件============"

if [ -n "$HOSTNAME" ] # if的使用方式,大家需要去了解一下,-n是如果不为空的意思
then
  echo "修改hostname为:$HOSTNAME"
  hostnamectl set-hostname "$HOSTNAME"
else
  echo "可以使用 'hostnamectl set-hostname newHostname' #设置当前hostname,立即生效,重启也生效"
fi
echo "准备开始,当前hostname:" 
hostname

# 下面是一连串的安装和处理了

echo "基础准备:"
yum install epel-release -y
yum update -y

echo "安装 htop:"
yum install htop -y

echo "安装 ngnix:"
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx -y
systemctl enable nginx
systemctl start nginx
touch "/etc/nginx/conf.d/virtual.conf"

echo "安装gitlab-runner:"
# For RHEL/CentOS/Fedora
curl -L "$GITLAB_RUNNER" | sudo bash
yum install gitlab-runner -y
echo "alias lg='su -l gitlab-runner'" >> "$HOME/.bashrc"
source "$HOME/.bashrc"

echo "建立pm2Data目录:"
PM2_FINALDIR="$DISK_PATH/$PM2_DATA_DIR"
if [ ! -d "$PM2_FINALDIR" ]; then #-d是判断是不是目录,不是就创建
  mkdir "$PM2_FINALDIR"
fi
chown gitlab-runner:gitlab-runner "$PM2_FINALDIR" #修改目录owner

# 我会生成一个新的脚本到gitlab-runner目录,为什么这样做呢?
# 因为需要在gitlab-ruuner用户跑服务,希望环境在目标用户那里。
# 另外的原因是,以其他用户身份安装pm2会出现问题,那我就跑2次脚本咯

echo "切换用户安装 node 套件"
######## nodejs.sh Start########
NODE_SHELL='/home/gitlab-runner/nodejs.sh'

echo "#!/bin/sh
# Centos 7.8

#硬盘目录:
#nodejs version
NODE_VERSION=\"$NODE_VERSION\"
PM2_FINALDIR=\"$PM2_FINALDIR\"

#NVM
NVM_SCRIPT=\"$NVM_SCRIPT\"
#PM2
PM2=\"$PM2\"

echo \"安装 nvm\"
curl -o- $NVM_SCRIPT | bash
echo \"安装 node $NODE_VERSION:\"
source \"\$HOME/.bashrc\"
nvm install $NODE_VERSION
nvm use $NODE_VERSION
echo \"安装pm2: $PM2\"  
npm i $PM2 -g
npm install --global coffeescript
npm install --global gulp

# pm2 目录的骚操作
PM2_HOME=\"\$HOME/.pm2\"
if [ -d \$PM2_HOME ]; then
  echo '处理.pm2目录'
  pwd
  ll
  mv \$PM2_HOME $PM2_FINALDIR
  rm \$PM2_HOME -r
  ln -s $PM2_FINALDIR/ \$PM2_HOME
else 
  echo -e '.pm2 目录不存在'
  ln -s $PM2_FINALDIR/ \$PM2_HOME
fi
source \"\$HOME/.bashrc\"
echo \"NodeJS 套件结束\"" > "$NODE_SHELL"

chown gitlab-runner:gitlab-runner "$NODE_SHELL"
chmod +x "$NODE_SHELL" # 让脚本可以运行
# sudo -H -u gitlab-runner bash -c "$NODE_SHELL" #无法跑pm2安装
echo "将切换到gitlab-runner 用户,然后运行./nodejs.sh"
######## nodejs.sh End ########
su -l gitlab-runner #切换用户,跑./nodejs.sh,就可以安装好nodejs环境

sudo gitlab-runner register # 注册gitlab-runner

echo "完成"
exit 0
Copy after login

这个脚本里遇到的最大问题是,我们目前无法在root中安装pm2,会报错。当然也可以通过研究解决,但万一以后pm2版本更新了呢?研究出来的方法是不是不能用?所以干脆脚本生成脚本,再跑一次。coding的时候记得字符串中的转义。

通过这个方式,我实现了另外2个共3个为一组的服务群快速安装。中间发生了一次,安装了一半的时候,发现供应商给错服务器了,只能3个服务器都需要重新安装!还好有脚本,否则真的是灾难!

推荐学习:Linux教程

The above is the detailed content of Teach you how to use shell scripts to quickly set up the server (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

See all articles