Linux 部署 Node.js 环境

Linux 部署 Node.js 环境

Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境,用来方便快速地搭建易于扩展的网络应用。本文适用服务器系统:

  • CentOS 7
  • Alibaba Cloud Linux 2
  • Alibaba Cloud Linux 3

操作步骤

1.远程连接 ECS 实例

  • ssh 链接
  • 通过 Workbench 远程连接
  • 通过客户端连接

2.安装应用

2.1 安装 Nginx
yum -y install nginx
nginx -v
2.2 安装 GIT
yum install git -y

如要安装最新版本 git,请参考Centos7 安装最新版本 git

2.3 安装 Node.js
yum install nodejs -y
yum install npm -y

3.命令行美化

3.1 安装 zsh 和 oh-my-zsh

安装 zsh

yum install zsh

切换默认 shell

chsh -s /bin/zsh

安装 oh-my-zsh

git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
3.2 语法高亮设置

安装

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

配置

在 ~/.zshrc 的 plugins 中加入 zsh-syntax-highlighting

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git zsh-syntax-highlighting)
3.3 自动补全插件设置

安装

git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions

配置

在 ~/.zshrc 的 plugins 中加入 zsh-syntax-highlighting

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git zsh-syntax-highlighting zsh-autosuggestions)
3.4 重启 shell 验证配置是否生效

配置完成后需要重新启动 shell 或者新开 shell 窗口。

4.部署 Node.js 应用

根据个人习惯创建应用目录

cd ~/home
mkdir apps
cd apps

拉取项目代码

git clone YOUR_APP_GIT

运行项目(根据项目具体情形安装依赖运行项目)

npm run start
5.Nginx 配置

编辑 nginx 配置文件

vi /etc/nginx/nginx.conf

https 域名转发配置规则,不需要开启 https 协议将 ssl 配置项注释即可

  server {
    listen 80;
    listen       443 ssl;
    server_name  api.example.com;
    index  index.php index.html index.htm;

    #SSL配置
    #ssl on;
    ssl_certificate      /path/to/example.com.pem; # 配置证书,填写证书绝对路径
    ssl_certificate_key  /path/to/example.com.key; # 配置证书私钥,填写证书绝对路径
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    location / {
      proxy_pass  http://127.0.0.1:3000; # 转发规则,转发到Node应用,服务器需要开启对应的端口号
      proxy_set_header Host $proxy_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }

你可能感兴趣的:(Linux 部署 Node.js 环境)