NotionNext 部署-VPS本地部署
NotionNext 部署-VPS本地部署
category
技术工具
tags
NotionNext
建站
type
Post
slug
deploy-notion-next-on-vps
summary
在你的云服务器上手动部署NotionNext
status
Published
date
Nov 20, 2022
icon
password

前言

NotionNext可以轻松地使用Vercel免费托管,不过免费的流量和函数执行次数有限,如果你的网站访问量非常大,可以尝试在自己的云服务器、vps上部署。
用国内的VPS部署服务、会大大提升网站的访问速度。
💡
可以选择裸机安装Nodejs环境启动项目,或者也可以用Docker容器化部署。
💡
比较推荐有技术经验的小伙伴使用这套方案!

安装

💡
准备一台任意操作系统的VPS云服务器,不同系统的安装步骤大同小异,以下我用 linux-centos-7 主机为例

1. 安装Git

💡
git是一个代码托管工具,你可以用它来下载github上的代码。
# 一个命令安装
yum install git -y
执行效果
[root@tangly1024 ~]# yum install git -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.lzu.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirror.lzu.edu.cn
base                                                                                                                                                         | 3.6 kB  00:00:00
extras                                                                                                                                                       | 2.9 kB  00:00:00
updates                                                                                                                                                      | 2.9 kB  00:00:00
(1/2): extras/7/x86_64/primary_db                                                                                                                            | 249 kB  00:00:00
(2/2): updates/7/x86_64/primary_db                                                                                                                           |  18 MB  00:00:02
Resolving Dependencies
--> Running transaction check
---> Package git.x86_64 0:1.8.3.1-23.el7_8 will be installed
--> Processing Dependency: perl-Git = 1.8.3.1-23.el7_8 for package: git-1.8.3.1-23.el7_8.x86_64
--> Processing Dependency: perl(Term::ReadKey) for package: git-1.8.3.1-23.el7_8.x86_64
--> Processing Dependency: perl(Git) for package: git-1.8.3.1-23.el7_8.x86_64
--> Processing Dependency: perl(Error) for package: git-1.8.3.1-23.el7_8.x86_64
--> Running transaction check
---> Package perl-Error.noarch 1:0.17020-2.el7 will be installed
---> Package perl-Git.noarch 0:1.8.3.1-23.el7_8 will be installed
---> Package perl-TermReadKey.x86_64 0:2.30-20.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================================================
 Package                                         Arch                                  Version                                            Repository                           Size
====================================================================================================================================================================================
Installing:
 git                                             x86_64                                1.8.3.1-23.el7_8                                   base                                4.4 M
Installing for dependencies:
 perl-Error                                      noarch                                1:0.17020-2.el7                                    base                                 32 k
 perl-Git                                        noarch                                1.8.3.1-23.el7_8                                   base                                 56 k
 perl-TermReadKey                                x86_64                                2.30-20.el7                                        base                                 31 k

Transaction Summary
====================================================================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 4.5 M
Installed size: 22 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/perl-Error-0.17020-2.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for perl-Error-0.17020-2.el7.noarch.rpm is not installed
(1/4): perl-Error-0.17020-2.el7.noarch.rpm                                                                                                                   |  32 kB  00:00:00
(2/4): perl-Git-1.8.3.1-23.el7_8.noarch.rpm                                                                                                                  |  56 kB  00:00:00
(3/4): perl-TermReadKey-2.30-20.el7.x86_64.rpm                                                                                                               |  31 kB  00:00:00
(4/4): git-1.8.3.1-23.el7_8.x86_64.rpm                                                                                                                       | 4.4 MB  00:00:01
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                               3.9 MB/s | 4.5 MB  00:00:01
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-9.2009.0.el7.centos.x86_64 (@anaconda)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:perl-Error-0.17020-2.el7.noarch                                                                                                                                1/4
  Installing : perl-TermReadKey-2.30-20.el7.x86_64                                                                                                                              2/4
  Installing : perl-Git-1.8.3.1-23.el7_8.noarch                                                                                                                                 3/4
  Installing : git-1.8.3.1-23.el7_8.x86_64                                                                                                                                      4/4
  Verifying  : git-1.8.3.1-23.el7_8.x86_64                                                                                                                                      1/4
  Verifying  : 1:perl-Error-0.17020-2.el7.noarch                                                                                                                                2/4
  Verifying  : perl-Git-1.8.3.1-23.el7_8.noarch                                                                                                                                 3/4
  Verifying  : perl-TermReadKey-2.30-20.el7.x86_64                                                                                                                              4/4

