nodejs+express 部署到Linux服务器
<h2>一,部署整体流程</h2><ol>
<li>本地express项目传输到Linux服务器上(没有上传node_module)</li>
<li>在服务器上安装node_module包</li>
<li>使用pm2管理node进程 </li>
</ol>
<h2>二,部署过程</h2>
<p> 1. 查看Linux上的node版本,一般服务器自带版本都比较低 </p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">node -v </pre>
</div>
<p> v6.17.1版本太低了,需要升级版本到V10以上</p>
<p> 2. 升级node版本</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># 清除node缓存
npm cache clean --force
# 安装n模块管理node版本
npm install -g n
# 查看n版本
n -V
# 安装node最新版本
n latest
# 查看安装后的版本
node -v
</pre>
</div>
<p> 3. 安装pm2模块</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">npm install -g pm2
</pre>
</div>
<p> 4. 进入express项目根目录下,启动项目</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># 启动项目下的www文件,并命名进程
pm2 start ./bin/www --name short-video-api --watch
# 查看启动日志
pm2 logs
</pre>
</div>
<p> 5. 如果启动日志没有报错,并出现启动打印信息,则启动成功,按照设定的端口访问(确定端口对外是开放的)</p>
<h4>注:还可以自定义端口启动</h4>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">pm2 start ecosystem.config.js --env production<span style="background-color: rgba(255, 255, 255, 1); font-family: "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px"> </span></pre>
</div>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">//ecosystem.config.js
module.exports = {
apps: [
{
name: 'short-video-api',
watch: true,
script: './bin/www',
env_production: {
HOST: '0.0.0.0',
PORT: 3001,
NODE_ENV: 'production',
},
},
],
}
</pre>
</div>
<p> </p>
<h2>三,服务对外支持https访问,通过nginx转发</h2>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">server {
listen 443;
server_namelocalhost;
ssl on;
root html;
index index.html index.htm;
ssl_certificate /usr/local/nginx/5220423.pem;
ssl_certificate_key/usr/local/nginx/5220423.key;
#ssl_session_cache shared:SSL:1m;
ssl_session_timeout5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
ssl_prefer_server_ciphers on;
location / {
root /var/www/html;
indexindex.html index.htm;
}
location /short-video-api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host$http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://127.0.0.1:3000;// nodejs项目端口
proxy_redirect default;
}
}
</pre>
</div>
<p> </p>
<h2>四,pm2常用命令 </h2>
<p>详情参考官网</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">$ pm2 start app.js # 启动app.js应用程序
$ pm2 start app.js -i 4 # cluster mode 模式启动4个app.js的应用实例
# 4个应用程序会自动进行负载均衡
$ pm2 start app.js --name="api" # 启动应用程序并命名为 "api"
$ pm2 start app.js --watch # 当文件变化时自动重启应用
$ pm2 start script.sh # 启动 bash 脚本
$ pm2 list # 列表 PM2 启动的所有的应用程序
$ pm2 monit # 显示每个应用程序的CPU和内存占用情况
$ pm2 show # 显示应用程序的所有信息
$ pm2 logs # 显示所有应用程序的日志
$ pm2 logs # 显示指定应用程序的日志
$ pm2 flush # 清空所有日志文件
$ pm2 stop all # 停止所有的应用程序
$ pm2 stop 0 # 停止 id为 0的指定应用程序
$ pm2 restart all # 重启所有应用
$ pm2 reload all # 重启 cluster mode下的所有应用
$ pm2 gracefulReload all # Graceful reload all apps in cluster mode
$ pm2 delete all # 关闭并删除所有应用
$ pm2 delete 0 # 删除指定应用 id 0
$ pm2 scale api 10 # 把名字叫api的应用扩展到10个实例
$ pm2 reset # 重置重启数量
$ pm2 startup # 创建开机自启动命令
$ pm2 save # 保存当前应用列表
$ pm2 resurrect # 重新加载保存的应用列表
$ pm2 update # Save processes, kill PM2 and restore processes
$ pm2 generate # Generate a sample json configuration file
</pre>
</div>
<p> </p>
<p> </p><br><br>
来源:https://www.cnblogs.com/front-web/p/15672575.html
頁:
[1]