分类 linux 下的文章

  • 备份原有的程序
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  • centos主机
比如centos7, 执行下面的命令, 如果是其他版本, 把7缓存对应的版本即可
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  • 之后运行 yum makecache 生成缓存

  • 网络环境
192.168.56.1 为不可联网的局域网
192.168.2.1 为可联网的局域网

一般是路由问题, 路由优先级高的路由是不能联网的网段, 导致了无法联网

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.56.1    0.0.0.0         UG    100    0        0 enp0s3
0.0.0.0         192.168.2.1     0.0.0.0         UG    101    0        0 enp0s8
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-bc50ad6b6c9b
172.21.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-bf445a7984dd
172.23.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-ed0e3c7e7e65
172.30.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-8c9e9e38a72d
192.168.2.0     0.0.0.0         255.255.255.0   U     101    0        0 enp0s8
192.168.32.0    0.0.0.0         255.255.240.0   U     0      0        0 br-f1aee05d16bb
192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
192.168.144.0   0.0.0.0         255.255.240.0   U     0      0        0 br-696dcda373dd
192.168.160.0   0.0.0.0         255.255.240.0   U     0      0        0 br-22be33c502cc
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         _gateway        0.0.0.0         UG    100    0        0 enp0s3
default         _gateway        0.0.0.0         UG    101    0        0 enp0s8
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-bc50ad6b6c9b
172.21.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-bf445a7984dd
172.23.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-ed0e3c7e7e65
172.30.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-8c9e9e38a72d
192.168.2.0     0.0.0.0         255.255.255.0   U     101    0        0 enp0s8
192.168.32.0    0.0.0.0         255.255.240.0   U     0      0        0 br-f1aee05d16bb
192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
192.168.144.0   0.0.0.0         255.255.240.0   U     0      0        0 br-696dcda373dd
192.168.160.0   0.0.0.0         255.255.240.0   U     0      0        0 br-22be33c502cc
  • 删除命令
[root@localhost ~]# route del -net default netmask 0.0.0.0 metric 100

参考内容, 命令输错了, 给了提示, 参考提示写的
[root@localhost ~]# route del -net default netmask 0.0.0.0 MASK enp0s3
Usage: inet_route [-vF] del {-host|-net} Target[/prefix] [gw Gw] [metric M] [[dev] If]
       inet_route [-vF] add {-host|-net} Target[/prefix] [gw Gw] [metric M]
                              [netmask N] [mss Mss] [window W] [irtt I]
                              [mod] [dyn] [reinstate] [[dev] If]
       inet_route [-vF] add {-host|-net} Target[/prefix] [metric M] reject
       inet_route [-FC] flush      NOT supported
  • 再次ping www.baidu.com
