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
# 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;
}
}

# 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:dnf -y install wget
  2. net-tools:dnf -y install net-tools
  3. tar:dnf -y install tar
    1
    2
    3
    4
    5
    # 解压tar包
    tar -xvf xxx.tar

    # 解压tar.gz
    tar -xzvf xxx.tar.gz
  4. unzip:dnf -y install unzip
  5. git:dnf -y install git
  6. tree: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,则重新安装
    dnf -y install vim*

    # 3. 配置vim
    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
54
55
56
# 停机、重启或切断电源
shutdown、halt、poweroff、reboot

# 退出ssh连接
Ctrl + d

# 查看IP
ip addr
ifconfig

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

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

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

# 删除文件夹
rm -rf 文件夹名
# 删除文件
rm -f 文件名

# 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等协议下载文件。

介绍nvm

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

node.js的版本是非常多的,不同的项目测试开发有时候会用到不同的版本,这时候需要切换不同的node.js版本。

通过nvm来安装node.js就可以实现nvm对node.js版本的管理。

一. 安装nvm&nodejs(Windows)

下载地址及教程。解说noinstall版本的方式。

  1. 解压后目录 nvm.png
  2. 双击install.cmd,会进入命令行,Enter确认,会在所在盘根目录看到生成文件settings.txt(没有生成的话就自己新建),将文件paste到nvm目录下,打开文件,将内容修改为以下。
    • root是nvm路径
    • path是你想要的node.js路径
    • 使用淘宝镜像,下载速度更快
  3. 配置环境变量,使得nvm全局可用(如果已存在以下两个环境变量,将其删除)。
    • 新增环境变量 NVM_HOME,值 C:_Code\nvm
    • 新增环境变量 NVM_SYMLINK,值 C:_Code\nodejs
    • 将新增的环境变量添加到Path
  4. 任意位置打开命令行,输入 nvm install latest,会下载最新版本的node.js到指定路径;当然想下载其他版本只要制定版本号就可以了。下载完成后,要输入 nvm use 版本号 以开始使用nodejs
  5. 几个常用的命令
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    # 查看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 版本号

二. 安装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.
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

三. 安装npm(Windows)

每个node.js都是自带npm的,但是该npm只属于对应版本的node.js;这样一来,在切换node.js版本的时候也会造成npm的切换,便会使得npm下的一些全局包无法共享使用,每切换一次node.js版本,都必须确认其npm下的全局包是否完全 —— 这显然是不符合我们的使用习惯的。

综上,我们需要一个独立的npm。

  1. 配置独立的npm安装路径。打开命令行,输入 npm config set prefix “C:_Code\nvm\npm”。用户文件目录下会生成一个.npmrc的文件,打开后可以看到如下内容:prefix=C:_Code\nvm\npm
  2. 下载npm。命令行输入 npm install npm -g。这样独立的npm就下载完成了。
  3. 配置环境变量。新增环境变量 NPM_HOME,值 C:_Code\nvm\npm,添加 %NPM_HOME% 到path。
  4. 一些常用命令
    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
    # 查看npm信息
    npm version

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

    # 1. 初始化一个项目,生成一个package.json文件
    npm init
    # 2. 安装项目依赖
    npm install
    # 3. 卸载项目依赖,可指定某个包或某个版本
    npm uninstall
    # 4. 更新项目依赖,可指定某个包或某个版本
    npm update
    # 5. 查看当前项目依赖
    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

四. 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核数,开启对应数目的进程

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

1. Windows下安装

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

  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

# 用当前目录下的配置文件创建新的运行环境
mamba 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即可
  1. 包管理
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>

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

Windows Terminal

从microStore上安装,或者winget安装winget install --id=Microsoft.WindowsTerminal -e。安装完毕重启后,即添加到右键。

常用命令
1
2
3
4
5
6
7
8
9
10
11
# 设置代理
netsh winhttp set proxy proxy-server="http=127.0.0.1:7891;https=127.0.0.1:7891"
# 清除代理
netsh winhttp reset proxy
# 查看代理
netsh winhttp show proxy

# 显示目录树
tree
# 显示目录树(包含文件)
tree /f

oh-myposh3

微软商店搜索下载(如果下载错误,打开用户账户控制设置,将“始终通知”栏拉到最高。这是微软的一个bug)。然后还必须下载相应的字体,字体官网;可以下载作者推荐的MesloLGM NF字体。下载完成后,解压,全选、点击安装。

1. 配置字体

