


Teach you how to use shell scripts to quickly set up the server (with code)
之前的文章《一文讲解css3实现椭圆轨迹旋转(总结)》中,给大家介绍了如何用css3实现椭圆轨迹旋转的方法。下面本篇文章给大家怎么使用shell脚本实现服务器快速设置,有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所助。
通过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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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)

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.

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.

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.

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.

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.

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.

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
