如何在云服务器上部署nodejs生产环境

58次阅读

第一步 : 申请服务器
1、我的是腾讯云的轻量应用服务器 :
服务器配置 :1 核 2G,5Mpbs 带宽 , 系统盘 50GSSD 云盘 , 系统镜像 CentOS 8.0 64bit;
2、重置服务器密码
如何在云服务器上部署nodejs生产环境插图
如何在云服务器上部署nodejs生产环境插图1
用户名采用系统默认 root, 然后输入密码保存。
3、购买域名 , 并正常备案和解析 , 如test.cn

第二步 :nginx 安装
1、在 Xshell 输入上一步的设置的用户名及密码 , 连接服务器。
2、下载 nginx 压缩包
cd 到 /usr/local 目录下

cd /usr/local 
wget http://nginx.org/download/nginx-1.21.3.tar.gz 

3、配置 nginx 安装所需的环境
1) 安装 gcc
安装 nginx 需要先将官网下载的源码进行编译 , 编译依赖 gcc 环境。安装指令如下 :

yum install gcc-c++ 

2) 安装 PCRE pcre-devel
Nginx 的 Rewrite 模块和 HTTP 核心模块会使用到 PCRE 正则表达式语法。这里需要安装两个安装包 pcre 和 pcre-devel。第一个安装包提供编译版本的库 , 而第二个提供开发阶段的头文件和编译项目的源代码。安装指令如下 :

yum install -y pcre pcre-devel 

3) 安装 zlib
zlib 库提供了开发人员的压缩算法 , 在 Nginx 的各种模块中需要使用 gzip 压缩。安装指令如下:

yum install -y zlib zlib-devel 

4) 安装 Open SSL
nginx 不仅支持 http 协议 , 还支持 https( 即在 ssl 协议上传输 http), 如果使用了 https, 需要安装 OpenSSL 库。安装指令如下 :

yum install -y openssl openssl-devel 

4、解压 nginx 压缩包并安装
1) 解压指令如下 :

tar -xzf nginx-1.21.3.tar.gz 

2)cd 到 nginx-1.21.3

cd nginx-1.21.3 

3) 然后进行配置 , 推荐使用默认配置 , 直接./configure 就好了 :

./configure 

额外说明 : 如果需要开始 https 支持 , 这里请不要直接执行./configure, 即不要直接执行该脚本 , 而是在该脚本后面加上 SSL 模块 , 请执行如下命令替代 ./confingure

./configure --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module 

另外需要添加 SSL 证书并修改 nginx.conf 配置文件即可
5、编译安装 nginx
1) 首先在当前目录 (cd /usr/local/nginx-1.21.3) 进行编译。输入 make 即可

make 

这一步可能会报错 , 解决方法 :点这里
2) 编译成功之后 , 就可以安装了 , 输入以下指令 :

make install 

这一部可能会报错 , 解决方法 :点这里
5、启动 nginx
进入 cd /usr/local/nginx/sbin 目录 , 输入./nginx 即可启动 nginx

./nginx 

关闭 nginx

./nginx -s quit 或者 ./nginx -s stop 

重启 nginx

./nginx -s reload 

查看 nginx 进程

ps aux|grep nginx 

设置 nginx 开机启动 , 只需在 rc.local 增加启动代码即可。

vim /etc/rc.local 

然后在底部增加 /usr/local/nginx/sbin/nginx

此外 , 进入 /usr/local/nginx/conf 目录可修改 nginx 的配置文件 nginx.conf, 譬如修改域名以及端口等 , 记得修改后重启 nginx

cd /usr/local/nginx/conf 
vim nginx.conf 

第三步 : 安装 nodejs
1、同样我们需要选定安装的环境

cd /usr/local/src 

2、下载 nodejs 新的 bin 包

wget https://nodejs.org/dist/v10.14.2/node-v10.14.2-linux-x64.tar.xz 

3、然后解压它

xz -d node-v10.14.2-linux-x64.tar.xz 
tar -xf node-v10.14.2-linux-x64.tar 

4、这一步很关键啊 , 因为这一步是为了让它能够全局调用 , 相当于 Windows 里面的环境变量设置

ln -s /usr/local/src/node-v10.14.2-linux-x64/bin/node /usr/bin/node 
ln -s /usr/local/src/node-v10.14.2-linux-x64/bin/npm /usr/bin/npm 

5、我们来测试一下

node -v npm –v 

第四步 : 搭建项目
1、通过 WinSCP 连接我们申请的云服务器 , 然后进入到 root 文件夹下面 ( 我是一般把项目文件都放在 root 下面 , 如下图每一个文件夹都是一个项目 )
如何在云服务器上部署nodejs生产环境插图2
2、运行项目 , 比如 www.test.cn 项目运行在 3000 端口
linux 杀死所有 nodejs 进程

pkill node 

让程序在后台运行 , 参考 :Linux 下 nohup masterha_manager 关闭窗口进程消失

3、利用 Xshell 工具 cd 到 /usr/local/nginx/conf, 打开 nginx.conf 文件

cd /usr/local/nginx/conf 
vim nginx.conf 

添加以下代码 , 重启 nginx

server { listen 80; server_name test.cn www.test.cn; location / { proxy_pass http://127.0.0.1:3000; root blog; } } 

4、在浏览器打开 http://www.test.cn
第五步 : 配置 ssl 证书 , 实现 https 访问
1、首先要注意的是先完成
第二步 -> 3 点 -> 4) 安装 Open SSL

yum install -y openssl openssl-devel 

第二步 -> 4 点 -> 3) 配置成如下命令替代 ./confingure

./configure --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module 

2、申请 ssl 证书并上传到服务器 , 我在服务器 /usr/local/nginx 目录下建 cert 文件夹存放 ssl 证书
如何在云服务器上部署nodejs生产环境插图3
3、第四步 -> 3 的 nginx.conf 配置成如下

 server { listen 443 ssl; server_name www.test.cn; ssl_certificate /usr/local/nginx/cert/www.pem; ssl_certificate_key /usr/local/nginx/cert/www.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $http_addr; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #以上三行 , 目的是将代理服务器收到的用户的信息传到真实服务器上 root /root/www.test.cn/static; index index.html index.htm; } } 

4、重启 nginx

cd /usr/local/nginx/sbin ./nginx -s reload 

4、在浏览器打开https://www.test.cn
参考 :Linux 下 nginx 的安装以及环境配置

原文链接:https://blog.csdn.net/weixin_43422861/article/details/122910068

正文完
 
追风者
版权声明:本站原创文章,由 追风者 2023-12-30发表,共计4119字。
转载说明:声明:本站内容均来自互联网,归原创作者所有,如有侵权必删除。 本站文章皆由CC-4.0协议发布。