Installed:
  git.x86_64 0:1.8.3.1-23.el7_8

Dependency Installed:
  perl-Error.noarch 1:0.17020-2.el7                         perl-Git.noarch 0:1.8.3.1-23.el7_8                         perl-TermReadKey.x86_64 0:2.30-20.el7

Complete!

2.安装nodejs

💡
这里使用nvm进行安装,NVM(Node version manager)是nodejs的专用版本管理器,可以快速方便地安装并切换的nodejs的版本。

借助git下载nvm

执行以下指令下载Git
# 用git从github下载nvm源码。
git clone https://github.com/cnpm/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
若您的服务器 因为网络限制 无法访问github,可使用国内的gitee下载nvm源码: git clone https://gitee.com/koalakit/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`

使用nvm安装Nodejs

执行以下命令安装NodeJS
# 1. 设置变量
echo "source ~/.nvm/nvm.sh" >> ~/.bashrc

# 2. 更新变量环境
source ~/.bashrc

# 3. 安装 nodejs , 这里举例使用v16.14.0版本,其它更新的版本也可以用
nvm install v16.14.0

# 4.【可选步骤】 npm切换国内淘宝网镜像,便于加速安装。
npm config set registry http://registry.npm.taobao.org

# 5. npm安装 yarn 
npm install -g yarn
💡
题外话: 用 nvm list-remote 命令可查看所有可安装的nodejs版本 用 nvm ls可查看所有已安装到本地的nodejs 版本

3. 运行NotionNext

# 来到您的用户主目录
cd ~ 

# 将Git上的代码下载到服务器中 ; 
git clone https://github.com/tangly1024/NotionNext && cd NotionNext


# 若您的服务器因为网络问题,无法访问github,可手动下载NotionNext代码 上传至服务器

首次安装项目环境

将项目所有依赖的脚本和库下载到服务器中
# 1.使用yarn命令安装依赖
yarn 

执行项目编译

每次修改代码配置,包括修改blog.config.js文件的内容后,都需要重新执行这步骤。
# 2.打包编译
yarn build

# 3.将你的notion_page_id设为环境变量,例如:
NOAGE_ID=29d5ia78b858e4a3bbc13e51b5400fb82

启动项目

将上一步编译好的代码内容,在服务器中运行;
# 执行命令启动
yarn start
执行效果记录
[root@tangly1024 NotionNext]# yarn
yarn install v1.22.19
info No lockfile found.
[1/4] Resolving packages...
warning react-notion-x > react-use > nano-css > sourcemap-codec@1.4.8: Please use @jridgewell/sourcemap-codec instead
warning valine > leancloud-storage > uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
warning valine > leancloud-storage > leancloud-realtime > uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
warning valine > leancloud-storage > superagent@3.8.3: Please upgrade to v7.0.2+ of superagent.  We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing.  See the releases tab for more information at <https://github.com/visionmedia/superagent/releases>.
warning valine > leancloud-storage > superagent > formidable@1.2.6: Please upgrade to latest, formidable@v2 or formidable@v3! Check these notes: https://bit.ly/2ZEqIau
warning valine > leancloud-storage > leancloud-realtime > @leancloud/platform-adapters-browser > @leancloud/adapters-superagent > superagent@5.3.1: Please upgrade to v7.0.2+ of superagent.  We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing.  See the releases tab for more information at <https://github.com/visionmedia/superagent/releases>.
warning valine > leancloud-storage > leancloud-realtime > @leancloud/platform-adapters-browser > @leancloud/adapters-superagent > superagent > formidable@1.2.6: Please upgrade to latest, formidable@v2 or formidable@v3! Check these notes: https://bit.ly/2ZEqIau
warning @waline/client > vue > @vue/compiler-sfc > magic-string > sourcemap-codec@1.4.8: Please use @jridgewell/sourcemap-codec instead
[2/4] Fetching packages...
[3/4] Linking dependencies...
warning "gitalk > react-flip-move@2.9.14" has incorrect peer dependency "react@0.13.x || 0.14.x || 15.x.x".
warning "gitalk > react-flip-move@2.9.14" has incorrect peer dependency "react-dom@0.13.x || 0.14.x || 15.x.x".
warning "react-facebook > react-spinner-children@1.0.8" has incorrect peer dependency "react@15.x || 16.x".
warning " > react-messenger-customer-chat@0.8.0" has incorrect peer dependency "react@^0.14.0 || ^15.0.0-0 || ^16.0.0-0".
warning "react-notion-x > react-lazy-images@1.1.0" has incorrect peer dependency "react@^15 || ^16".
warning "react-notion-x > react-lazy-images@1.1.0" has incorrect peer dependency "react-dom@^15 || ^16".
warning "react-notion-x > react-image@4.0.3" has unmet peer dependency "@babel/runtime@>=7".
warning "react-notion-x > react-pdf > file-loader@6.2.0" has unmet peer dependency "webpack@^4.0.0 || ^5.0.0".
warning "eslint-config-next > @typescript-eslint/parser > @typescript-eslint/typescript-estree > tsutils@3.21.0" has unmet peer dependency "typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta".
[4/4] Building fresh packages...
success Saved lockfile.
Done in 214.69s.
[root@tangly1024 NotionNext]# yarn build
yarn run v1.22.19
$ next build && next-sitemap --config next-sitemap.config.js
info  - Loaded env from /root/NotionNext/.env.local
Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://nextjs.org/telemetry

