初果实验室

  • ThinkPad 笔记本安装 Ubuntu 22.04 LTS 散记

    前因

    家里有一台安装了 Windows 10 操作系统的 ThinkPad 笔记本,

    由于使用年限较长,启动后有屏幕闪烁的问题,

    这台机器安装有两块硬盘,

    一块是128GB 的固态硬盘,安装了操作系统,剩余空间不到5GB,

    另外一块是1TB的机械硬盘,剩余空间还有400GB,

    闲置比较浪费,想利用起来,

    于是使用 DiskGenius 将剩余空间划分出 200GB

    用来安装 Ubuntu 操作系统,这样可以通过 SSH 远程访问继续使用,

    原 Windows 系统 NTFS 格式的分区可以挂载在 Ubuntu 系统下面来访问。

    准备

    U盘一个,镜像制作工具  https://rufus.ie/zh/

    分区类型选择 GPT,之前选择 MBR 一直无法进入安装界面,

    文件系统选择 NTFS,

    Ubuntu 22.04 LTS 操作系统下载地址:

    https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/22.04.4/

    个人使用建议选择桌面版 ubuntu-22.04.4-desktop-amd64.iso

    安装

    讲U盘插入电脑,制作完毕后将U盘插入电脑,

    系统启动时进入 BIOS 界面,

    选择从 USB Disk 启动,保存后重启即进入 Ubuntu 安装流程,

    接下来就是选择键盘布局、语言、时区、配置WiFi、设置用户名密码等步骤。

    远程访问

    安装完成后的一些初始化操作:

    安装时只能添加普通权限的用户,

    修改 root 用户密码

    sudo passwd root

    修改完后 使用命令 su 切换到 root 用户 就拥有了最高权限。

    安装 openssh-server

    apt install openssh-server

    再将 /etc/ssh/sshd_config 中的 PermitRootLogin 的值修改为 yes

    PermitRootLogin yes

    删除行首 # 字符才能生效,

    重新启动 sshd

    service sshd start

    重新启动 ssh

    service sshd restart

    查看 22 端口状态

    lsof -i:22

  • 使用 Nginx 反向代理加速 github 访问

    由于国内某些原因,访问 github.com 很不稳定,

    可以在一台访问 github 流畅的服务器上部署 Nginx 作为代理,

    假如代理服务器 域名是 proxy.server.name

    对于 git clone https://github.com/ohmyzsh/ohmyzsh.git

    可修改为  git clone https://proxy.server.name/ohmyzsh/ohmyzsh.git

    Nginx 配置如下:

    server {
        listen 80;
        server_name proxy.server.name;
        location /{
            proxy_pass https://github.com;
        }
    }
  • 终端软件 Tabby Terminal 介绍

    Windows 下的 Terminal  种类比较多,

    最近发现的一款开源的可跨平台使用的终端 Tabby  各种功能很好用,

    之前在 Windows 下面使用最多的是 vandyke 的共享软件  SecureCRT

    缺点是订阅价格挺高,打算逐渐放弃 SecureCRT 转而使用 Tabby

    Putty  也有一部分用户,但是多会话管理功能比较弱,

    微软的 Windows Terminal ,目前功能还不够完善,

    MacOS 和 Linux 操作系统都有自带的 Terminal ,

    MacOS  下 iterm2 也比较流行。

    Tabby 可以备份配置文件,同步至远端服务器,

    这样更换电脑就不需要重新填写配置信息了。

  • Ubuntu更换阿里云软件源

    Ubuntu 22.04 LTS 阿里云源

    修改文件 /etc/apt/sources.list

    deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
    deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
    deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
    deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
    deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
    deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
    
    
    
    

    然后执行 apt update

    如果提示 GPG error 类型的错误,

    将协议修改为 https 再 update,

    不同版本的源不能混合使用,因为代号不一样。

  • npm run dev 报错 System limit for number of file watchers reached 解决方法

    npm run dev 的时候遇到下面的错误信息,

    System limit for number of file watchers reached

    意思就是监视的文件总数超过了系统限制,

    在开发环境,为了修改文件后可以实时刷新页面,

    所以需要监视文件的修改。

    修改这个限制是一种解决方法,可以参考下面的链接

    [ https://bbs.huaweicloud.com/blogs/detail/108323 ]

    但是如果系统配置较低,这个数值不能太大。

    由于我使用 vite 作为打包工具,

    解决办法之一是忽略某些类库目录的监控,

    可以在 配置文件 vite.config.js 的 server watch 里面配置 ignores 参数 ,

    下面的配置就是忽略了 vendor 目录。

    export default defineConfig({
        server:{
          watch: {
                ignored: ['**/vendor/**'],
          },
        }
    });

    参考链接

    [ https://vitejs.dev/config/server-options.html#server-watch ]

  • tmux 常用命令

    tmux 是一个终端复用软件,类似 GNU/Screen

    基本命令

    tmux new -s session 创建名称为 session 的会话

    tmux ls 会话列表

    tmux kill-session -t session 注销名称为 session 的会话

    tmux a -t  session 进入名称为 session 的会话

    tmux a  进入默认第一个会话

    会话内部命令

    <leader>  为命令前置按键,默认为  <Ctrl + B>

    即在执行任何命令前,需要先按下 <Ctrl + B>

    释放后再按其他键,可通过修改 ~/.tmux.conf 配置来自定义前置按键

    <leader> +  ?   命令清单

    <leader> + c        新建 window

    <leader> + &      关闭 window

    <leader> + n      下一个 window

    <leader> + p       前一个 window

    <leader> + ,       修改 window 名称

    <leader> + d       退出当前会话

    <leader> + $num    切换第 $num 个 window

    <leader> +  “    纵向切割当前 window,变为左右2个pane

    <leader> +  %    横向切割当前 window, 变为上下2个pane

    <leader> +  :source-file ~/.tmux.conf    重载 tmux 配置

    待翻译:

    <leader>+ C-z Suspend the current client
    <leader>+ Space Select next layout
    <leader>+ ! Break pane to a new window
    <leader>+ # List all paste buffers
    <leader>+ $ Rename current session
    <leader>+ &amp; Kill current window
    <leader>+ ' Prompt for window index to select
    <leader>+ ( Switch to previous client
    <leader>+ ) Switch to next client
    <leader>+ , Rename current window
    <leader>+ . Move the current window
    <leader>+ / Describe key binding
    <leader>+ 0 Select window 0
    <leader>+ 1 Select window 1
    <leader>+ 2 Select window 2
    <leader>+ 3 Select window 3
    <leader>+ 4 Select window 4
    <leader>+ 5 Select window 5
    <leader>+ 6 Select window 6
    <leader>+ 7 Select window 7
    <leader>+ 8 Select window 8
    <leader>+ 9 Select window 9
    <leader>+ : Prompt for a command
    <leader>+ ; Move to the previously active pane
    <leader>+ = Choose a paste buffer from a list
    <leader>+ ? List key bindings
    <leader>+ C Customize options
    <leader>+ D Choose a client from a list
    <leader>+ E Spread panes out evenly
    <leader>+ L Switch to the last client
    <leader>+ M Clear the marked pane
    <leader>+ [ Enter copy mode
    <leader>+ ] Paste the most recent paste buffer
    <leader>+ c Create a new window
    <leader>+ d Detach the current client
    <leader>+ f Search for a pane
    <leader>+ i Display window information
    <leader>+ n Select the next window
    <leader>+ o Select the next pane
    <leader>+ p Select the previous window
    <leader>+  q       Display pane numbers
    <leader>+  s       Choose a session from a list     
    <leader>+  t       Show a clock             
    <leader>+  w       Choose a window from a list
    <leader>+  x       Kill the active pane 
    <leader>+  z       Zoom the active pane   
    <leader>+  {       Swap the active pane with the pane above
    <leader>+  }       Swap the active pane with the pane below
    <leader>+  ~       Show messages  
    <leader>+  DC      Reset so the visible part of the window follows the cursor
    <leader>+  PPage   Enter copy mode and scroll up
    <leader>+  Up      Select the pane above the active pane
    <leader>+  Down    Select the pane below the active pane
    <leader>+  Left    Select the pane to the left of the active pane
    <leader>+  Right   Select the pane to the right of the active pane
    <leader>+  M-1     Set the even-horizontal layout
    <leader>+  M-2     Set the even-vertical layout
    <leader>+  M-3     Set the main-horizontal layout
    <leader>+  M-4     Set the main-vertical layout      
    <leader>+  M-5     Select the tiled layout          
    <leader>+  M-n     Select the next window with an alert
    <leader>+  M-o     Rotate through the panes in reverse
    <leader>+  M-p     Select the previous window with an alert
    <leader>+  M-Up    Resize the pane up by 5
    <leader>+  M-Down  Resize the pane down by 5
    <leader>+  M-Left  Resize the pane left by 5
    <leader>+  M-Right Resize the pane right by 5
    <leader>+  C-Up    Resize the pane up                
    <leader>+  C-Down  Resize the pane down
    <leader>+  C-Left  Resize the pane left     
    <leader>+  C-Right Resize the pane right
    <leader>+  S-Up    Move the visible part of the window up
    <leader>+  S-Down  Move the visible part of the window down
    <leader>+  S-Left  Move the visible part of the window left
    <leader>+  S-Right Move the visible part of the window right

    配置文件路径:~/.tmux.conf

    推荐配置  https://github.com/gpakosz/.tmux

    推荐状态栏 statusline 插件 https://github.com/powerline/powerline

    推荐插件管理器  https://github.com/tmux-plugins/tpm

    cheatsheet  https://tmuxcheatsheet.com/

    tmux Guide https://tmuxguide.readthedocs.io/en/latest/tmux/tmux.html

  • neovim 配置 Github Copilot

    Github Copilot 针对 neovim 开发了一款插件 Copilot.vim

    安装之前确保 neovim 的版本比较新,另外还需要安装 node.js

    Linux / macOS  安装命令:

    
    git clone https://github.com/github/copilot.vim.git \
      ~/.config/nvim/pack/github/start/copilot.vim
      

    下载完毕后,打开 nvim 执行 :Copilot setup 

    第一次使用需要 Github 授权,

    插件会自动生成 一个 6 位字符认证码,

    在浏览器中打开插件提示的 URL,

    在这个页面填写上面的代码,

    通过授权后即可使用。

    相关命令:

    :Copilot enable //激活Copilot

    :Copilot disable //关闭Copilot

    :Copilot status //查看Copilot 状态

    :help Copilot   // Copilot 帮助文档

    问题1:

    <Tab> map has been disabled or is claimed by another plugin

    解决办法:

    因为使用了 packer 作为 neovim 的插件管理,

    在文件 ~/.config/nvim/lua/user/options.lua 中添加下面配置即可。

    vim.g.copilot_assume_mapped = true

    问题相关链接:

    [https://www.reddit.com/r/neovim/comments/sk70rk/using_github_copilot_in_neovim_tab_map_has_been/]

    问题2:

    当选择 Copilot 给出的提示代码时按键是Tab

    自动完成(cmp.lua) 选择也是Tab

    解决此冲突,可以设置选择 Copilot 建议的按键为:Ctrl + J

    然后在 cmp.lua 的配置中禁用 <C+j> 按键映射

    在 init.lua 中添加下面的配置:

    vim.g.copilot_no_tab_map = true
    
    vim.api.nvim_set_keymap("i", "<C-J>", 'copilot#Accept("<CR>")', { silent = true, expr = true })

    参考链接:

    https://www.reddit.com/r/neovim/comments/sk70rk/using_github_copilot_in_neovim_tab_map_has_been/

  • Linode UBUNTU 22.04.1 部署 Laravel Framework 9.42.0 Breeze

    操作步骤视频:

    https://www.bilibili.com/video/BV1Ev4y1d7S8/?vd_source=d997f54cc2fcf65d9d414ae1fc2125c6

    查看操作系统版本
    cat /etc/os-release

    查看内存信息
    free -m

    建议最少2GB

    更新软件包
    apt update

    安装PHP基本扩展
    php -v
    apt install php8.1-cli
    apt install php8.1-curl
    apt install php8.1-redis
    apt install php8.1-mysql
    apt install php8.1-dom

    安装依赖应用
    apt install curl
    apt install redis
    apt install mysql-server
    apt install composer

    基于 Laravel 创建项目 app 的初始代码
    composer create-project laravel/laravel app
    cd ~/app

    启动应用web服务
    php artisan serve –host 172.105.194.149 –port 80

    访问 http://172.105.194.149/

    项目脚手架搭建完毕。

    安装数据库管理工具 phpMyAdmin

    切换到网站服务根目录
    cd ~/app/publilc

    下载源码
    wget https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.zip

    解压
    unzip -qq phpMyAdmin-5.2.0-all-languages.zip

    更改目录名
    mv phpMyAdmin-5.2.0-all-languages pma

    cd pma

    修改配置文件
    cp config.sample.inc.php config.inc.php
    vim config.inc.php
    设置密码加密密钥 参数 blowfish_secret

    使用32位长度随机字串填充,
    将 First Server 下面的 AllowNoPassword 设置为 true,
    意思是允许空密码登录数据库
    使用命令 :wq 保存修改并退出 vim
    cd ~/app
    启动服务
    php artisan serve –host 172.105.194.149 –port 80
    访问 phpMyAdmin
    http://172.105.194.149/pma/

    添加 MySQL 新用户 feng
    自动生成密码 **************
    创建数据库 app

    修改 Laravel app MySQL 数据库配置
    cd ~/app
    vim .env

     

    部署 Laravel Breeze

    Laravel 框架初始化完成,

    如果应用需要鉴权功能,

    即基本的用户注册和登录等,

    可以使用 Laravel Breeze 扩展。

    以下是扩展安装的步骤:

    cd ~/app
    composer require laravel/breeze –dev
    php artisan breeze:install
    php artisan migrate

     

    还需要编译前端资源并启动服务,

    安装npm
    apt install npm

    安装 n
    npm install -g n

    使用 n 安装最新的nodejs
    n latest

    清除缓存

    hash -r

    查看 node 版本
    node -v

    npm install

    启动前端服务

    npm run dev — –host 172.105.194.149

    如果是生产环境

    npm run build

    Laravel Breeze 部署完成。

     

     

  • Linux系统下使用 less +F 替代 tail -f 监视文件

    在Linux系统下通常使用 tail -f 监视文件的变化,
    如果需要具体分析某些文件内容,
    此时需要按下 Ctrl + c 退出后,再去编辑这个文件。
    如果使用 less +F 命令,
    当需要回顾文件时,按下 Ctrl + c ,然后使用 j k 键滚动查看,
    此时文件如果有写入,但视图暂时不会更新,
    需要继续监视文件时,按下 shift + f 【大写F】,则恢复监视状态。
    总结起来,如果监视的文件持续写入内容很多,并且有回顾需求时,
    可以考虑使用 less +F 替代 tail -f

     参考文章:

    https://www.linux.com/training-tutorials/tail-f-vs-less-f/

    https://github.com/gwsw/less