Skip to content
On this page

部署上线

安装node

koa2项目部署在阿里云服务器

Ubantu

在安装前先更新 apt-get
sudo apt-get update

更新完毕就可以开始安装nodejs
sudo apt-get install nodejs

centos

node下载地址http://nodejs.cn/download/

选一个下载目录
 cd /usr/local/src/
下载
 wget https://npm.taobao.org/mirrors/node/v14.15.5/node-v14.15.5-linux-x64.tar.xz

如果提示y/n? 你一路y到底

如果没有解压工具,先安装解压文件插件,有则跳过,一般有的
yum install -y tar
 
解压文件
 tar -xvf node-v14.15.5-linux-x64.tar.xz
改名
 mv node-v14.15.5-linux-x64  nodejs 

确认一下nodejs下bin目录是否有node和npm文件,如果有执行软连接
  --1、软链接就是:ln –s 源文件 目标文件,
  --只会在选定的位置上生成一个文件的镜像,不会占用磁盘空间,类似与windows的快捷方式。
  --2、硬链接ln源文件目标文件,没有参数-s,
  --会在选定的位置上生成一个和源文件大小相同的文件,无论是软链接还是硬链接,文件都保持同步变化。
  ln -sv /usr/local/src/nodejs/bin/npm   /usr/local/bin/ 
  ln -sv /usr/local/src/nodejs/bin/node   /usr/local/bin/
 
验证nodejs是否安装好
 node -v
配置npm 镜像地址
 查看当前镜像:
 `npm config get registry`

下载淘宝镜像
 npm install -g cnpm --registry=https://registry.npm.taobao.org

 切换镜像
 npm config set registry https://registry.npm.taobao.org 
 
回车直接设置成功,没反应的

再查看是否切换成功即可
npm config get registry

成功

安装mysql5.7.24

centos

Linux下安装mysql-5.7.24

ERROR! The server quit without updating PID file

有问题的,没成功

cpp
检查mysql用户组和用户是否存在,如果没有,则创建
cat /etc/group | grep mysql
cat /etc/passwd |grep mysql
groupadd mysql
useradd -r -g mysql mysql

找个目录
cd /usr/local/src/ 
下载
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
解压
tar xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
  
移动解压好的文件夹到/usr/local,并命名为mysql
mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql
  
  如果/usr/local/下已经存在mysql
  mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/
  cd /usr/local/
  mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql
  
/usr/local/mysql目录下创建data目录
mkdir /usr/local/mysql/data
  
cd /usr/local/mysql/bin
./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
最后一行root@localhost的临时密码记一下,比如sqW<kCBij5aa 
  启动服务
/usr/local/mysql/support-files/mysql.server start
  报错:mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log'
  可能没有mariadb.log文件
    mkdir /var/log/mariadb
    touch /var/log/mariadb/mariadb.log
    chown -R mysql:db /var/log/mariadb/
      重新启动试试
      /usr/local/mysql/support-files/mysql.server start
      继续报错ERROR! The server quit without updating PID file
      无解,还是用小皮phpstudy吧
      
  软链接
ln -sv /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
  ln -sv /usr/local/mysql/bin/mysql /usr/bin/mysql

安装 pm2

npm install pm2 -g

pm2 -v

提示 pm2 command not found

表示pm2没有加入环境变量。

此时需要配置好pm2环境变量。

ln -sv   /usr/local/src/nodejs/bin/pm2   /usr/local/bin

安装:npm i pm2 -g

使用pm2启动项目,在终端中输入命令:pm2 start app.js --name zidingyimingzi

查看项目列表命令:pm2 ls

重启项目:pm2 restart 自定义名称

停止项目:pm2 stop 自定义名称

删除项目:pm2 delete 自定义名称

nginx(windows)

http://tengine.taobao.org/nginx_docs/cn/docs/windows.html

前期准备

官网下载解压http://nginx.org/en/download.html

去到解压的目录,start nginx.exe

查看任务进程是否存在,dos或打开任务管理器都行

tasklist /fi "imagename eq nginx.exe"

如果失败,可能是80端口被占用,nginx.confog里面修改

nginx -s stop快速退出
nginx -s quit优雅退出
nginx -s reload更换配置,启动新的工作进程,优雅的关闭以往的工作进程
nginx -s reopen重新打开日志文件

报错没有nginx.pid,任务管理器把nginx.exe全部结束,重新 start nginx ,就会生成logs/nginx.pid

修改完成后保存,使用以下命令检查一下配置文件是否正确,后面是nginx.conf文件的路径,successful就说明正确了

nginx -t -c /nginx-1.15.2/conf/nginx.conf

代理部署项目

项目build好

配置ngnix的配置文件nginx.conf,然后重启运行即可

server
{
   listen 3003;
   server_name localhost;
   ##  所有3003请求发给5500
   location = / {
       proxy_pass http://localhost:5500;
   }
   ##  所有3003/no开头的请求转去3000/no
   location /no {
       proxy_pass http://localhost:3000;
   }
   ##  所有3003/ok/的请求转去3000/ok/
   location /ok/ {
       proxy_pass http://localhost:3000;
   }
}

上面代码的意思是将localhost:3003转发为location:5500,

也就是说现在访问localhost:3003实际上是访问location:5500,

而访问localhost:3003/no则是访问localhost:3000,并以no开头的url

nginx(linux)

Linux 部署vue项目(使用nginx)

nginx解决跨域问题

nginx安装

默认安装目录cd /usr/local/nginx/

打包文件推荐路径:mkdir -p  /usr/local/webapp/dist

#查看运行
ps -ef | grep nginx

查找主配置文件
/usr/sbin/nginx -t

可用查找配置文件路径
cat /etc/nginx/nginx.conf 