info  - Checking validity of types
info  - Creating an optimized production build
info  - Compiled successfully
info  - Collecting page data .[请求API] from:page-paths id:02ab3b8678004aa69e9e415905ef32a5
[请求API] from:tag-static-path id:02ab3b8678004aa69e9e415905ef32a5
[请求API] from:tag-page-static-path id:02ab3b8678004aa69e9e415905ef32a5
info  - Collecting page data ..[请求API] from:category-paths id:02ab3b8678004aa69e9e415905ef32a5
[请求API] from:category-paths id:02ab3b8678004aa69e9e415905ef32a5
info  - Collecting page data ...[响应成功]: from:page-paths
[API耗时] 1754ms
info  - Collecting page data .[响应成功]: from:tag-static-path
[API耗时] 1736ms
[响应成功]: from:tag-page-static-path
[API耗时] 1714ms
info  - Collecting page data ..[响应成功]: from:category-paths
[API耗时] 1778ms
info  - Collecting page data ...[响应成功]: from:category-paths
[API耗时] 2140ms
info  - Collecting page data
[    ] info  - Generating static pages (0/23)[请求API] from:search-props id:02ab3b8678004aa69e9e415905ef32a5
[请求API] from:404 id:02ab3b8678004aa69e9e415905ef32a5
[请求API] from:index id:02ab3b8678004aa69e9e415905ef32a5
[请求API] from:category-index-props id:02ab3b8678004aa69e9e415905ef32a5
[请求API] from:archive-index id:02ab3b8678004aa69e9e415905ef32a5
[请求API] from:tag-index-props id:02ab3b8678004aa69e9e415905ef32a5
[请求API] from:search-props id:02ab3b8678004aa69e9e415905ef32a5
[=== ] info  - Generating static pages (8/23)[响应成功]: from:tag-index-props
[API耗时] 1587ms
[请求API] from:search-props id:02ab3b8678004aa69e9e415905ef32a5
[响应成功]: from:404
[API耗时] 1668ms
[请求API] from:tag-props id:02ab3b8678004aa69e9e415905ef32a5
[响应成功]: from:index
[API耗时] 1774ms
[响应成功]: from:search-props
[API耗时] 1819ms
[响应成功]: from:archive-index
[API耗时] 1788ms
[请求API] from:tag-props id:02ab3b8678004aa69e9e415905ef32a5
[请求API] from:tag-props id:02ab3b8678004aa69e9e415905ef32a5
[响应成功]: from:search-props
[API耗时] 1737ms
[请求API] from:tag-props id:02ab3b8678004aa69e9e415905ef32a5
[请求API] from:category-props id:02ab3b8678004aa69e9e415905ef32a5
[ ===] info  - Generating static pages (14/23)[响应成功]: from:category-index-props
[API耗时] 1958ms
[请求API] from:category-props id:02ab3b8678004aa69e9e415905ef32a5
[   =] info  - Generating static pages (15/23)[响应成功]: from:search-props
[API耗时] 1720ms
[响应成功]: from:tag-props
[API耗时] 1687ms
[    ] info  - Generating static pages (17/23)[响应成功]: from:category-props
[API耗时] 1606ms
[响应成功]: from:tag-props
[API耗时] 1707ms
[响应成功]: from:tag-props
[API耗时] 1698ms
[响应成功]: from:tag-props
[API耗时] 1728ms
[响应成功]: from:category-props
[API耗时] 1730ms
info  - Generating static pages (23/23)
info  - Finalizing page optimization

