跳到主要内容

6、部署常见问题

部署常见问题

版权声明

注意

本着开源共享、共同学习的精神:

本文是记录了 博主Ruyu 开源项目:《Ruyu-Blog 》https://gitee.com/kuailemao/ruyu-blog 部署&使用过程得一些细节,转载无需和我联系,但请注明文章来源。如果侵权之处,请联系博主进行删除,谢谢~

强烈推荐:

博主Ruyu

Ruyu-Blog开源博客gitee地址:https://gitee.com/kuailemao/ruyu-blog

Ruyu-Blog开源博客网站地址:https://www.kuailemao.xyz/

image-20240804154704039

image-20240804154737946

案例:前端前台构建需要依赖python3环境-2024.8.16(已解决)

1、报错现象

[root@localhost kuailemao-blog]#     pnpm install
Lockfile is up to date, resolution step is skipped
Packages: +742
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

╭─────────────────────────────────────────────────────────────────╮
│ │
│ Update available! 8.12.0 → 9.7.1. │
│ Changelog: https://github.com/pnpm/pnpm/releases/tag/v9.7.1 │
│ Run "pnpm add -g pnpm" to update. │
│ │
│ Follow @pnpmjs for updates: https://twitter.com/pnpmjs │
│ │
╰─────────────────────────────────────────────────────────────────╯

Downloading mirrors.huaweicloud.com/element-plus/2.7.6: 8.71 MB/8.71 MB, done
Downloading mirrors.huaweicloud.com/echarts/5.5.1: 10.36 MB/10.36 MB, done
Progress: resolved 742, reused 0, downloaded 742, added 742, done
node_modules/.pnpm/vue-demi@0.14.8_vue@3.4.31/node_modules/vue-demi: Running postinstall script, done in 220ms
node_modules/.pnpm/esbuild@0.18.20/node_modules/esbuild: Running postinstall script, done in 101ms
node_modules/.pnpm/node-sass@9.0.0/node_modules/node-sass: Running install script, done in 22s
node_modules/.pnpm/node-sass@9.0.0/node_modules/node-sass: Running postinstall script, failed in 301ms
.../node_modules/node-sass postinstall$ node scripts/build.js
│ Building: /usr/local/nodejs/bin/node /root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose --libsass_ext= -…
│ gyp info it worked if it ends with ok
│ gyp verb cli [
│ gyp verb cli '/usr/local/nodejs/bin/node',
│ gyp verb cli '/root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/bin/node-gyp.js',
│ gyp verb cli 'rebuild',
│ gyp verb cli '--verbose',
│ gyp verb cli '--libsass_ext=',
│ gyp verb cli '--libsass_cflags=',
│ gyp verb cli '--libsass_ldflags=',
│ gyp verb cli '--libsass_library='
│ gyp verb cli ]
│ gyp info using node-gyp@8.4.1
│ gyp info using node@16.20.2 | linux | x64
│ gyp verb command rebuild []
│ gyp verb command clean []
│ gyp verb clean removing "build" directory
│ gyp verb command configure []
│ gyp verb find Python Python is not set from command line or npm configuration
│ gyp verb find Python Python is not set from environment variable PYTHON
│ gyp verb find Python checking if "python3" can be used
│ gyp verb find Python - executing "python3" to get executable path
│ gyp verb find Python - "python3" is not in PATH or produced an error
│ gyp verb find Python checking if "python" can be used
│ gyp verb find Python - executing "python" to get executable path
│ gyp verb find Python - executable path is "/usr/bin/python"
│ gyp verb find Python - executing "/usr/bin/python" to get version
│ gyp verb find Python - version is "2.7.5"
│ gyp verb find Python - version is 2.7.5 - should be >=3.6.0
│ gyp verb find Python - THIS VERSION OF PYTHON IS NOT SUPPORTED
│ gyp ERR! find Python
│ gyp ERR! find Python Python is not set from command line or npm configuration
│ gyp ERR! find Python Python is not set from environment variable PYTHON
│ gyp ERR! find Python checking if "python3" can be used
│ gyp ERR! find Python - "python3" is not in PATH or produced an error
│ gyp ERR! find Python checking if "python" can be used
│ gyp ERR! find Python - executable path is "/usr/bin/python"
│ gyp ERR! find Python - version is "2.7.5"
│ gyp ERR! find Python - version is 2.7.5 - should be >=3.6.0
│ gyp ERR! find Python - THIS VERSION OF PYTHON IS NOT SUPPORTED
│ gyp ERR! find Python
│ gyp ERR! find Python **********************************************************
│ gyp ERR! find Python You need to install the latest version of Python.
│ gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
│ gyp ERR! find Python you can try one of the following options:
│ gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
│ gyp ERR! find Python (accepted by both node-gyp and npm)
│ gyp ERR! find Python - Set the environment variable PYTHON
│ gyp ERR! find Python - Set the npm configuration variable python:
│ gyp ERR! find Python npm config set python "/path/to/pythonexecutable"
│ gyp ERR! find Python For more information consult the documentation at:
│ gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
│ gyp ERR! find Python **********************************************************
│ gyp ERR! find Python
│ gyp ERR! configure error
│ gyp ERR! stack Error: Could not find any Python installation to use
│ gyp ERR! stack at PythonFinder.fail (/root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/lib/find-python.js:330:47)
│ gyp ERR! stack at PythonFinder.runChecks (/root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/lib/find-python.js:159:21)
│ gyp ERR! stack at PythonFinder.<anonymous> (/root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/lib/find-python.js:266:16)
│ gyp ERR! stack at PythonFinder.execFileCallback (/root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/lib/find-python.js:297:7)
│ gyp ERR! stack at ChildProcess.exithandler (node:child_process:394:7)
│ gyp ERR! stack at ChildProcess.emit (node:events:513:28)
│ gyp ERR! stack at maybeClose (node:internal/child_process:1100:16)
│ gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:304:5)
│ gyp ERR! System Linux 3.10.0-957.el7.x86_64
│ gyp ERR! command "/usr/local/nodejs/bin/node" "/root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-gyp@8.4.1/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "…
│ gyp ERR! cwd /root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-sass@9.0.0/node_modules/node-sass
│ gyp ERR! node -v v16.20.2
│ gyp ERR! node-gyp -v v8.4.1
│ gyp ERR! not ok
│ Build failed with error code: 1
└─ Failed in 301ms at /root/ruyu-blog/blog-frontend/kuailemao-blog/node_modules/.pnpm/node-sass@9.0.0/node_modules/node-sass
 ELIFECYCLE  Command failed with exit code 1.
