ssh命令进行远程连接

想要链接的远程linux主机需要安装ssh服务(SSH 为建立在应用层基础上的安全协议。SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议)。

1
2
3
ssh 用户名@ip地址

// 例子:ssh root@192.168.1.1

之后输入登陆密码就可以了。

SSH登陆失败

1. Host key verification failed

1
2
3
4
5
6
7
8
9
10
11
12
13
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:9Vh/HwSVrme7tqZgQZ82G0SyYPhzMMqT7iaczHw3Nq8.
Please contact your system administrator.
Add correct host key in C:\\Users\\Umino/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in C:\\Users\\Umino/.ssh/known_hosts:2
ECDSA host key for 47.105.81.6 has changed and you have requested strict checking.
Host key verification failed.

系统每次成功进行SSH连接远程操作,都会把访问过的计算机的public key记录在 .ssh文件夹 的 known_hosts 中。下次再远程访问服务器的时候,会核对公钥,如果公钥不同,会发出警告,并且不能连接上去,避免受到攻击。

  • 解决方法1:在 known_hosts 中,找到对应的服务器ip的 ssh-rsa 删除该行,退出保存known_hosts,重新执行主机ssh连接子机,通过操作。
  • 解决方法2:重新获取公钥
    1
    ssh-keygen -R 要访问的ip
  • 解决方法3:建立主机和子机的相互信任机制
    1. A机 .ssh/ 目录下
      1
      ssh-keygen -t rsa
      1
      cd /root/.shh/id_rsa.pub
      把A公钥id_rsa.pub生成key copy到B服务器根目录root/.ssh/下

      2. Corrupted MAC on input.ssh_dispatch_run_fatal: Connection to 192.168.31.51 port 22: message authentication code incorrect

  • 解决方法1:
    1
    2
    3
    4
    5
    # 1. 执行命令
    ssh -Q mac

    # 选择其中一种方法进行连接,如
    ssh -m hmac-sha2-512 user@ip

1. deployer发布

  1. 将项目上传到git
  2. 项目内安装hexo deployernpm install hexo-deployer-git --save
  3. 编辑_config.yml,新增deploy,并且更改url地址
    1
    2
    3
    4
    5
    6
    7
    8
    9
    # URL
    ## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
    url: https://umino-natsusou.github.io/<项目名>

    deploy:
    type: git
    repo: https://github.com/<username>/<project>
    # example, https://github.com/hexojs/hexojs.github.io
    branch: gh-pages
  4. 输入 hexo d 发布

2. 公共仓库和私有仓库协同发布

  1. hexo源码上传到私有仓库,令建一个公共仓库用来发布page
  2. 用github工作流来简化发布流程:.github/workflows目录下新建自定义yml后缀的文件,写上如下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    name: Deploy Site
    on:
    push:
    branches: [ main ]

    jobs:
    build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
    uses: actions/checkout@v3
    with:
    persist-credentials: true # false 是用 personal token,true 是使用 GitHub token
    fetch-depth: 0

    # ... TODO Build your site

    # <work-dir> 静态资源所在的目录
    # <domain> 需要 github.io 绑定的域名
    # <username> git commit 用户名
    # <mail> git commit 邮箱
    # <message> git commit 的 message
    # <secrets_name> Secrets 名称
    # <github_username> github 的用户名(公开仓库)
    # <repo> 公开仓库名
    # 添加域名的在run下添加一行: echo "<domain>" > CNAME
    # secrets是github的全域变量,test是在源码仓库Settings的Secrets里定义的,填上Settings -> Developer settings里的token
    - name: Commit and Push
    working-directory: public
    run: |
    git init
    git checkout -b gh-pages
    git add -A
    git -c user.name='umino-natsusou' -c user.email='uminonatsusou@gmail.com' commit -m 'deploy site'
    git remote add origin https://${{secrets.test}}@github.com/umino-natsusou/AncientreesDeploy.git
    git push origin gh-pages -f -q

一. 创建分类

  1. 命令行执行,成功后source下生成categories目录。
1
hexo new page categories
  1. 打开source/categories/index.md,添加type: "categories" layout: "categories"
    到其中。
1
2
3
4
5
6
---
title: 文章分类
date: 2022-01-07 22:53:43
type: "categories"
layout: "categories"
---
  1. 给文章添加“categories”属性:
1
2
3
4
5
6
7
---
title: 文章1
date: 2022-11-07 22:53:43
categories:
- 人工智能
- Python
---