Page                                                          Size     First Load JS
┌ ● / (ISR: 5 Seconds) (1930 ms)                              524 B           543 kB
├   /_app                                                     0 B             542 kB
├ ● /[...slug]                                                1.58 kB         544 kB
├ ○ /404 (1811 ms)                                            640 B           543 kB
├ ● /archive (ISR: 1 Seconds) (1806 ms)                       656 B           543 kB
├ ● /category (ISR: 1 Seconds) (2097 ms)                      667 B           543 kB
├ ● /category/[category] (ISR: 1 Seconds) (3373 ms)           707 B           543 kB
├   ├ /category/技术分享 (1748 ms)
├   └ /category/学习思考 (1619 ms)
├ ● /category/[category]/page/[page]                          716 B           543 kB
├ λ /feed                                                     249 B           542 kB
├ ● /page/[page]                                              652 B           543 kB
├ ● /search (ISR: 1 Seconds) (1944 ms)                        801 B           543 kB
├ ● /search/[keyword] (ISR: 1 Seconds) (1899 ms)              678 B           543 kB
├   └ /search/NotionNext BLOG (1759 ms)
├ ● /search/[keyword]/page/[page] (ISR: 1 Seconds) (1867 ms)  687 B           543 kB
├   └ /search/NotionNext BLOG/page/1 (1735 ms)
├ ● /tag (ISR: 1 Seconds) (1606 ms)                           651 B           543 kB
├ ● /tag/[tag] (ISR: 1 Seconds) (6924 ms)                     679 B           543 kB
├   ├ /tag/开发 (1743 ms)
├   ├ /tag/思考 (1730 ms)
├   ├ /tag/建站 (1715 ms)
├   └ /tag/文字 (1709 ms)
└ ● /tag/[tag]/page/[page]                                    687 B           543 kB
+ First Load JS shared by all                                 542 kB
  ├ chunks/framework-bb5c596eafb42b22.js                      42.1 kB
  ├ chunks/main-c9bcff17d385c69a.js                           28.7 kB
  ├ chunks/pages/_app-e8a826e85bd57a50.js                     469 kB
  ├ chunks/webpack-de079d6da0c84f1e.js                        2.32 kB
  └ css/02c66091ce82e882.css                                  31.3 kB

λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)(Static)  automatically rendered as static HTML (uses no initial props)(SSG)     automatically generated as static HTML + JSON (uses getStaticProps)
   (ISR)     incremental static regeneration (uses revalidate in getStaticProps)

Loaded env from /root/NotionNext/.env.local
Done in 113.42s.
[root@tangly1024 NotionNext]# yarn start
yarn run v1.22.19
$ next start
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Loaded env from /root/NotionNext/.env.local

访问项目

💡
项目默认将在 3000端口启动,访问你的服务器 http://ip:3000 即可打开网站。
💡
有时候3000端口可能无法访问,原因是服务器防火墙,以及云服务厂商的安全组限制。
关于阿里云服务器安全组 点击展开
阿里云在云服务器ECS→安全组中,手动添加一个安全组,暴露端口3000,授权对象0.0.0.0/0 表示所有ip的人都可以访问这个端口。另外,推荐再添加一个80端口暴露的安全组,便于nginx访问。
notion image
  • 访问效果,用服务器ip加端口号打开了网页
notion image

自定义端口

假如3000端口被占用,或您想多开notionnext可用以下命令指定端口
yarn start -p 8080
# -p [自定义端口号]

4.后台运行

直接使用 yarn start启动服务的话,一旦你关闭ssh终端或登出服务器,这个进程也会随之关闭。为了让你的网站始终在后台保持运行,可以用以下方式:
  1. ctrl+c 退出上面正运行的yarn进程
  1. 改用nohup运行
    1. cd NotionNext
      
      nohup yarn start >/dev/null 2>&1 &
      💡
      若想要输出日志文件,可执行nohup yarn start > notionnext.log 2>&1 &
      执行结果
      [root@tangly1024 ~]# nohup yarn start >/dev/null 2>&1 &
      [1] 5240
      # 会打印出后台 运行的进程号。

