Skip to content

Instantly share code, notes, and snippets.

@doKill
Last active August 12, 2020 07:48
Show Gist options
  • Save doKill/82b3a6e8ec58066d9c09639aaac845c0 to your computer and use it in GitHub Desktop.
Save doKill/82b3a6e8ec58066d9c09639aaac845c0 to your computer and use it in GitHub Desktop.
配node环境

使用nvm安装

  1. 安装nvm命令如下,可以根据目前nvm最新版本进行安装,以v0.35.3为例
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh | bash
    or Wget:
    wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh | bash
    然后 source ~/.bashrc
  2. 通过命令 nvm --version 可查看版本,说明安装成功
  3. 通过 nvm ls-remote 可查看node所有版本
  4. 通过 nvm install <version> (版本号) 例如:nvm install v10.6.0
  5. 安装后可通过node -v ,npm -v 查看版本,说明安装成功

安装NGINX服务器
cd / && rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 在根目录添加CentOS7 Nginx的yum软件库
yum install nginx 安装
systemctl start nginx.service 运行
systemctl enable nginx.service 添加系统引导时启动,这时nginx服务器已经开始运行了,在浏览器输入IP或域名,就能看到nginx欢迎页面了,若看不到注意下面

查端口80的防火墙是否打开,若开的话执行第二句

firewall-cmd --query-port=80/tcp  
firewall-cmd --add-port=80/tcp

装其余软件

yum -y install git
npm i express pm2 -g

配置Nginx代理
vi /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  45.32.141.68;#ip或域名
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
	    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_pass http://127.0.0.1:3000;#端口为node启动时的端口
	    proxy_redirect off;
        #root   /usr/share/nginx/html;
        #index  index.html index.htm;
    }
}

开启nginx gzip
vi /etc/nginx/nginx.conf   填入以下信息

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript application/javascript text/javascript text/css application/xml application/x-httpd-php image/jpeg image/gif image/png;
gzip_disable "MSIE [1-6]\.";

nginx -s reload  

启动服务   进入对应的项目pm2 start app.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment