Shell编程基础之三个实战

例1:
部署服务器初始环境

[root@localhost 20200421]# vim README.md
## 初始化服务器
- author:TNT
- email:sam19920329@gmail.com
- date:2020/04/21

---
介绍:
针对于企业内部刚刚引进的服务器,如何进行基本软件的安装.

服务器刚刚装完系统后:需要安装的工具有:net-tools;ntpdate;epel-release;wget;Development Tools;
                     服务器的基本配置:firewalld=off;SElinux=disabled;同步时间;

[root@localhost 20200421]# vim initserver.sh

#!/usr/bin/env bash
# author:TNT
# email:sam19920329@gmail.com
# datae:2020/04/21
# usage:实战初始化服务器

#1:关闭防火墙和SElinux
/bin/systemctl stop firewall && /bin/systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' etc/selinux/config
/sbin/setenforce 0

#2:安装基本的软件
/bin/yum -y install net-tools epel-release ntpdate wget
if [ $? -eq 0 ];then
  /bin/yum -y groupinstall "Development Tools"
else
 printf "脚本没运行成功,可能有软件安装失败.\n"
 exit 1
fi

#3:同步互联网时间
/sbin/ntpdate -b ntp1.aliyun.com

然后传送到远程服务器去.

[root@localhost 20200421]# scp initserver.sh root@1.1.1.1:/opt/

然后ssh远程过去执行脚本.

[root@localhost 20200421]# ssh root@1.1.1.1 "bash /opt/initserver.sh"

然后就简单部署完毕服务器初始环境了,可以根据需求修改脚本文件.

例2:
部署配置java环境:
先去官网下载jdk的包.jdk11或者jdk8.
在这里插入图片描述
然后再检查下服务器有没有java这个命令.

[root@localhost jdkdeploy]# which java
/usr/local/jdk/bin/java
[root@localhost jdkdeploy]# echo $PATH
/usr/local/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

环境变量,调用PATH的一个值,它的值是下面那些,里面存储的都是一些命令.比如ls ip等等,也就是因为环境变量的作用,才可以让我们在系统的任意一个位置去调用里面的命令.

手动部署:

[root@localhost jdkdeploy]# tar xf jdk-11.0.7_linux-x64_bin.tar.gz -C /usr/local/
[root@localhost jdkdeploy]# cd /usr/local/
[root@localhost local]# ls
bin  etc  games  include  jdk  jdk-11.0.7  lib  lib64  libexec  nginx  sbin  share  src  tomcat
看到里面有个jdk-11.0.7

其实现在已经算是安装好了,linux里面安装软件其实就是去拷贝.

接着我们要把这个jdk-11.0.7改名,改成jdk-11

[root@localhost ~]# mv /usr/local/{jdk-11.0.7,jdk-11}

要去一个环境变量的配置文件中去声明JDK,去新建一个配置JDK环境变量的脚本.

[root@localhost ~]# vim /etc/profile.d/
#声明JAVA的家目录
export JAVA_HOME=/usr/local/jdk-11
#把JAVA的家目录以及JAVA的命令路径添加到环境变量里
export PATH=$PATH:$JAVA_HOME/bin
[root@localhost ~]# source /etc/profile
[root@localhost ~]# which java
/usr/local/jdk/bin/java

然后你的JAVA就能成功的在你的系统中被应用.
上面这样就是手动简单的把JAVA开发环境部署好了.

脚本自动jdk部署:

[root@localhost jdkdeploy]# vim jdkdeploy.sh

# author:TNT
# email:sam19920329@gmail.com
# date:2020/04/21
# usage:脚本部署jdk环境


# 1.解压jdk--> /usr/local
/bin/tar xf $1 -C /usr/local/
mv /usr/local/{jdk*,jdk}

# 2.添加java到环境变量$PATH
echo 'export JAVA_HOME=/usr/local/jdk' >/etc/profile.d/jdkenv.sh
echo 'export PATH=$PATH:$JAVA_HOME/bin' >>/etc/profile.d/jdkenv.sh

# 3.让JAVA环境变量生效
source /etc/profile
[root@localhost jdkdeploy]# bash jdkdeploy.sh jdk-11.0.7_linux-x64_bin.tar.gz 

例3:
lnmp环境部署

主要是在linux里把nginx,myaridb,php安装到我们服务器里.

手动部署:

安装nginx:

[root@localhost lnmpArch]# yum -y install epel-release
[root@localhost lnmpArch]# yum -y install nginx
[root@localhost lnmpArch]# systemctl start nginx.service
[root@localhost lnmpArch]# cd /etc/nginx/
[root@localhost nginx]# vim conf.d/server.conf

:r ./nginx.conf 把配置文件读过来修改.

location / {
    root   /website/html;
    index  index.php index.html index.htm;

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    root           /website/html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
[root@localhost nginx]# systemctl restart nginx.service
[root@localhost nginx]# mkdir -p /website/html
[root@localhost nginx]# vim /website/html/index.html

然后浏览器访问测试nginx即可.

安装mysql:

[root@localhost ~]# yum -y install mariadb-server mariadb
[root@localhost ~]# systemctl start mariadb

安装php:

[root@localhost ~]# yum -y install php php-fpm php-mysql php-devel php-mbstring php-mcrypt php-gd php-xml
[root@localhost ~]# systemctl start php-fpm.service

如果要调试nginx -php -mysql这三者的连通性,我们会用到一段PHP的代码.
https://www.php.net/manual/zh/function.mysql-connect.php

<?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?>

修改下数据库的用户和密码,复制到下面首页.然后用浏览器访问

[root@localhost ~]# vim /website/html/index.php

脚本部署lnmp:

#!/usr/bin/env bash
#
# author: TNT
# email: sam19920329@gmail.com
# date: 2020/04/22
# usage: 部署lnmp架构

#1.安装nginx并且配置nginx
yum -y install nginx
cp -f nginx.conf /etc/nginx/nginx.conf
cp server.conf /etc/nginx/conf.d/server.conf
mkdir -p /website/html
cp index.php /website/html/index.php

systemctl start nginx

#2.安装数据库
yum -y install mariadb-server mariadb
systemctl start mariadb
sleep 5
mysql -uroot -e "create database wordpress;"
mysql -uroot -e "create user TNT@localhost identified by '123456'"

#3.安装php环境
yum -y install php php-fpm php-mysql php-devel php-mbstring php-mcrypt php-gd php-xml
systemctl start php-fpm

然后浏览器访问测试访问.
如果想上线什么项目的话就把代码内容放到脚本文件指定的目录/website/html/index.php就可以.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值