[root@localhost kuailemao-blog]#


这个如何解决呢?

2、解决办法

gpt回答

根据你提供的错误信息,node-gyp 找不到合适的 Python 版本。它找到的 Python 版本是 2.7.5,而 node-gyp 需要 Python 3.6 或更高版本。以下是解决此问题的步骤:

  1. 安装 Python 3

首先,你需要在系统中安装 Python 3。如果你的系统已经安装了 Python 3,请确保它在 PATH 中。

在 CentOS 或 RHEL 系统上,你可以使用以下命令安装 Python 3:

sudo yum -y install python3 --disablerepo=epel
  1. 设置 PYTHON 环境变量

安装 Python 3 之后,设置 PYTHON 环境变量以指向 Python 3 可执行文件。

export PYTHON=$(which python3)
  1. 配置 npm 使用 Python 3

你还可以配置 npm 以使用 Python 3:

npm config set python $(which python3)

pnpm config set python $(which python3)
  1. 重试 pnpm install

现在重新运行 pnpm install

pnpm install
  1. 验证安装

如果问题仍然存在,确保你的 python3 确实在 PATH 中并可以正确执行:

python3 --version

你应该看到类似于 Python 3.x.x 的输出(确保版本是 3.6 或更高)。

总结

通过以上步骤,你应该能解决 node-gyp 找不到合适 Python 版本的问题。确保 Python 3 安装并配置正确后,pnpm install 应该能顺利完成。

如果还有问题,可以检查以下几点:

  • 确认 python3 是否在 PATH 中,可以通过 which python3 来验证路径。
  • 确认 npm 配置正确,可以通过 npm config get python 来查看配置。

希望这些步骤能帮助你解决问题!


本yum安装python3方法

    ##安装python3
sudo yum -y install python3 --disablerepo=epel

安装python3后现象:

image-20240818084942011

[root@localhost ~]# python
python python2 python2.7 python3 python3.6 python3.6m
[root@localhost ~]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@localhost ~]# ll `which python`
lrwxrwxrwx. 1 root root 7 Aug 15 12:14 /usr/bin/python -> python2
[root@localhost ~]# ll `which python2`
lrwxrwxrwx. 1 root root 9 Aug 15 12:14 /usr/bin/python2 -> python2.7
[root@localhost ~]# ll `which python2.7`
-rwxr-xr-x. 1 root root 7216 Oct 31 2018 /usr/bin/python2.7
[root@localhost ~]# ll `which python3`
lrwxrwxrwx. 1 root root 9 Aug 17 17:15 /usr/bin/python3 -> python3.6
[root@localhost ~]# ll `which python3.6`
-rwxr-xr-x. 2 root root 11336 Nov 15 2023 /usr/bin/python3.6
[root@localhost ~]#

