加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux 环境安装 Node与nginx

发布时间:2020-12-13 23:36:35 所属栏目:Linux 来源:网络整理
导读:Linux 环境安装 使用 nvm 安装 Node wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash# 激活nvmsource ~/.nvm/nvm.sh# 安装nodenvm install node# 切换到该版本nvm use node 使用 nvm 管理 node 版本 # 安装某版本 比如 4

Linux 环境安装

使用 nvm 安装 Node

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

# 激活nvm
source ~/.nvm/nvm.sh

# 安装node
nvm install node

# 切换到该版本
nvm use node

使用 nvm 管理 node 版本

# 安装某版本 比如 4.2.2
nvm install 4.2.2

# 列出远程服务器上所有可用版本
nvm ls-remote

# 在不同版本间切换
nvm use 4.2.2

#安装最新版 Node
nvm install node

#切换到最新版
nvm use node

#列出已安装实例
nvm ls

#安装最新不稳定版本
nvm install unstable

安装 nginx

# 新环境需要gcc gcc-c++
yum install -y gcc gcc-c++ make

# 下载 openssl 以支持 ssl 功能
# http://www.openssl.org/
wget https://www.openssl.org/source/openssl-1.1.1.tar.gz

# 下载 zlib 支持 gzip
# http://www.zlib.net/
wget http://www.zlib.net/zlib-1.2.11.tar.gz

# 下载 pcre 支持 rewrite
# http://www.pcre.org/
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz

# 下载 nginx 
# http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.16.0.tar.gz

# 解压
tar zxvf openssl-1.1.1.tar.gz
tar zxvf zlib-1.2.11.tar.gz
tar zxvf pcre-8.43.tar.gz
tar zxvf nginx-1.16.0.tar.gz

# 安装PCRE库
cd /usr/local/pcre-8.43
./configure
make && make install

# 安装SSL库
cd /usr/local/openssl-1.1.1
./config
make && make install

# 安装zlib库
cd /usr/local/zlib-1.2.11
./configure
make && make install

# 将nginx目录重命名
mv nginx-1.16.0 nginx; cd nginx/

# 安装nginx
./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.43 --with-zlib=/usr/local/zlib-1.2.11 --with-openssl=/usr/local/openssl-1.1.1

make && make install

# 配置开机启动
cd /lib/systemd/system
vim nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

# 保存退出
systemctl enable nginx.service

# 常用命令
systemctl start nginx.service  # 启动,也可以使用sbin/nginx启动
systemctl stop nginx.service  # 结束nginx 
systemctl restart nginx.service  # 重启,可使用sbin/nginx -s reload

# 配置文件
vim /usr/local/nginx/conf/nginx.conf

# 启动服务
# 启动
/usr/local/nginx/sbin/nginx
# 重启 
/usr/local/nginx/sbin/nginx -s reload
# 停止
/usr/local/nginx/sbin/nginx -s quit
/usr/local/nginx/sbin/nginx -s stop
# 查看运行状态
ps -ef|grep nginx

修改配置文件

user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    # 开启gzip
    gzip  on;
    gzip_buffers 32 4k;
    gzip_comp_level 4;
    gzip_min_length 400;
    gzip_types text/plain application/xml application/javascript;
    gzip_vary on;
    
    server {
        listen       80;
        server_name  www.abc.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/www/civ;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html?$query_string;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    # ssl
    server {
        listen 443;
        server_name test.abc.com;
        ssl on;
        ssl_certificate /etc/ssl/server.crt;
        ssl_certificate_key /etc/ssl/server.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        location / {
            root /home/www/test;
            index index.html;
        }
    }
    
    # 80 sub site
    server {
        listen 80;
        server_name sub.abc.com;

        location / {
            root /home/www/sub;
            index index.html;
            try_files $uri $uri/ /index.html?$query_string;
        }
    }
    
    # proxy go-service
    server {
        listen 80;
        server_name api.abc.com;

        charset utf-8;
        access_log /home/www/go/src/api.abc.com.access.log;

        location /(css|js|fonts|img)/ {
            access_log off;
            expires 1d;
            root "/home/www/go/src/static";
            try_files $uri @backend;
        }

        location / {
            try_files /_not_exists_ @backend;
        }

        location @backend {
            proxy_set_header X-Forwarded-For $remote_addr;

            proxy_set_header Host            $http_host;

            proxy_pass http://127.0.0.1:8080;
        }
    }
}

报错:

# 1. 如果启动遇到下面的错误
[[email?protected] nginx]# /usr/local/nginx/sbin/nginx
nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
2019/07/30 16:00:41 [emerg] 8243#0: open() "/usr/local/nginx/logs/access.log" failed (2: No such file or directory)

# 可能是没有logs目录导致
# 在nginx目录下新建一个logs文件夹就可以了
mkdir logs

未完待续 最后更新 2019-7-30 17:35:22

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读