如何关闭进程

pkill -f "yarn start"
或者麻烦点
ps -ef | grep yarn # 查找正在后台运行的yarn进程

kill 4952 # 4952 是上一步找到的进程号,每次都是不同的随机数。详见下方执行效果展开
执行效果
如下, 我用 ps -ef | grep yarn 指令 查出两个进程,id分别是 4952 ,5228 , 第一个进程4952就是正在运行的网站。
[root@tangly1024 ~] ps -ef | grep yarn
root       4952      1  1 02:52 ?        00:00:00 node /root/.nvm/versions/node/v16.13.0/bin/yarn start
root       5228   4987  0 02:52 pts/0    00:00:00 grep --color=auto yarn
[root@tangly1024 ~] kill 4952
[1]+  Exit 1                  nohup yarn start > /dev/null 2>&1
💡
其中 4952 就是要删除的进程号
 

5. 关于网站崩溃

部分网友的服务器运行yarn start 启动NotionNext站点后,总会出现过一段时间 站点进程莫名其妙被杀死,导致网站暂停,这个问题,我目前还没有发现根本原因。
如果您的站点出现这种情况,可以尝试这种做法,自动启动站点
创建一个脚本start_yarn.sh 在您的服务器上,
脚本的内容如下,粘贴到文本中
#!/bin/bash

while true; do
    # 检查 Yarn 进程是否存在
    if pgrep -x "yarn" > /dev/null; then
        echo "Yarn process is already running."
    else
        # 执行命令
        nohup yarn start >/dev/null 2>&1 &

        # 获取最新启动的进程ID
        pid=$!

        echo "Yarn process started with PID: $pid"
    fi

    # 等待一段时间后重新执行
    sleep 5
done
如何将脚本粘贴到文件中并保存?
# 进入项目文件夹
cd NotionNext
# 用编辑器创建并打开这个文件
vim start_yarn.sh

# 按键盘上的 i , 进入INSERT编辑模式
# 复制上方的脚本内容,按下快捷键 SHIRT + Insert 键粘贴到start_yarn.sh
# 按键盘的的Esc件,退出编辑模式
# 输入 :wq  在按下回车键,保存脚本。
为这个脚本添加执行权限
chmod +x start_yarn.sh

运行启动脚本

nohup ./start_yarn.sh > /dev/null 2>&1 &

运行关闭脚本

pkill -f "yarn"

自定义修改

1.用环境变量修改配置

💡
用环境变量的好处是,不需要修改代码,不用重新打包编译。直接重启服务即可。
  • NOTION_PAGE_ID等诸多配置支持用环境变量配置。
    • # 1.设置环境变量
      NOTION_PAGE_ID=29d5ia78b858e4a3bbc13e51b5400fb82
      # 2.关闭旧进程
      # 命令参考上文
      # 3.后台运行
      nohup yarn start >/dev/null 2>&1 &
      💡
      删除环境变量可用以下命令: unset NOTION_PAGE_ID

2.修改代码

  • 如果修改blog.config.js或其他代码文件,需要重新执行 yarn build 才可 生效,
  • 您可以在项目根目录的.env.local文件中配置您的环境变量
  • 重新编译后需要先关闭旧进程,再用 yarn start 重启服务 。
 

Nginx反向代理

  • 为了隐藏你的3000端口号,用一个nginx就可以,如果你有宝塔面板之类的运维工具可以很方便地一键配置。
  • 小白安装nginx
    • # 添加nginx源
      sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
      
      # 安装nginx
      sudo yum install nginx
      sudo systemctl enable nginx
      sudo systemctl start nginx
      安装成功访问服务器地址 效果:
      notion image
  • 反向代理配置参考
    • 修改相关配置文件 /etc/nginx/conf.d/default.conf
      # 用vi编辑器修改
      vi /etc/nginx/conf.d/default.conf
      
      # 在 `localtion /` 下增加一行
      proxy_pass  http://127.0.0.1:3000;
      
      # 刷新ng配置
      sudo systemctl reload nginx
      notion image
      notion image
      💡
      如果访问出错可能是SELinux设置不允许nginx的转发 ,执行 setsebool -P httpd_can_network_connect on 即可修复。
  • NotionNext
  • 建站
  • NotionNext 操作手册NotionNext配置评论插件Twikoo