打开Windows Termina,打开设置json文件,在profiles项下配置字体:

1
2
3
4
5
6
"defaults": {
"font": {
"face": "MesloLGM NF"
}
},
"list": [...]

命令行输入oh-my-posh init pwsh | Invoke-Expression即可进行使用。

2. 配置自动执行脚本

每次打开都需要输入以上命令行显然是很麻烦的,这时候需要配置poweshell执行脚本来使得每次打开powershell、程序会自动进入on-my-posh3。

1
2
3
4
5
6
7
8
# 1. 启动编辑power shell配置文件的引擎
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }

# 2. 打开配置文件
notepad $profile

# 2. 写入配置脚本、保存。以后输入 notepad $profile 便可打开此文件进行脚本配置
oh-my-posh init pwsh | Invoke-Expression

如果新打开powershell窗口提示系统禁止运行脚本的话(计算机上启动 Windows PowerShell 时,执行策略很可能是 Restricted(默认设置)Restricted 执行策略不允许任何脚本运行),以管理员身份打开,命令行输入set-executionpolicy remotesigned即可。

3. 主题

命令Get-PoshThemes可以安装主题,主题所在目录为C:\Users[user name]\AppData\Local\Programs\oh-my-posh\themes。配置主题的话,找到相应的主题名称,在配置脚本中这样写:

1
oh-my-posh init pwsh --config C:\Users\[user name]\AppData\Local\Programs\oh-my-posh\themes\bubbles.omp.json | Invoke-Expression

WSL2

WSL是windows推出的可让开发人员不需要安装虚拟机或者设置双系统启动就可以原生支持运行GNU/Linux的系统环境,简称WSL子系统。WSL2使用全新体系架构使其能真正的运行一个Linux内核。

wsl2必须运行 Windows 10 版本 2004 及更高版本(内部版本 19041 及更高版本)或 Windows 11。可以通过winver命令查看版本。

1. 启用虚拟机平台和Linux子系统功能

控制面板>程序>程序和功能>启用或关闭Windows功能>适用于Linux的Windows子系统、虚拟机平台。打上勾、重启后生效。

2. 在MicroStore安装一Linux发行版(或者Ubantu)

下载完成后便可启动。命令行输入wsl -l --all -v查看已安装的Linux发行版。其形式如下:
wsl2-info
可以看到还是VERSION1,即wsl版本,接下来将其升级到wsl2版本(如果version=2则不用升级)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 1. 确保已在 BIOS 中启用 CPU 虚拟化。在Bios的CPU设置中启用,AMD是SVM,Intel是VT-x。

# 2. 升级,并且之后关闭wsl以重启
wsl --update
wsl --shutdown

# 3. 开启hyper-v,也可以在在windows功能中开启。(家庭版没有hyper-v选项,先用命令行添加hyper-v功能)
bcdedit /set hypervisorlaunchtype auto

# 4. 将wsl设为默认。以后下载安装的Linux默认就是wsl2
wsl --set-default-version 2

# 5. 查看已安装的Linux
wsl -l -v

# 6. 将目标Linux转为wsl2
wsl --set-version <name> 2