如上,文章1属于分类“人工智能 -子分类 Python”,一篇文章只能属于一个分类,可以往后添加多个子分类。

二. 创建标签

  1. 命令行执行,成功后source下生成categories目录。
1
hexo new page tags
  1. 打开source/tags/index.md,添加type: "categories" layout: "categories"
    到其中。
1
2
3
4
5
6
---
title: 文章分类
date: 2022-01-07 22:53:43
type: "tags"
layout: "tags"
---
  1. 给文章添加“tags”属性:
1
2
3
4
5
6
7
8
9
10
11
---
title: 文章1
date: 2022-11-07 22:53:43
categories:
- 人工智能
- Python
tags:
- jQuery
- 表格
- 表单验证
---

三. 添加到模板

在模板文件(scaffolds中)中添加分类和标签属性,以后新建的日志就会自动添加这两个属性了。

1
2
3
4
5
6
---
title: {{ title }}
date: {{ date }}
categories:
tags:
---

四. 在指定目录下新建文章

在/_post目录(对应模板目录)下新建目录,新建文章时执行 type:hexo new <layout> -p <directory>/<name>

文档地址

  1. 安装Hexo:npm install -g hexo-cli
  2. 新建文件夹项目,在文件夹内执行: hexo init
  3. 然后:npm install
  4. 启动:hexo g 编译,然后hexo s启动

一. 新建与删除

  1. 新建:hexo new [layout] <title>
  2. 在指定目录下新建:在/_post目录(对应模板目录)下新建目录,然后执行 hexo new <layout> -p <directory>/<name>,如hexo new post -p test/test 。或者对应文件夹内直接新建
  3. 删除:直接删除对应文件就可以,然后重新编译:
    1
    2
    3
    4
    5
    6
    7
    hexo clean

    hexo g

    hexo s

    hexo d

二. 更换主题

ButterfyNext

  1. 自定义样式:suorce文件夹下新建_data文件夹,在文件夹中新建各种样式文件,然后修改配置文件,添加样式文件的路径即可。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # 自定义样式文件路径
    custom_file_path:
    #head: source/_data/head.njk
    #header: source/_data/header.njk
    #sidebar: source/_data/sidebar.njk
    #postMeta: source/_data/post-meta.njk
    #postBodyEnd: source/_data/post-body-end.njk
    footer: source/_data/footer.njk
    #bodyEnd: source/_data/body-end.njk
    #variable: source/_data/variables.styl
    #mixin: source/_data/mixins.styl
    style: source/_data/styles.styl
  2. 删除网页底部信息栏:_data文件夹中新建styles.styl,添加以下样式
    1
    2
    3
    .powered-by {
    display: none
    }
  3. 增加文章阴影:styles.styl中添加以下添加样式
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    if (hexo-config('motion.transition.post_block')) {
    .post-block {
    visibility: hidden;
    margin-top: 60px;
    margin-bottom: 60px;
    padding: 25px;
    border-radius: 20px 20px 20px 20px;
    -webkit-box-shadow: 0 0 5px rgba(202, 203, 203, .5);
    -moz-box-shadow: 0 0 5px rgba(202, 203, 204, .5);
    }
    .pagination, .comments {
    visibility: hidden;
    }
    }
  4. 开启本地搜索功能。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    // 安装插件
    npm install hexo-generator-searchdb --save

    // 修改 hexo/_config.yml 站点配置文件,新增以下内容
    search:
    path: search.xml
    field: post
    format: html
    limit: 10000

    // 修改主题配置文件,启用搜索
    local_search:
    enable: true
  5. 网站运行时间设置:_data文件夹下新footer.njk文件。添加以下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <!-- 网站运行时间的设置 -->
    <span id="timeDate">载入天数...</span>
    <!-- <span id="times">载入时分秒...</span> -->
    <script>
    var now = new Date();
    function createtime() {
    var grt= new Date("11/17/2022 8:00:00");//此处修改你的建站时间或者网站上线时间
    now.setTime(now.getTime()+250);
    days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
    hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
    if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
    seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds);
    if(String(snum).length ==1 ){snum = "0" + snum;}
    // var times = document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
    document.getElementById("timeDate").innerHTML = "本站已安全运行 "+dnum+" 天 "+hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
    }
    setInterval("createtime()",250);
    </script>
  6. 添加live-2d。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    // 1. 安装插件
    npm install -save hexo-helper-live2d

    // 2. 安装模型
    npm install live2d-widget-model-wanko --save

    // 可选择其他模型
    # live2d-widget-model-chitose

    # live2d-widget-model-epsilon2_1

    # live2d-widget-model-gf

    # live2d-widget-model-haru/01 (use npm install --save live2d-widget-model-haru)

    # live2d-widget-model-haru/02 (use npm install --save live2d-widget-model-haru)

    # live2d-widget-model-haruto

    # live2d-widget-model-hibiki

    # live2d-widget-model-hijiki

    # live2d-widget-model-izumi

    # live2d-widget-model-koharu

    # live2d-widget-model-miku

    # live2d-widget-model-ni-j

    # live2d-widget-model-nico

    # live2d-widget-model-nietzsche

    # live2d-widget-model-nipsilon

    # live2d-widget-model-nito

    # live2d-widget-model-shizuku

    # live2d-widget-model-tororo

    # live2d-widget-model-tsumiki

    # live2d-widget-model-unitychan

    # live2d-widget-model-wanko

    # live2d-widget-model-z16

    // 3. 站点配置文件_config.yml添加以下代码
    live2d:
    enable: true
    scriptFrom: local
    pluginRootPath: live2dw/
    pluginJsPath: lib/
    pluginModelPath: assets/
    tagMode: false
    log: false
    model:
    use: live2d-widget-model-<模型名字>
    display:
    position: right
    # width: 150 # 大小根据模型结构自己调整合适的
    # height: 300
    mobile:
    show: false # 是否在手机端显示
  7. 背景图片设:将背景图放到source/images下,然后styles.styl中添加以下样式
    1
    2
    3
    4
    5
    6
    7
    body {
    background-image: url(../images/background.png);
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: 100% 100%;
    opacity: 0.8;
    }

