nginx
80端口 默认入口:/usr/share/nginx/html
目录 : /etc/nginx/conf.d/*
参考:https://juejin.cn/post/6897100462676246541
nginx 命令
# 常规netstat -nltp # 查看端口使用情况netstat -ntlp | grep 3030# nginxnginx -t # 语法检测service nginx restart # 重启nginxnginx -s reload # 重新加载,错误会回滚
安装依赖
? npm 安装存在问题
sudo apt-get update # 更新软件列表sudo apt-get upgrade -y # 对比各软件版本并更新sudo apt-get install nodejs -y # 安装 nodejssudo apt-get install npm -y # 安装 npmsudo npm install n -g # 安装 nodejs 版本管理器n latest # 更新 nodejs 至最新版本,可自行选择,更新完成记得重新连接服务器输入 node -v 检查是否更新成功sudo npm install pm2 -g # 安装 pm2,node 进程管理工具,这里主要用于持久化运行和监控 Nestjs 服务器
安装 nginx
sudo apt-get install nginx -y # 安装 Nginx 服务器service nginx start # 启动 Nginx 服务器
配置 nginx 服务器
Hash路由
server {listen 8010;server_name 127.0.0.1;index index.html;gzip on;gzip_min_length 1000;gzip_proxied expired no-cache no-store private auth;gzip_types text/plain application/x-javascript text/html text/css application/xml application/octet-stream;root /data/test_web;}
History 路由
server {listen 80;server_name localhost;location / {root /app;index index.html;try_files $uri $uri/ /index.html;}error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}}
其他
server{listen 80;index index.php index.html index.htm;server_name rpc.freeswap.info;location /images/ {root /data/rpc;}location / {proxy_pass http://127.0.0.1:8545;proxy_set_header Host $proxy_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# add_header Access-Control-Allow-Origin '*';# add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';# add_header Access-Control-Allow-Headers 'content-type,x-sf,Origin,Accept,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';}}
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=nextjs_cache:10m inactive=1d use_temp_path=off;upstream nextjs_upstream {server 127.0.0.1:3000;keepalive 500;}server {listen 443 ssl http2;server_name your.domain.com;ssl_certificate /etc/nginx/ssl/domain.com.cer;ssl_certificate_key /etc/nginx/ssl/domain.com.key;ssl_dhparam /etc/nginx/ssl/dhparam.pem;ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';ssl_prefer_server_ciphers on;ssl_session_cache shared:SSL:20m;ssl_session_timeout 120m;gzip on;gzip_min_length 1k;gzip_buffers 16 8k;gzip_comp_level 5;gzip_vary on;gzip_proxied any;gzip_types text/css application/json application/javascript image/svg+xml;charset utf-8;access_log /var/log/nginx/your.domain.com/access.log main;error_log /var/log/nginx/your.domain.com/error.log warn;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;proxy_set_header X-Forward-Proto http;location /_next/static {proxy_cache nextjs_cache;proxy_pass http://nextjs_upstream;add_header X-Cache-Status $upstream_cache_status;}location /static {proxy_cache nextjs_cache;proxy_ignore_headers Cache-Control;proxy_cache_valid 60m;proxy_pass http://nextjs_upstream;# For testing cache - remove before deploying to productionadd_header X-Cache-Status $upstream_cache_status;}location / {proxy_pass http://nextjs_upstream;}error_page 404 /404.html;error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}}
问题
root 权限问题
sudo passwd root # 设置root密码su root # 登录rootsudo passwd -l root # 禁用root账号
无法创建nginx配置文件
sudo touch test.conf
xftp 无法创建文件
编辑 /etc/ssh/sshd_config 文件
# PermitRootLogin Prohibit-passwordPermitRootLogin yesPermitEmptyPasswords no
重启ssh服务
service ssh restart(或者执行:/etc/init.d/ssh restart)
重启服务器
登录
再次通过xshell登录
yarn
安装 yarn
https://www.myfreax.com/how-to-install-yarn-on-ubuntu-18-04/
安装 nodejs 和 npmjs
https://www.myfreax.com/how-to-install-node-js-on-ubuntu-18-04/
pm2
pm2 命令
# pm2 命令pm2 list # pm2 列表pm2 start yarn --name demo -- start # 运行 yarn start - 取名为demopm2 startup # 开机自启pm2 unstartup # 关闭开机自启pm2 show demo # 查看详情pm2 monit # 监控性能pm2 start ecosystem.config.js # 通过配置启动pm2 start ecosystem.config.js --only myapp # 单独处理 myapp
配置启动多个项目
module.exports = {apps: [{name: 'p57_swap',script: 'node_modules/next/dist/bin/next',args: 'start -p 3012',max_memory_restart: '1024M',cwd: '/data/p57_swap', // 根目录instances: 1,error_file: './logs/error.log',out_file: './logs/out.log',log_date_format: 'YYYY-MM-DD HH:mm Z',watch: true,ignore_watch: ['node_modules'],},{name: 'qj_swap',script: 'node_modules/next/dist/bin/next',args: 'start -p 3011',max_memory_restart: '1024M',cwd: '/data/qj_swap', // 根目录instances: 1,error_file: './logs/error.log',out_file: './logs/out.log',log_date_format: 'YYYY-MM-DD HH:mm Z',watch: true,ignore_watch: ['node_modules'],}],}
实战 - 部署nextjs项目
文件
重要文件及文件夹说明
项目
配置
nginx 配置文件地址
文件路径:/etc/nginx/conf.d
qj_swap nginx 配置文件 qj_swap.conf
server {listen 8011;server_name 12.12.12.12; # 需配置为对应服务器端口index index.html;gzip on;gzip_min_length 1000;gzip_proxied expired no-cache no-store private auth;gzip_types text/plain application/x-javascript text/html text/css application/xml application/octet-stream;location / {proxy_pass http://127.0.0.1:3011;proxy_set_header Host $proxy_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}
注意:12.12.12.12 - 改为对应服务端口
pm2 配置文件
文件路径: /etc/nginx/conf.d/ecosystem.config.js
参数说明
- name : pm2名称
- script:命令
- args:命令参数
- cwd:项目根目录
module.exports = {apps: [{name: 'p57_swap',script: 'node_modules/next/dist/bin/next',args: 'start -p 3012',max_memory_restart: '1024M',cwd: '/data/p57_swap', // 根目录instances: 1,error_file: './logs/error.log',out_file: './logs/out.log',log_date_format: 'YYYY-MM-DD HH:mm Z',watch: false,ignore_watch: ['node_modules'],}],}
ecosystem.config.js 为固定名称
启动方式 : pm2 start ecosystem.config.js
相关链接
pm2官网:https://pm2.keymetrics.io/
样式修改:https://styled-components.com/
实战 - 自动化部署 vitepress
获取 token - 本项目未用
配置 Repository secrets
配置 workflows
.github/workflows/main.yml
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actionsname: tk-notes-ci/cdon:push:branches: [master]# pull_request:# branches: [master]jobs:build:runs-on: ubuntu-lateststrategy:matrix:node-version: [16.x]steps:# download source- name: Checkoutuses: actions/checkout@master# Install & Build- name: Builduses: actions/setup-node@master- run: |yarn installyarn build# copy dist- name: Uploaduses: appleboy/scp-action@masterwith:host: ${{ secrets.HOST }}username: ${{ secrets.USERNAME }}password: ${{ secrets.PASSWORD }}port: ${{ secrets.PORT }}source: './docs/.vitepress/dist/*'target: '/data/tk-notes'strip_components: 3 # 去掉 3 层路径, /data/tk-notes/docs/.vitepress/dist/* => /data/tk-notes/*# - name: Deploy# uses: appleboy/ssh-action@master# with:# host: ${{ secrets.HOST }}# username: ${{ secrets.USERNAME }}# password: ${{ secrets.PASSWORD }}# port: ${{ secrets.PORT }}# script: |# cd /data/tk-notes# yarn install# yarn build# pm2 start yarn --name tk-docs -- serve