# 附:添加hyper-v。新建新建hyperv.cmd文件,写入以下内容,运行
pushd "%~dp0"
dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txt
for /f %%i in ('findstr /i . hyper-v.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"
del hyper-v.txt
Dism /online /enable-feature /featurename:Microsoft-Hyper-V-All /LimitAccess /ALL
3. 启用systemd

以下在linux系统内操作

1
2
3
4
5
6
7
8
9
# 1. etc目录下新建wsl.conf文件
sudo vim /etc/wsl.conf

# 2. 写上以下
[boot]
systemd=true

# 3. 重启wsl2
wsl --shutdown
4. 网络&代理相关

wsl2的IP地址和主机IP一致,即127.0.0.1可以访问。
若出现以下报错:

1
2
wsl: 检测到 localhost 代理配置,但未镜像到 WSL。
NAT 模式下的 WSL 不支持 localhost 代理。

解决:
在Windows中的C:\Users<your_username>目录下创建一个.wslconfig文件,然后在文件中写入如下内容:

1
2
3
4
5
6
[experimental]
autoMemoryReclaim=gradual
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

重启。

  1. 设置代理方法一:安装代理软件
  2. 设置代理方法二:使用windows的代理
    1. windows下的代理软件打开允许局域网连接
    2. Linux下新建代理设置脚本并且应用
      1
      vim ~/.proxyrc
      写入(7890是windows下代理软件设置的端口)
      1
      2
      3
      4
      #!/bin/bash
      host_ip=$(cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " ")
      export http_proxy="http://$host_ip:7890"
      export https_proxy="https://$host_ip:7890"
      应用
      1
      source ~/.proxyrc
5. 常用命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 # 查看已安装的Linux发行版
wsl -l —all -v

# 导出Linuxtar文件到D盘
wsl —export Ubuntu-20.04 d:/wsl-ubuntu-20.04.tar

# 注销当前linux发行版
wsl —unregister Ubuntu-20.04

# 重新导入并安装wsl到D盘
wsl —import Ubuntu-20.04 d:/wsl-ubuntu-20.04 d:/wsl-ubuntu-20.04.tar —version 2

# 设置默认登陆用户名为安装时的用户名
ubuntu2004 cofig —default-user USERNAME

一. 前置

  1. 安装Anaconda
  2. 安装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
34
35
36
37
38
39
40
41
42
43
44
45
46
// 1. 下载项目
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui

// 2. 进入项目内
cd stable-diffusion-webui

// 3. 下载模型。[1.4 AI model](https://huggingface.co/CompVis),这是官方模型。

// 4. 将下载好的模型放置于models\Stable-diffusion文件夹下

// 5. 降低显存负荷(可以跳过),编辑文件webui-user.bat,另COMMANDLINE_ARGS=--medvram(或--lowvram)

// 6. 打开environment-wsl2.yaml。在这里可以配置名称。在最末添加pip镜像(国内需要)。总体如下:
name: CustomSD
channels:
- pytorch
- defaults
dependencies:
- python=3.8.5
- pip=20.3
- cudatoolkit=11.3
- pytorch=1.11.0
- torchvision=0.12.0
- numpy=1.19.2
- pip:
- -i https://pypi.tuna.tsinghua.edu.cn/simple

// 7. 创建SD的python运行环境
conda env create -f environment-wsl2.yaml

// 8. 激活该环境
conda activate CustomSD

// 9. 安装运行,键入命令。这一步耗时较长,大概十几分钟。由于众所周知的原因,或会出现某些库无法安装的问题,这时候需要不断键入以下命令以完全完成安装。有条件的可以设置git代理。
webui-user.bat

// 10. 安装完成后、加载模型后,它应该会给您一个 LAN 地址,例如 ' 127.0.0.1:7860 ',打开这个地址进行探索。

// 11. 更新(记得activate项目python环境)
git pull //拉取最新代码,建议使用sourcetree进行更新
pip install -r requirements.txt //可能会有依赖库更新,手动安装一下

// 注意事项!!
// 如果遇到winerror,或者觉得损坏了某些东西并想从头开始重新安装,删除这些目录:venv,repositories然后重试。
// 如果想供局域网访问,按以下修改webui.py文件,之后0.0.0.0:7860可以访问
server_name="0.0.0.0" if cmd_opts.listen else None => server_name="0.0.0.0"

下载地址

Git 代理

1
2
3
4
5
6
7
8
9
10
11
12
# http
git config --global http.proxy http://127.0.0.1:7890
# https
git config --global https.proxy https://127.0.0.1:7890
# socks5
git config --global http.proxy socks5://192.168.0.1:1080
git config --global https.proxy socks5://192.168.0.1:1080
# 取消全局代理
git config --global --unset http.proxy
git config --global --unset https.proxy
# 只对github仓库走代理,其他不走代理,加上github.com,如
git config --global http://github.com.proxy http://192.168.0.1:1080

Git 操作

1. 解决每次涉及账户的操作都需要输密码的问题

  1. 保存账号密码:项目目录下输入git config --global credential.helper store, 之后在需要输入账号密码的时候输入一次账号密码就可以了,账号密码会被保存起来。
  2. 设置密钥:

2. 首次提交

1
2
3
4
5
6
7
8
9
10
11
git init
git add README.md
git commit -m "first commit"
# 重命名分支
git branch -M main
# 添加远程地址
git remote add origin https://github.com/umino-natsusou/demo.git
# 首次提交要加 -u
git push -u origin main
# 或者如下。-f表示远程仓库的修改将会被删除
git push -u origin main -f

3. 远程仓库地址设置

1
2
3
4
5
6
7
8
# 查看远程仓库地址
git remote -v

# 添加远程仓库地址
git remote add origin <地址>

# 更改远程仓库地址
git remote set-url origin <新地址>