search
HomeOperation and MaintenanceLinux Operation and MaintenanceTeach you how to use shell scripts to quickly set up the server (with code)

之前的文章《一文讲解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

这个脚本里遇到的最大问题是,我们目前无法在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
This article is reproduced at:掘金. If there is any infringement, please contact admin@php.cn delete
The 5 Core Components of the Linux Operating SystemThe 5 Core Components of the Linux Operating SystemMay 08, 2025 am 12:08 AM

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The 5 Essential Elements of Linux: ExplainedThe 5 Essential Elements of Linux: ExplainedMay 07, 2025 am 12:14 AM

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux Operations: Security and User ManagementLinux Operations: Security and User ManagementMay 06, 2025 am 12:04 AM

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

Linux Operations: File System, Processes, and MoreLinux Operations: File System, Processes, and MoreMay 05, 2025 am 12:16 AM

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Linux Operations: Shell Scripting and AutomationLinux Operations: Shell Scripting and AutomationMay 04, 2025 am 12:15 AM

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux Operations: Understanding the Core FunctionalityLinux Operations: Understanding the Core FunctionalityMay 03, 2025 am 12:09 AM

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

Linux: Entering and Exiting Maintenance ModeLinux: Entering and Exiting Maintenance ModeMay 02, 2025 am 12:01 AM

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

Understanding Linux: The Core Components DefinedUnderstanding Linux: The Core Components DefinedMay 01, 2025 am 12:19 AM

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.

See all articles

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 Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools