nginx

80端口 默认入口:/usr/share/nginx/html
目录 : /etc/nginx/conf.d/*

参考:https://juejin.cn/post/6897100462676246541

nginx 命令

  1. # 常规
  2. netstat -nltp # 查看端口使用情况
  3. netstat -ntlp | grep 3030
  4. # nginx
  5. nginx -t # 语法检测
  6. service nginx restart # 重启nginx
  7. nginx -s reload # 重新加载,错误会回滚

安装依赖

? npm 安装存在问题

  1. sudo apt-get update # 更新软件列表
  2. sudo apt-get upgrade -y # 对比各软件版本并更新
  3. sudo apt-get install nodejs -y # 安装 nodejs
  4. sudo apt-get install npm -y # 安装 npm
  5. sudo npm install n -g # 安装 nodejs 版本管理器
  6. n latest # 更新 nodejs 至最新版本,可自行选择,更新完成记得重新连接服务器输入 node -v 检查是否更新成功
  7. sudo npm install pm2 -g # 安装 pm2,node 进程管理工具,这里主要用于持久化运行和监控 Nestjs 服务器

安装 nginx

  1. sudo apt-get install nginx -y # 安装 Nginx 服务器
  2. service nginx start # 启动 Nginx 服务器

配置 nginx 服务器

Hash路由

  1. server {
  2. listen 8010;
  3. server_name 127.0.0.1;
  4. index index.html;
  5. gzip on;
  6. gzip_min_length 1000;
  7. gzip_proxied expired no-cache no-store private auth;
  8. gzip_types text/plain application/x-javascript text/html text/css application/xml application/octet-stream;
  9. root /data/test_web;
  10. }

History 路由

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. location / {
  5. root /app;
  6. index index.html;
  7. try_files $uri $uri/ /index.html;
  8. }
  9. error_page 500 502 503 504 /50x.html;
  10. location = /50x.html {
  11. root /usr/share/nginx/html;
  12. }
  13. }

其他

  1. server{
  2. listen 80;
  3. index index.php index.html index.htm;
  4. server_name rpc.freeswap.info;
  5. location /images/ {
  6. root /data/rpc;
  7. }
  8. location / {
  9. proxy_pass http://127.0.0.1:8545;
  10. proxy_set_header Host $proxy_host;
  11. proxy_set_header X-Real-IP $remote_addr;
  12. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  13. # add_header Access-Control-Allow-Origin '*';
  14. # add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
  15. # 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';
  16. }
  17. }
  1. proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=nextjs_cache:10m inactive=1d use_temp_path=off;
  2. upstream nextjs_upstream {
  3. server 127.0.0.1:3000;
  4. keepalive 500;
  5. }
  6. server {
  7. listen 443 ssl http2;
  8. server_name your.domain.com;
  9. ssl_certificate /etc/nginx/ssl/domain.com.cer;
  10. ssl_certificate_key /etc/nginx/ssl/domain.com.key;
  11. ssl_dhparam /etc/nginx/ssl/dhparam.pem;
  12. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  13. 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';
  14. ssl_prefer_server_ciphers on;
  15. ssl_session_cache shared:SSL:20m;
  16. ssl_session_timeout 120m;
  17. gzip on;
  18. gzip_min_length 1k;
  19. gzip_buffers 16 8k;
  20. gzip_comp_level 5;
  21. gzip_vary on;
  22. gzip_proxied any;
  23. gzip_types text/css application/json application/javascript image/svg+xml;
  24. charset utf-8;
  25. access_log /var/log/nginx/your.domain.com/access.log main;
  26. error_log /var/log/nginx/your.domain.com/error.log warn;
  27. proxy_http_version 1.1;
  28. proxy_set_header Connection "";
  29. proxy_set_header Host $http_host;
  30. proxy_set_header X-Real-IP $remote_addr;
  31. proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
  32. proxy_set_header X-Forward-Proto http;
  33. location /_next/static {
  34. proxy_cache nextjs_cache;
  35. proxy_pass http://nextjs_upstream;
  36. add_header X-Cache-Status $upstream_cache_status;
  37. }
  38. location /static {
  39. proxy_cache nextjs_cache;
  40. proxy_ignore_headers Cache-Control;
  41. proxy_cache_valid 60m;
  42. proxy_pass http://nextjs_upstream;
  43. # For testing cache - remove before deploying to production
  44. add_header X-Cache-Status $upstream_cache_status;
  45. }
  46. location / {
  47. proxy_pass http://nextjs_upstream;
  48. }
  49. error_page 404 /404.html;
  50. error_page 500 502 503 504 /50x.html;
  51. location = /50x.html {
  52. root /usr/share/nginx/html;
  53. }
  54. }

问题

root 权限问题

  1. sudo passwd root # 设置root密码
  2. su root # 登录root
  3. sudo passwd -l root # 禁用root账号

无法创建nginx配置文件

  1. sudo touch test.conf

xftp 无法创建文件

权限不够,需要让xshell 登录root账号

编辑 /etc/ssh/sshd_config 文件

  1. # PermitRootLogin Prohibit-password
  2. PermitRootLogin yes
  3. PermitEmptyPasswords no

image.png

重启ssh服务

service ssh restart(或者执行:/etc/init.d/ssh restart)

重启服务器

shutdown -r now

登录

再次通过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 命令

  1. # pm2 命令
  2. pm2 list # pm2 列表
  3. pm2 start yarn --name demo -- start # 运行 yarn start - 取名为demo
  4. pm2 startup # 开机自启
  5. pm2 unstartup # 关闭开机自启
  6. pm2 show demo # 查看详情
  7. pm2 monit # 监控性能
  8. pm2 start ecosystem.config.js # 通过配置启动
  9. pm2 start ecosystem.config.js --only myapp # 单独处理 myapp

配置启动多个项目

  1. module.exports = {
  2. apps: [
  3. {
  4. name: 'p57_swap',
  5. script: 'node_modules/next/dist/bin/next',
  6. args: 'start -p 3012',
  7. max_memory_restart: '1024M',
  8. cwd: '/data/p57_swap', // 根目录
  9. instances: 1,
  10. error_file: './logs/error.log',
  11. out_file: './logs/out.log',
  12. log_date_format: 'YYYY-MM-DD HH:mm Z',
  13. watch: true,
  14. ignore_watch: ['node_modules'],
  15. },
  16. {
  17. name: 'qj_swap',
  18. script: 'node_modules/next/dist/bin/next',
  19. args: 'start -p 3011',
  20. max_memory_restart: '1024M',
  21. cwd: '/data/qj_swap', // 根目录
  22. instances: 1,
  23. error_file: './logs/error.log',
  24. out_file: './logs/out.log',
  25. log_date_format: 'YYYY-MM-DD HH:mm Z',
  26. watch: true,
  27. ignore_watch: ['node_modules'],
  28. }
  29. ],
  30. }

实战 - 部署nextjs项目

文件

重要文件及文件夹说明

项目

项目放置地址 - /data

配置

nginx 配置文件地址

文件路径:/etc/nginx/conf.d

qj_swap nginx 配置文件 qj_swap.conf

  1. server {
  2. listen 8011;
  3. server_name 12.12.12.12; # 需配置为对应服务器端口
  4. index index.html;
  5. gzip on;
  6. gzip_min_length 1000;
  7. gzip_proxied expired no-cache no-store private auth;
  8. gzip_types text/plain application/x-javascript text/html text/css application/xml application/octet-stream;
  9. location / {
  10. proxy_pass http://127.0.0.1:3011;
  11. proxy_set_header Host $proxy_host;
  12. proxy_set_header X-Real-IP $remote_addr;
  13. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  14. }
  15. }

注意:12.12.12.12 - 改为对应服务端口

pm2 配置文件

文件路径: /etc/nginx/conf.d/ecosystem.config.js

参数说明

  • name : pm2名称
  • script:命令
  • args:命令参数
  • cwd:项目根目录
  1. module.exports = {
  2. apps: [
  3. {
  4. name: 'p57_swap',
  5. script: 'node_modules/next/dist/bin/next',
  6. args: 'start -p 3012',
  7. max_memory_restart: '1024M',
  8. cwd: '/data/p57_swap', // 根目录
  9. instances: 1,
  10. error_file: './logs/error.log',
  11. out_file: './logs/out.log',
  12. log_date_format: 'YYYY-MM-DD HH:mm Z',
  13. watch: false,
  14. ignore_watch: ['node_modules'],
  15. }
  16. ],
  17. }

ecosystem.config.js 为固定名称

启动方式 : pm2 start ecosystem.config.js

相关链接

pm2官网:https://pm2.keymetrics.io/

样式修改:https://styled-components.com/

实战 - 自动化部署 vitepress

获取 token - 本项目未用

token,配置github actions 权限
image.png

配置 Repository secrets

port 端口默认 22
image.png

配置 workflows

.github/workflows/main.yml

  1. # 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
  2. # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
  3. name: tk-notes-ci/cd
  4. on:
  5. push:
  6. branches: [master]
  7. # pull_request:
  8. # branches: [master]
  9. jobs:
  10. build:
  11. runs-on: ubuntu-latest
  12. strategy:
  13. matrix:
  14. node-version: [16.x]
  15. steps:
  16. # download source
  17. - name: Checkout
  18. uses: actions/checkout@master
  19. # Install & Build
  20. - name: Build
  21. uses: actions/setup-node@master
  22. - run: |
  23. yarn install
  24. yarn build
  25. # copy dist
  26. - name: Upload
  27. uses: appleboy/scp-action@master
  28. with:
  29. host: ${{ secrets.HOST }}
  30. username: ${{ secrets.USERNAME }}
  31. password: ${{ secrets.PASSWORD }}
  32. port: ${{ secrets.PORT }}
  33. source: './docs/.vitepress/dist/*'
  34. target: '/data/tk-notes'
  35. strip_components: 3 # 去掉 3 层路径, /data/tk-notes/docs/.vitepress/dist/* => /data/tk-notes/*
  36. # - name: Deploy
  37. # uses: appleboy/ssh-action@master
  38. # with:
  39. # host: ${{ secrets.HOST }}
  40. # username: ${{ secrets.USERNAME }}
  41. # password: ${{ secrets.PASSWORD }}
  42. # port: ${{ secrets.PORT }}
  43. # script: |
  44. # cd /data/tk-notes
  45. # yarn install
  46. # yarn build
  47. # pm2 start yarn --name tk-docs -- serve

相关链接

GitHub Actions 入门教程
appleboy/scp-action@master