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

第一步:申请服务器
1、我的是腾讯云的轻量应用服务器:
服务器配置:1核2G,5Mpbs带宽,系统盘50GSSD云盘,系统镜像CentOS 8.0 64bit;
2、重置服务器密码
在这里插入图片描述
在这里插入图片描述
用户名采用系统默认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下面,如下图每一个文件夹都是一个项目)
在这里插入图片描述
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证书
在这里插入图片描述
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的安装以及环境配置

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值