LNMP部署Piwigo地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 1. 前置工具安装
sudo dnf install wget unzip

# 2. 进入要部署项目的目录下,下载piwigo并解压
# 下载
sudo wget https://piwigo.org/download/dlcounter.php?code=latest -O piwigo.zip
# 解压
sudo unzip piwigo.zip

# 3. 进入piwigo根目录,设置权限
# 将所有者和组更改为Web服务器用户和组
sudo chown -R nginx:nginx .
# 确保Web服务器和PHP进程具有读、写和执行权限
sudo chmod -R 777 .

# 4. 新建piwigo的nginx配置文件(自定义名称)
# 新建
sudo vim /etc/nginx/conf.d/piwigo.conf
# 写上配置
server {
listen 8888; # 端口号
server_name example.com; # 域名
root /home/piwigo; # 项目地址
index index.php index.html index.htm; # 入口文件

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 如果用caddy,则配置如下
example.com:8888 {
root * /home/piwigo
php_fastcgi unix//run/php-fpm/www.sock {
index index.php
}
file_server
}

# 5. 登入数据库,新建一个数据库,新建数据库用户、并赋予其权限
mysql -u 用户名 -p
# 新建数据库
CREATE DATABASE piwigo;
# 新建piwigo用户
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
# 赋予其权限
GRANT ALL PRIVILEGES ON piwigo.* TO 'newuser'@'localhost';
# 刷新权限表
flush privileges;

# 6. 重启所有相关服务,在浏览器中输入对应地址,进入安装页。
sudo systemctl restart nginx
sudo systemctl restart mariadb
sudo systemctl restart php-fpm

1. 安装Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 1. 更新软件列表
sudo dnf update

# 2. 安装nginx
# 安装
sudo dnf install nginx
# 查看是否安装成功
nginx -v
# 查看配置文件是否有错误
sudo nginx -t

# 3. 启动nginx服务
# 启动
sudo systemctl start nginx
# 添加到开机自启动
sudo systemctl enable nginx

# 4. 启动后输入服务器地址(ip addr)可查看到启动成功的网页返回