ruyu-blog需要python3环境时配置

pnpm config set python $(which python3)
python3 --version

扩展:给某个文件做软连接 -s -f -n选项

sudo ln -sfn /usr/bin/python3.6 /usr/bin/python  #注意,这个命令时有风险的哦!!!

sudo ln -sfn /usr/bin/python3.6 /usr/bin/python 这条命令用于创建一个符号链接,使 /usr/bin/python 指向 /usr/bin/python3.6。以下是每个参数的含义:

  • sudo: 提权命令,意味着需要以超级用户权限执行该命令。
  • ln: 创建链接命令。
  • -s: 创建符号链接(软链接),而不是硬链接。
  • -f: 强制删除已经存在的目标文件(如果 /usr/bin/python 已经存在,会被删除)。
  • -n: 如果目标是一个符号链接,直接覆盖它,而不是在其上创建新的链接。
  • /usr/local/bin/python3.8: 链接的源文件,即要指向的实际文件路径。
  • /usr/bin/python: 链接的目标文件,即符号链接的路径。

这个命令的最终效果是使系统在执行 python 命令时,实际上运行的是 python3.8

注意

**这种做法在需要特定版本的 Python 并且不想直接更改系统默认 python 版本的情况下非常有用。**然而,需注意以下几点:

  1. 系统依赖:许多系统工具和脚本依赖于特定版本的 Python(经常是 Python 2.x),将 /usr/bin/python 修改为指向 Python 3.x 可能会导致这些工具和脚本无法正常工作。(例如yum)
  2. 环境管理:使用 pyenv 或者虚拟环境(virtualenvvenv)工具来管理不同版本的 Python 和依赖项通常是更安全和优雅的方法。

如果你确定没有上述问题,或者你已经做好了相应的准备和测试,那么可以使用这条命令来更改默认的 python 版本。


这里我们来测试下:

配置前:

[root@localhost ~]# python
python python2 python2.7 python3 python3.6 python3.6m
[root@localhost ~]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@localhost ~]# ll `which python`
lrwxrwxrwx. 1 root root 7 Aug 15 12:14 /usr/bin/python -> python2
[root@localhost ~]# ll `which python2`
lrwxrwxrwx. 1 root root 9 Aug 15 12:14 /usr/bin/python2 -> python2.7
[root@localhost ~]# ll `which python2.7`
-rwxr-xr-x. 1 root root 7216 Oct 31 2018 /usr/bin/python2.7
[root@localhost ~]# ll `which python3`
lrwxrwxrwx. 1 root root 9 Aug 17 17:15 /usr/bin/python3 -> python3.6
[root@localhost ~]# ll `which python3.6`
-rwxr-xr-x. 2 root root 11336 Nov 15 2023 /usr/bin/python3.6
[root@localhost ~]#

配置后:

ln -sfn /usr/bin/python3.6 /usr/bin/python
  • ln: 创建链接命令。
  • -s: 创建符号链接(软链接),而不是硬链接。
  • -f: 强制删除已经存在的目标文件(如果 /usr/bin/python 已经存在,会被删除)。
  • -n: 如果目标是一个符号链接,直接覆盖它,而不是在其上创建新的链接。

image-20240818090830337

如何恢复呢?(已恢复)

#原来内容
ln -sfn python2 /usr/bin/python

image-20240818090724252

image-20240818090756082

3、验证效果

欧克。

案例:前端前台node-sass@9.0.0依赖包经常导致pnpm install失败-2024.8.16(已解决)

1、故障现象

部署过程可能出现的问题就是前端前台pnpm install有问题,其他都没问题,我们再次构建下就行了:一般不会有啥问题的。

(这个看几率的,有时就正常,有时就有问题的)

image-20240817172313630

image-20240817172011186

image-20240817172026797

image-20240817172140106

image-20240817172155476

2、解决办法

重新安装下这个包就好了:

##部署前端前台
cd /root/ruyu-blog/blog-frontend/kuailemao-blog/
rm -rf dist
source /etc/profile
pnpm install
pnpm uninstall node-sass@9.0.0
pnpm install node-sass@9.0.0
pnpm build

#删除老容器和老镜像
docker rm -f blog-qt
docker rmi -f blog-qt

#构建新镜像和启动新容器
docker build . -t blog-qt
docker run --name blog-qt -d -p 80:80 blog-qt
docker ps
##