In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use shell script to achieve server quick setup". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Realize server quick setup through shell script
Now it is the norm for enterprises to go to sea, and we are faced with more work of installing servers in countries and regions. Although cloud services are ubiquitous and various SaaS can be used quickly, customization requirements will always be there, so a rapid deployment approach is necessary.
What are the current rapid deployment methods?
Server image-the fastest, but it must be within the same vendor, and it may not be possible to go out to sea with different cloud service providers in different regions.
Docker-- can be quickly deployed through mirroring to deal with complex environments.
Shell script-can simplify the installation of most complex applications, suitable for situations where the system environment does not change much, for example, you can use the same version of the system.
Although shell scripting doesn't work as well as docker and mirroring, if you haven't tried it, you can try it as a tool that can greatly improve efficiency, because we've also seen that many linux installers provide similar one-click installation scripts.
Before I go to the script, I'd like to explain the content of my server software:
Gitlab-runner integrated deployment
NodeJS environment
NodeJS standard runs under gitlab-runner users.
Use nvm to manage nodejs versions
Use pm2 to manage processes
Here is a brief explanation of how to quickly deploy through the shell script that I used when I went out to sea:
#! / bin/sh # the first sentence is basically the standard beginning of all sh scripts # Centos 7.8 # indicates the system and version this script applies to, and shows the # hard disk directory: # variable declaration DISK_PATH= "/ data" # the hard disk path that has been mount before # hostname:HOSTNAME= "my_server_name" # server name Easy to distinguish between # gitlab-runner:GITLAB_RUNNER= "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh"#nodejs versionNODE_VERSION=" v8.17.0 "# NVM # nvm installation script NVM_SCRIPT=" https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh"#PM2 # pm2 version PM2= "pm2" # pm2 data path: # pm2 log path after login We do not want to put it in the system disk under PM2_DATA_DIR= "pm2Data" # will be combined into a complete path DISK_PATH+/PM2_DATA_DIRecho "start to install SDK channel suite =" if [- n "$HOSTNAME"] # if usage, you need to know -n means if not empty means then echo "modify hostname to: $HOSTNAME" hostnamectl set-hostname "$HOSTNAME" else echo "you can use 'hostnamectl set-hostname newHostname' # to set the current hostname, effective immediately, restart is also effective" fiecho ready to start Current hostname: "hostname# below is a series of installation and processing of echo" basic preparation: "yum install epel-release-yyum update-yecho" installation htop: "yum install htop-yecho" installation ngnix: "rpm-ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpmyum install nginx-ysystemctl enable nginxsystemctl start nginxtouch" / etc/nginx/conf.d/virtual.conf "echo" installation Gitlab-runner: "# For RHEL/CentOS/Fedoracurl-L" $GITLAB_RUNNER "| sudo bashyum install gitlab-runner-yecho" alias lg='su-l gitlab-runner' "> >" $HOME/.bashrc "source" $HOME/.bashrc "echo" create pm2Data directory: "PM2_FINALDIR=" $DISK_PATH/$PM2_DATA_DIR "if [!-d" $PM2_FINALDIR "] Then #-d is to determine whether it is a directory or not, not to create a mkdir "$PM2_FINALDIR" fichown gitlab-runner:gitlab-runner "$PM2_FINALDIR" # modify the directory owner# I will generate a new script to the gitlab-runner directory, why? # because you need to run the service in gitlab-ruuner, you want the environment to be there for the target user. # another reason is that there is a problem installing pm2 as another user Then I'll run the script twice. Echo "switch user installation node suite" # nodejs.sh Start#NODE_SHELL='/home/gitlab-runner/nodejs.sh'echo "#! / bin/sh# Centos 7." hard disk directory: # nodejs versionNODE_VERSION=\" $NODE_VERSION\ "PM2_FINALDIR=\" $PM2_FINALDIR\ "# NVMNVM_SCRIPT=\" $NVM_SCRIPT\ "# PM2PM2=\" $PM2\ "echo\" install Nvm\ "curl-o-$NVM_SCRIPT | bashecho\" install node $NODE_VERSION:\ "source\"\ $HOME/.bashrc\ "nvm install $NODE_VERSIONnvm use $NODE_VERSIONecho\" install pm2: $PM2\ "npm I $PM2-gnpm install-- global coffeescriptnpm install-- global gulp# pm2 directory PM2_HOME=\"\ $HOME/.pm2\ "if [- d\ $PM2_HOME] Then echo 'processes the .pm2 directory' pwd ll mv\ $PM2_HOME $PM2_FINALDIR rm\ $PM2_HOME-r ln-s $PM2_FINALDIR/\ $PM2_HOMEelse echo-e'. The pm2 directory does not exist in'ln-s $PM2_FINALDIR/\ $PM2_HOMEfisource\ "\ $HOME/.bashrc\" echo\ "NodeJS Suite ends\" > "$NODE_SHELL" chown gitlab-runner:gitlab-runner "$NODE_SHELL" chmod + x "$NODE_SHELL" # so that the script can Run # sudo-H-u gitlab-runner bash-c "$NODE_SHELL" # unable to run pm2 install echo "will switch to the gitlab-runner user Then run. / nodejs.sh "# nodejs.sh End # su-l gitlab-runner # switch users, run. / nodejs.sh, you can install the nodejs environment sudo gitlab-runner register # register gitlab-runnerecho" complete "exit 0
The biggest problem with this script is that we are currently unable to install pm2 in root and will report an error. Of course, it can be solved through research, but what if the pm2 version is updated later? Is the method developed can't be used? So just generate the script and run it again. Remember the escape in the string when coding.
In this way, I implemented the rapid installation of two other three service clusters in a group. It happened once in the middle, half of the installation, found that the supplier gave the wrong server, only 3 servers need to be reinstalled! Fortunately, there is a script, otherwise it would be a disaster!
This is the end of the content of "how to use shell script to achieve server quick setup". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.