# 附:反向代理设置 /etc/nginx/conf.d/app2.example.com.conf
server {
listen 80;
server_name app.example.com;

location / {
proxy_pass http://localhost:4000; # 将流量发送到应用程序所在的端口
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
# 附:HTTPS&HTTP&webSocket重定向
# HTTPS
server {
listen 443 ssl;
server_name app.example.com; # 域名
ssl_certificate /etc/nginx/certs/app.example.com.pem; # ssl路径
ssl_certificate_key /etc/nginx/certs/app.example.com.key; # ssl路径
# 其他ssl设置
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
# access_log /etc/nginx/conf.d/logs/example.com.log combined;

location / {
proxy_pass http://localhost:8088; # 代理的服务端口
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

# webSocket
server {
listen 443 ssl;
server_name paint.roaring.win; # 域名
ssl_certificate /etc/nginx/certs/app.example.com.pem; # ssl路径
ssl_certificate_key /etc/nginx/certs/app.example.com.key; # ssl路径
# 其他ssl设置
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
# access_log /etc/nginx/conf.d/logs/example.com.log combined;

location / {
proxy_pass http://localhost:8003; # 代理的服务端口
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

# for websocket
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}

# HTTP转HTTPS
server {
listen 80;
server_name paint.roaring.win; # HTTP重定向到HTTPS
return 301 https://$server_name$request_uri;
}

可以修改nginx默认监听端口(将80端口改为其他)

1
2
3
4
5
6
7
8
9
# vim /etc/nginx/nginx.conf

server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
……
}

nginx常用命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 启动、停止、重启服务
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

# 查看nginx状态
sudo systemctl status nginx

# 查看服务端口
sudo netstat -tlnp | grep nginx

# 开启/关闭 开机自启动
sudo systemctl enable nginx
sudo systemctl disable nginx

安装SSL证书

  1. acme.sh 自建证书
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 1. nginx服务开启后,运行以下命令智能生成证书(以webgpt.ancientrees.com为例)。生成的证书在/root/.acme.sh/。
acme.sh --issue -d webgpt.ancientrees.com --nginx

# 2. 复制移动证书到nginx下,集中管理(没有certs、webgpt.ancientrees.com目录则先新建)
acme.sh --install-cert -d webgpt.ancientrees.com \
--key-file /etc/nginx/certs/webgpt.ancientrees.com/key.pem \
--fullchain-file /etc/nginx/certs/webgpt.ancientrees.com/cert.pem \
--reloadcmd "service nginx force-reload"

# 3. 打开配置服务器文件
vim /etc/nginx/conf.d/webgpt.ancientrees.com.conf

# 4. 配置
server {
listen 443 ssl;
server_name webgpt.ancientrees.com;
ssl_certificate /etc/nginx/certs/webgpt.ancientrees.com/cert.pem;
ssl_certificate_key /etc/nginx/certs/webgpt.ancientrees.com/key.pem;
location / {
proxy_pass http://localhost:1002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
listen 80;
server_name webgpt.ancientrees.com; # HTTP重定向到HTTPS
return 301 https://$server_name$request_uri;
}

# 5. 重启服务
sudo systemctl restart nginx

2. 安装MariaDB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 1. 更新源(如已做则不用)
sudo dnf update

# 2. 清空MySQl,以免冲突无法安装
sudo dnf remove mysql mysql-server mysql-libs
# 禁止mysql的源
sudo dnf config-manager --disable mysql57-community

# 3. 安装 MariaDB
# 安装
sudo dnf install mariadb-server
# 确认安装成功
mariaDB -V

# 4. 启动服务&加入开机启动
# 启动服务
sudo systemctl start mariadb
# 加入开机启动
sudo systemctl enable mariadb

# 5. 进行安全设置
sudo mysql_secure_installation

mariaDB常用命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 查看可用的mariaDB版本
sudo dnf search mariadb

# 查看所有依赖项
sudo rpm -qa | grep mariadb

# 启动、关闭、重启服务
sudo systemctl start mariadb
sudo systemctl stop mariadb
sudo systemctl restart mariadb

# 查看状态
sudo systemctl status mariadb
# 查看服务所在端口
sudo netstat -tulnp | grep mariadb

# 加入/关闭 开机启动
sudo systemctl enable mariadb
sudo systemctl disable mariadb

# 登录数据库(首次登入用root用户)
mysql -u 用户名 -p

# 新建数据库用户
CREATE USER '用户名'@'localhost' IDENTIFIED BY '密码';

# 赋予数据库用户权限(希望用户能够从任何主机访问MariaDB,请将 localhost 替换为 % )
# 1. 赋予用户对所有数据库的所有权限
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';
# 2. 赋予用户对某个数据库的所有权限
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
# 3. 赋予用户对某个数据库的限定权限
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON database_name.* TO 'username'@'localhost';
# 4. 刷新权限列表使生效
FLUSH PRIVILEGES;

# 查看用户详细信息
SELECT User, Host, authentication_string FROM mysql.user;
# 查看数据库列表
SHOW DATABASES;

# 新建数据库
CREATE DATABASE database_name;

# 退出数据库
Ctrl + C

3. 安装PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 1. 安装
sudo dnf install php

# 2. 安装sql模块
sudo dnf install php-mysqli

# 3. 安装GD模块
# 安装前置
# libpng:提供PNG图像格式的支持
# jpegsrc:提供JPEG图像格式的支持
# freetype:提供TrueType字体的支持
sudo dnf install libpng-devel
sudo dnf install libjpeg-turbo-devel
sudo dnf install freetype-devel
# 安装GD库
sudo dnf install php-gd

# 4. 启动fpm服务&添加到开机启动
sudo systemctl start php-fpm
sudo systemctl enable php-fpm

# 5. 创建一个nginx配置文件来定义一个虚拟主机(一般一个配置文件对应一个服务)
vim /etc/nginx/conf.d/example.com.conf

# 6. 编辑ngin配置文件(example.com.conf),写上以下
server {
listen 81; # 端口号
server_name example.com; # 域名
root /var/www/html; # 项目地址
index index.php index.html index.htm; # 入口文件

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

# 7. 配置文件定义的项目地址处,新建一个测试文件
# 新建
vim /var/www/html/index.php
# 写上
<?php
echo phpinfo();
?>

# 8. 最后,重启nginx服务(sudo systemctl restart nginx)。便可在设置好的地址(ip或者域名)查看网站

php常用命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 查看可用的php版本
dnf module list php

# 查看版本
php -v

# 查看所有依赖
rpm -qa | grep php
# 查看已安装的模块
php -m
# 查看状态
systemctl status php-fpm

# 启动、停止重启
sudo systemctl start php-fpm
sudo systemctl stop php-fpm
sudo systemctl restart php-fpm

# 设置、关闭开机后自动启动
sudo systemctl enable php-fpm
sudo systemctl disable php-fpm

# 安装完新的PHP模块后,要重启服务才能生效
sudo systemctl restart nginx
sudo systemctl restart php-fpm

系统初始化

Debian系用apt-get(apt),Red Hat系用yum、dnf

  1. wget:sudo dnf -y install wget
  2. net-tools:sudo dnf -y install net-tools
  3. tar:sudo dnf -y install tar
    1
    2
    3
    4
    5
    # 解压tar包
    tar -xvf xxx.tar

    # 解压tar.gz
    tar -xzvf xxx.tar.gz
  4. unzip:sudo dnf -y install unzip
  5. git:sudo dnf -y install git
  6. tree:sudo dnf -y install tree(键入 tree 即可查看当前目录下的文件树)
  7. vim:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # 1. 查看已存在的包
    rpm -qa|grep vim

    # 2. 如果只显示vim-minimal-8.0.1763-15.el8.x86_64,则重新安装
    sudo dnf -y install vim*

    # 3. 配置vim
    sudo vim /etc/vimrc

    # 4. 按i进入编辑模式,添加以下(已存在的配置可删除)
    set nu " 设置显示行号
    set showmode " 设置再命令行最下面显示当前模式
    set autoindent " 设置enter键后,光标移动到下一行时。以上一行起始字符对齐
    syntax on " 语法检测,当编辑C或者shell时,关键字会用特殊颜色显示

    # 5. 按ESC退出编辑模式,输入:wq保存并且退出
  8. nodeJS
  9. conda
  10. nginx
  11. ssh证书工具。acme.sh,弃用,用cloudfare的提供的证书更好。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    # 1. 安装acme.sh
    curl https://get.acme.sh | sh -s email=username@example.com

    # 2. 重载
    source ~/.bashrc

    # 3. 开启自动更新
    acme.sh --upgrade --auto-upgrade

    # 4. 选择默认的CA(letsencrypt是免费的)
    acme.sh --set-default-ca --server letsencrypt

    # 5. 把对应域名的服务开启,智能生成证书(不需要指定任何根目录)
    ## nginx
    acme.sh --issue -d mydomain.com --nginx
    ## apache
    acme.sh --issue -d mydomain.com --apache

    # 6. 生成的证书在/root/.acme.sh/中,用install命令将其复制移动到指定位置,进行使用
    ## apache示例(没有相关目录则新建)
    acme.sh --install-cert -d domain.com \
    --cert-file /path/to/certfile/in/apache/cert.pem \
    --key-file /path/to/keyfile/in/apache/key.pem \
    --fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \
    --reloadcmd "service apache2 force-reload"
    ## nginx示例(没有相关目录则新建)
    acme.sh --install-cert -d domain.com \
    --key-file /etc/nginx/certs/domain.com/key.pem \
    --fullchain-file /etc/nginx/certs/domain.com/cert.pem \
    --reloadcmd "service nginx force-reload"

    # 7. 于服务器设置处配置证书路径,开启SSL(nginx)
    server {
    listen 443 ssl;
    server_name domain.com;
    ssl_certificate /etc/nginx/certs/domain.com/cert.pem;
    ssl_certificate_key /etc/nginx/certs/domain.com/key.pem;
    location / {
    proxy_pass http://localhost:1002; # 代理的服务端口
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    }
    }
    server {
    listen 80;
    server_name domain.com; # HTTP重定向到HTTPS
    return 301 https://$server_name$request_uri;
    }

    # 8. 重启服务以生效
    sudo systemctl restart nginx

1. 常用命令行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 停机、重启或切断电源
shutdown、halt、poweroff、reboot

# 退出ssh连接
Ctrl + d

# 查看IP
ip addr
ifconfig

# 重设用户密码
sudo passwd root
# 切换用户(如下切换到root用户)
su - root

# 新建文件夹
mkdir 文件夹名称
# 删除文件夹
rm -rf 文件夹名
# 删除文件
rm -f 文件名

# 复制文件
cp source_file destination_file
# 复制文件夹
cp -r source_folder destination_folder

# vi/vim 新建/打开文件
vi/vim 文件名

# 升级源
dnf update

# 查看已开启服务端口
netstat -tnl

# 查看已开放远程访问的端口
firewall-cmd --list-ports
# 查看某个端口状态
firewall-cmd --zone=public --query-port=80/tcp
# 开放某个端口访问(重启防火墙后生效)
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 关闭某个端口访问
firewall-cmd --zone=public --remove-port=80/tcp --permanent

# 查看防火墙状态
firewall-cmd --state
# 开启防火墙
systemctl start firewalld.service
# 关闭防火墙
systemctl stop firewalld.service
# 重启防火墙
firewall-cmd --reload

2. 域名设置

1. 二级域名设置

按以下步骤添加二级域名:

  1. 于DNS管理处,新增A记录
  2. Name填上二级域名的前缀,如xxx.domain.com的话,就填上xxx
  3. value填上服务器ip地址,保存即可

3. 知识概念

  • apt-get 命令是Debian和Ubuntu系统上的包管理器,用于在系统上安装、升级和删除软件包。它可以从系统的软件包存储库中自动下载和安装软件包,并解决它们的依赖关系。
  • yumdnf 命令是Red Hat和CentOS系统上的包管理器,用于安装、升级和删除软件包。它可以从系统的软件包存储库中自动下载和安装软件包,并解决它们的依赖关系。
  • wget 命令是一个Linux/Unix操作系统下的命令行工具,用于从互联网上下载文件。可以通过URL地址下载文件,也可以通过FTP和HTTP等协议下载文件。

1. nvm&nodejs(Windows)

nvm是node.js的版本管理工具。

node.js的版本是非常多的,不同的项目测试开发有时候会用到不同的版本,这时候需要切换不同的node.js版本。通过nvm来安装node.js就可以实现nvm对node.js版本的管理。

常用的命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 查看nvm版本
nvm version

# 查看已安装的node版本
nvm list

# 查看所有可用nodejs版本
nvm list available

# 安装最新版本的nodejs。如果出现错误,则前往官网下载最新的nvm版本,覆盖更新。
nvm install latest

# 安装指定版本
nvm install 版本号

# 查看已安装的nodejs版本
nvm ls

# 使用用指定版本号的nodejs
nvm use 版本号

# 删除指定版本号的nodejs
nvm uninstall 版本号

2. nvm&nodejs(Linux)

git安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 1. 
cd ~/
# 2. 下载项目
git clone https://github.com/nvm-sh/nvm.git .nvm
# 3.
cd ~/.nvm
# 4. check out the latest version(can see in github)
git checkout v0.39.7
# 5. activate
. ./nvm.sh

# 6. 设置配置。打开配置文件bashrc,看配置是否存在
vim ~/.bashrc

# 7. i进入编辑模式,写入配置,主要配置NVM_DIR。如果已自动配置,则输入:q退出
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

# 8. 使用配置
source ~/.bashrc

# 9. 判断nvm是否已经安装成功
nvm -v

# 6. 安装最新版本node
nvm install node

# 7. 使用最新版本的node
nvm use node

# 8. 安装新的node版本并且将全局npm包迁移
nvm install node --reinstall-packages-from=node

3. npm

常用npm命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 查看npm信息
npm version

# 全局安装最新的npm版本
npm install npm -g

# 安装生产依赖
npm install <package-name> --save
# 安装开发依赖
npm install <package-name> --save-dev

# 1. 初始化一个项目,生成一个package.json文件
npm init
# 2. 初始化一个项目,并且跳过问卷
npm init -y
# 3. 安装项目依赖
npm install
# 4. 卸载项目依赖,可指定某个包或某个版本
npm uninstall
# 5. 更新项目依赖,可指定某个包或某个版本
npm update
# 6. 查看当前项目依赖
npm list

# 查看全局安装的包
npm list -g**

# 设置镜像源
npm config set registry=http://registry.npmjs.org
# 查看代理
npm config get proxy
# 设置http代理
npm config set proxy=http://127.0.0.1:7891
# 设置https代理
npm config set https-proxy=http://127.0.0.1:7891
# 清除代理或源
npm config delete proxy
npm config delete https-proxy
npm config delete registry

# 运行某生产依赖的程序,如hexo
npx hexo g

4. pm2(主要在linux端应用)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 1. 安装pm2
npm install pm2 -g

# 2. 开机自启动服务
## a. 把要开机自启动的服务开启
## b. 执行以下命令,新建开机自启动服务配置文件
pm2 startup
## c. 保存配置。配置保存于/root/.pm2/dump.pm2。
pm2 save
## d. 查看是否已开启
systemctl status pm2-root.service

# 3. 一些常用的pm2命令
启动script:pm2 start pnpm --watch --name XX -- run script命令
启动:pm2 start demo.js //demo.js是你要启动的app_name|app_id文件
停止:pm2 stop app_name|app_id
删除:pm2 delete app_name|app_id
重启:pm2 restart app_name|app_id
监控:pm2 monit
查看所有:pm2 list
查看某个:pm2 show 0
停止所有:pm2 stop all
停止某个:pm2 stop 0
重载所有:pm2 reload all
重载某个:pm2 reload 0
重启所有:PM2 restart all
删除某个:pm2 delete 0
安装最新:npm install pm2@lastest -g
升级:pm2 updatePM2
帮助:pm2 --help
停止所有:pm2 stop all
查看所有的进程:pm2 list
查看所有的进程状态:pm2 status
查看某一个进程的信息:pm2 describe app_name|app_id
参数说明
--watch:监听应用目录源码的变化,一旦发生变化,自动重启。如果要精确监听、不见听的目录,最好通过配置文件
-i --instances:启用多少个实例,可用于负载均衡。如果-i 0或者-i max,则根据当前机器核数确定实例数目,可以弥补node.js缺陷
--ignore-watch:排除监听的目录/文件,可以是特定的文件名,也可以是正则。比如--ignore-watch="test node_modules "some scripts"
-n --name:应用的名称,查看应用信息的时候可以用到
-o --output <path>:标准输出日志文件的路径,有默认路径
-e --error <path>:错误输出日志文件的路径,有默认路径
--interpreter <interpreter>:the interpreter pm2 should use for executing app (bash, python...)
如完整参数命令:
pm2 start demo.js --watch -i 2 //开启2个进程
pm2 start app.js -i max //根据机器CPU核数,开启对应数目的进程

1. Windows下安装

首推用conda这种方式开启python,地址。推荐安装miniconda,更加精简和轻便。

下载安装完毕后,配置环境变量:

  1. C:\_Code\miniconda3 —— (Python)
  2. C:\_Code\miniconda3\Scripts —— (Conda Scripts)
  3. C:\_Code\miniconda3\Library\bin —— (Conda Library Application)

配置完成后,cmd输入python检测。(windows下会出现输入python后打开应用商店的情况,这时候进入“应用执行别名”,把python相关的“应用安装程序”关掉即可)。一切安装完毕好,执行 conda init 命令,重新打开终端。

配置文件(配置了VPN则可忽略)

配置文件为~.condarc,打开配置文件,进行编辑。因为是国内,所以需要配置镜像。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
show_channel_urls: true
ssl_verify: false

2. Linux下安装

1
2
3
4
5
6
7
8
9
10
11
# 1. 安装脚本
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh

# 2. 安装
sh Miniconda3-latest-Linux-x86_64.sh

# 3. 使用配置
source ~/.bashrc

# 4. 检查是否安装成功
conda -V

使用

1. 环境管理命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 查看版本
conda --version

# 查看详情
conda info

# 升级
conda update conda
# 有时候不起作用,则使用强制更新
conda update --force conda

# 查看所有环境
conda env list
conda info -e

# 创建环境
conda create -n <env_name>
# 创建指定版本的环境
conda create -n [env_name] python=3.9.12
# 复制环境/重命名环境(通过复制来完成)
conda create -n <new_env_name> --clone <old_env_name>
# 删除环境
conda remove -n <env_name> --all

# 用当前目录下的配置文件创建新的运行环境
conda env create -f environment.yaml

# 启用环境(启用环境、关闭环境推荐用conda命令)
conda activate <env_name>
# 关闭环境
conda deactivate

# 打包环境
conda env export > <env_name>.yaml
# 打包环境(用pip)
pip freeze > requirements.txt

# 查看已添加的channels
conda config --get channels
# 添加清华镜像(安装一次,镜像也只配置一次)
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
conda config --set show_channel_urls yes
# 执行完上述命令后,会生成 ~/.condarc
# 删除镜像源
conda config --remove channels 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free'
# 要删除所有镜像源的话,直接删除~/.condarc即可
2. 包管理命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 查看当前环境下所有的包
conda list
# 查看指定环境下所有的包
conda list -n <env_name>

# 搜索某个包的信息,查看该包可用的版
conda search <package_name>

# 安装指定包
conda install <package_name>
# 安装指定包的指定版本
conda install <package_name>=1.1.0
# 使用指定镜像地址安装包
conda install -c <镜像地址> <package_name>

# 更新当前环境下所有的包
conda update --all
# 更新指定包
conda update <package_name>

# 删除包
conda remove <package_name>

3. 环境管理工具

1. PIP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 1. 输出环境中已经安装的所有的包及其精确版本号
pip freeze > requirements.txt
# 2. 通过读取这个文件并安装指定版本的依赖包来安装项目
pip install -r requirements.txt

# 查看pip版本
pip --version

# 更新pip
pip install --upgrade pip
# 如果更新失败,可以尝试
# 1. pip依赖于setuptools进行安装。如果setuptools版本过低,可能也会导致pip升级失败
pip install --upgrade setuptools
# 2. 卸载旧版本pip并重装
pip uninstall pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

2. VENV

使用虚拟环境,不同项目和环境之间的依赖关系不会相互干扰。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 当前目录下创建名为venv的虚拟环境(第二个venv为虚拟环境名称,可以变更)
python -m venv venv

# windows下激活环境
venv\Scripts\activate

# linux下激活环境
source env/bin/activate

# 退出虚拟环境
deactivate

# 删除虚拟环境
删除venv文件夹即可

3. PIPENV

非常好用的包管理工具。结合了pip和virtualenv的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 安装
pip install pipenv


# 项目初始化
# 创建虚拟环境会默认查找该目录下有没有.venv目录,如果没有的话,会在用户目录下创建虚拟环境
# 推荐先在项目下创建一个.venv文件夹,然后再创建虚拟环境
mkdir .venv
pipenv install


# 1. 安装依赖包
pipenv install <package_name>
# 2. 卸载依赖包
# 这将从Pipfile和Pipfile.lock中移除指定的依赖库
pipenv uninstall <package_name>
# 3. 卸载所有包
pipenv uninstall [module]
# 4. 更新依赖包
pipenv update
# 5. 生成依赖关系快照。
# 生成 Pipfile.lock 文件,记录当前所有依赖库的版本信息,便于在其他环境中重现。
pipenv lock
# 6. 查看包依赖
pipenv graph

# 1. 激活虚拟环境
pipenv shell
# 2. 退出虚拟环境
exit

# 1. 列出本地工程路径
pipenv --where
# 2. 列出虚拟环境路径
pipenv --venv
# 3. 列出虚拟环境的 Python 可执行文件
pipenv --py

# 查看项目的虚拟环境路径
pipenv --venv

4. m2-base

安装m2-base可以使得在当前python环境下能够使用linux的bash命令:
conda install m2-base