配置文件在
cd /usr/local/nginx/conf/nginx.conf
vim nginx.conf

前端

原理:

数据在3000端口,你访问nginx的3002,nginx给你访问3000

javascript
$(document).ready(function () {
  $('#get').click(function () {
    $.ajax({
      url:'http://localhost:3002/ok',
      //  带cookies的请求
      xhrFields:{
        withCredentials:true
      },
      success:function(res) {
        console.log("success",res)
      },
      error:function(err) {
        console.log('fail',err)
      }
    })
  })
})

nginx.conf

vim 你的 nginx.conf
server
{
    listen 3002;
    server_name localhost;
    # 注意设定 root路径是有dist的
    location / {
      root /usr/local/webapp/dist;
      index /index.html;
    }

    #跨域 ip和port自行替换
    location /adminApi {
      proxy_pass http://ip:port;
   }
    location /ok {
        proxy_pass http://localhost:3000;

        #   指定允许跨域的方法,*代表所有
        add_header Access-Control-Allow-Methods *;

        #   预检命令的缓存,如果不缓存每次会发送两次请求
        add_header Access-Control-Max-Age 3600;
        
        #   带cookie请求需要加上这个字段,并设置为true
        add_header Access-Control-Allow-Credentials true;

        #   表示允许这个域跨域调用(客户端发送请求的域名和端口) 
        #   $http_origin动态获取请求客户端请求的域   不用*的原因是带cookie的请求不支持*号
        add_header Access-Control-Allow-Origin $http_origin;

        #   表示请求头的字段 动态获取
        add_header Access-Control-Allow-Headers 
        $http_access_control_request_headers;

        #   OPTIONS预检命令,预检命令通过时才发送请求
        #   检查请求的类型是不是预检命令
        if ($request_method = OPTIONS){
            return 200;
        }
    }
}

使配置生效

sbin/nginx -s reload
sbin/nginx -s stop
sbin/nginx

管理站点

宝塔

https://blog.csdn.net/weixin_47472689/article/details/112269731

yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh
粘贴入命令行后回车,所有选择全部yes

https://www.cnblogs.com/yingyigongzi/p/13926828.html

小皮phpstudy

添加网站,要加端口,端口要在安全组放行,然后把文件上传对应文件夹即可

phpstudy添加网站

自动部署

cli3自动部署项目

https://segmentfault.com/a/1190000038751033?utm_source=tag-newest

jenkis 部署 命令 脚本

#!/bin/bash

APP_NAME="my-app"

ssh -tT test@172.22.33.44 <<\EOF
  cd /usr/local/htdocs/$APP_NAME || { echo "Failed to change directory"; exit 1; }
  git reset --hard || { echo "Git reset failed"; exit 1; }
  git clean -df || { echo "Git clean failed"; exit 1; }
  git pull || { echo "First git pull failed"; exit 1; }
  git checkout master || { echo "Git checkout failed"; exit 1; }
  git pull || { echo "Second git pull failed"; exit 1; }
  git status || { echo "Git status check failed"; exit 1; }
  npm install --loglevel verbose || { echo "npm install failed"; exit 1; }
  npm run build || { echo "npm build failed"; exit 1; }
  pm2 stop $APP_NAME || { echo "pm2 stop failed"; exit 1; }
  pm2 delete $APP_NAME || { echo "pm2 delete failed"; exit 1; }
  pm2 start npm --name $APP_NAME -- run start || { echo "pm2 start failed"; exit 1; }
  pm2 restart $APP_NAME || { echo "pm2 restart failed"; exit 1; }
EOF
ssh -tT test@12.3.4.5 <<\EOF
  cd /usr/local/xxx/www
  ## pm2 start npm --name www.cn -- run test
  git clean -df
  git checkout .
  git pull
  git checkout main
  git pull
  npm install
  npm run gulp
  npm run build-test
EOF

pm2

npm脚本
"build": "pm2 start ecosystem.config.js --env production"

ecosystem.config.js

module.exports = {
  apps: [
    {
      name: "www.cn",
      script: "./server/www.js",
      watch: true,
      // instances:"max",
      env: {
        "PORT": 3000,
        "NODE_ENV": "development"
      },
      env_test: {
        "PORT": 3000,
        "NODE_ENV": "test",
      },
      env_production: {
        "PORT": 3000,
        "NODE_ENV": "production",
      }
    }
  ]
};

server/www.js

js
const Koa = require('koa')
const path = require('path')
const koaBody = require('koa-body')
const bodyParser = require('koa-bodyparser')
const cacheControl = require('koa-cache-control')
const views = require('koa-views')
const static = require('koa-static')
const mount = require('koa-mount')
const convert = require('koa-convert')
const i18n = require('koa-i18n')
const cors = require('koa-cors')
const locale = require('koa-locale')
const routers = require('./routes')
const config = require('./config')

const app = new Koa()

app.use(bodyParser())

// 配置模版引擎中间件
app.use(views('views', {
  map: { html: 'ejs' }
}))

// 静态资源目录对于相对入口文件index.js的路径
var staticPath = '../dist'
if (process.env.NODE_ENV === 'development') {
  staticPath = '../static'
}

app.use(mount('/', static(path.join( __dirname,  staticPath)) ))

app.use(koaBody({
  multipart: true,
  formidable: {
    maxFileSize: 2000 * 1024 * 1024 // 设置上传文件大小最大限制,默认2M
  }
}))

// 处理跨域
app.use(convert(cors()))

// 初始化路由中间件
app.use(routers.routes(1)).use(routers.allowedMethods(2))

app.listen(config.listen_port, () => {
  console.log(`http://localhost:${config.listen_port}`)
})

粤ICP备2024285819号