一切正常了, 但是重启后这条路由依旧存在, 查询了文档得知这种修改只是临时性的, 需要修改对应的网卡配置文件才行
  • [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=no # 原来这一行为yes, 我改为no后, 重启后那条路由不见了, ping百度也正常了
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=7e7e1163-222c-4630-8478-b0708e85ac2d
DEVICE=enp0s3
ONBOOT=yes

IPADDR=192.168.56.7
NETMASK=255.255.255.0
GATEWAY=192.168.56.1
#DNS1=192.168.56.1
  • route -n
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.2.1     0.0.0.0         UG    101    0        0 enp0s8
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-bc50ad6b6c9b
172.21.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-bf445a7984dd
172.23.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-ed0e3c7e7e65
172.30.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-8c9e9e38a72d
192.168.2.0     0.0.0.0         255.255.255.0   U     101    0        0 enp0s8
192.168.32.0    0.0.0.0         255.255.240.0   U     0      0        0 br-f1aee05d16bb
192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
192.168.144.0   0.0.0.0         255.255.240.0   U     0      0        0 br-696dcda373dd
192.168.160.0   0.0.0.0         255.255.240.0   U     0      0        0 br-22be33c502cc

  • 创建 index.php 发布入口文件
<?php
if (empty($_SERVER['DOCUMENT_ROOT']) || strpos($_SERVER['DOCUMENT_ROOT'], ':') === false) {
    $hostSystem = 'linux';
} else {
    $hostSystem = 'windows';
}
echo '<h1><a href="">系统版本: ' . $hostSystem . '</a></h1>';

$baseDir = dirname(__FILE__, 2);
$baseDir = str_replace('\\', '/', $baseDir);
$dirs = scandir($baseDir);
unset($dirs[0]);
unset($dirs[1]);
$dirList = [];
foreach ($dirs as $key => $dir) {
    if (!is_dir($baseDir . '/' . $dir . '/.git')) {
        unset($dirs[$key]);
    } else {
        $dirListItem = [
            'value' => $dir,
            'app_type' => '',
        ];
        if (file_exists($baseDir . '/' . $dir . '/src/public/index.php') || file_exists($baseDir . '/' . $dir . '/public/index.php')) {
            $dirListItem['app_type'] = 'laravel';
        } else if (is_dir($baseDir . '/' . $dir . '/src/runtime') || is_dir($baseDir . '/' . $dir . '/runtime')) {
            $dirListItem['app_type'] = 'hyperf';
        } else {
            $dirListItem['app_type'] = 'front';
        }
        $dirList[$dir] = $dirListItem;
    }
}
$dirs = array_values($dirs);
$requestMethod = empty($_SERVER['REQUEST_METHOD']) ? 'GET' : $_SERVER['REQUEST_METHOD'];
switch ($requestMethod) {
    case 'GET':
        echo '<form action="" method="POST">
<h3>选择需要执行的目录: </h3>
';
        $dirLists = [];
        foreach ($dirList as $dirListItem) {
            $dirLists[$dirListItem['app_type']][] = $dirListItem;
        }
        foreach ($dirLists as $appType => $dirList) {
            echo '<h2><font style="color: blue;">' . $appType . '</font></h2>';
            foreach ($dirList as $dirListItem) {
                echo '<label><input type="radio" name="dir" value="' . $dirListItem['value'] . '">' . $dirListItem['value'] . '</label>';
            }
        }
        echo '
<br><input type="submit" value="执行">
</form>
<style>
    label{
        font-weight: lighter;
    }
</style>';
        break;
    case 'POST':
        // 执行发布
        $request = $_REQUEST;
        $dir = empty($_POST['dir']) ? '' : $_POST['dir'];
        if (empty($dir)) {
            die('请选择目录');
        }
        if (empty($dirList[$dir])) {
            die('目录不在白名单中');
        }

        $cmd = [];

        $appType = $dirList[$dir]['app_type'];
        $startTTL = microtime(true);
        echo "<pre>";
        if ($hostSystem == 'windows') {
        } else {
            $da = [];

            $branch = 'develop';
            $branch = 'master';
            switch ($appType) {
                case 'hyperf':
                    $shell = 'sudo ' . dirname(__FILE__) . '/php_deploy_hyperf.sh ' . $dir . ' ' . $branch;
                    echo '<h3>' . $shell . '</h3>';
                    system($shell, $status);
                    break;
                case 'laravel':
                    $shell = 'sudo ' . dirname(__FILE__) . '/php_deploy_laravel.sh ' . $dir . ' ' . $branch;
                    echo '<h3>' . $shell . '</h3>';
                    system($shell, $status);
                    break;
                case 'front':
                    $shell = 'sudo ' . dirname(__FILE__) . '/php_deploy_front.sh ' . $dir . ' ' . $branch;
                    echo '<h3>' . $shell . '</h3>';
                    system($shell, $status);
                    break;
                default:
                    die('未能识别需要执行的代码类型');
            }
        }
        echo '用时: ' . intval((microtime(true) - $startTTL) * 1000) / 1000;
        echo '</pre>';
        if ($status) {
            echo "执行失败";
        } else {
            echo "执行成功";
        }
        break;
}
  • vim 前端用到的发布文件 vim php_deploy_front.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/root/.nvm/versions/node/v13.9.0/bin
export PATH
project_dir=/home/project
project_name=$1
echo $project_dir"/"$project_name
branch=$2

function gitPullCover(){
    project_name=$1
    branch=$2
    project_dir=$3
    self_path=$project_dir"/history/"$project_name;
    cp -rf $project_dir"/"$project_name $project_dir"/history/"
    history_path=$project_dir"/history/"$project_name
    covertimes=5;
    if [ ! -d $history_path ];then
      mkdir $history_path
      covertimes=20
    fi
    history_path=$self_path"_history"
    if [ ! -d $history_path ];then
      mkdir $history_path
    fi

    echo "覆盖"covertimes"版本"
    covertimes=5
    for vv in {5..1}
    do
        cd $self_path
        git checkout $branch
        git_hashcode=`git log -n $vv --pretty=format:"%H"`
        v2=0;
        for line in $git_hashcode
        do
            declare -i v2=$v2+1;
            if [ "$v2" -eq "$vv" ]
            then
                echo $line;
                git checkout $line
                git reset --hard
                cd ../
                echo "$self_path/* $history_path"
                cp -rf $self_path/* $history_path
            fi
        done
    done
    cd $self_path
    git checkout $branch
}

function gitPull(){
    cd $1
    echo `git log -n 1`
    git log -n 1
    git checkout .
    git pull origin $branch
}

function dockerReload(){
    cd $1"/src"
    docker-compose down && docker-compose up -d
}

gitPullCover $project_name $branch $project_dir

gitPull $project_dir"/"$project_name $branch
  • vim 后端laravel用到的发布文件 vim php_deploy_laravel.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/root/.nvm/versions/node/v13.9.0/bin
export PATH
project_dir=/home/project/"$1"
echo $project_dir
branch=$2

function gitPull(){
    cd $1
    echo `git log -n 1`
    git log -n 1
    git checkout .
    git pull origin $2
}

gitPull $project_dir $branch
  • vim 后端hyperf框架用到的发布文件 vim php_deploy_hyperf.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/root/.nvm/versions/node/v13.9.0/bin
export PATH
project_dir=/home/project/"$1"
echo $project_dir
branch=$2

function gitPull(){
    cd $1
    echo `git log -n 1`
    git log -n 1
    git checkout .
    git pull origin $branch
}

function dockerReload(){
    cd $1"/src"
    docker-compose down && docker-compose up -d
}

gitPull $project_dir

dockerReload $project_dir
  • 如果无法执行, 检查是否权限问题, 是否开启发布的用户的 sudo 的免密登录

相同的文件内容的shell, 相同的权限, 出现部分情况下无法正确执行的情况

遇到过一起, window下创建的shell, 拷贝到linux下同php无法正确全部执行其中的shell内容, 后来打开看了发现, window下创建的脚本通过vim打开多个 xxx.sh" [dos] 12L, 330C 其中的 [doc] 导致脚本一些执行上的问题
通过cat -A xxx.sh 显示的换行符为 ^M$, linux下创建的文件换行符为 $
转换window换行符为linux sed -i 's/\r//' 文件名
即使解决了换行符问题, 还有新的编码问题出现, 所以脚本千万别在window机器下编写拷贝到linux机器中, 需要通过linux创建文件编辑, window拷贝内容粘贴进去最靠谱

设置普通用户输入sudo,免密进入root账户

方法一:
编辑并在用户相关的内容后面添加需要免密登录的帐号
vim /etc/sudoers
> admin ALL=(ALL) NOPASSWD: ALL

方法二: (未测试)
开始系统内部的wheel用户组,
在/etc/suoers 中编辑配置文件如下:
%wheel ALL=(ALL) NOPASSWD: ALL
比如新开账户为mike,
useradd mike|echo "1234546"|passwd mike
id mike(查看用户归属用户组)---uid 和 gid
uid=505(mike) gid=505(mike) groups=505(mike)
通过usermod 命令更改用户所属用户组

[root@backup ~]# usermod mike -g wheel
[root@backup ~]# id mike
uid=505(mike) gid=10(wheel) groups=10(wheel)

更改为wheel组下的 ,
然后在mike用户下再sudo su 切换既不用输入密码了