ubuntu服务器从零开始搭建 nginx https node pm2 postgres github

ubuntu服务器从零开始搭建

  • 1. nginx 安装及配置
    • 1.1. 安装
    • 1.2. 配置
      • 1.2.1 字体跨域
    • 1.3. 配置 Let's encrypt
  • 2. node
    • 1. node环境安装
    • 2. 主要包安装
  • 3. Postgres sql 数据库安装
  • 4. GitHub 配置

1. nginx 安装及配置

1.1. 安装

sudo apt-get update

sudo apt-get install nginx

1.2. 配置

cd /etc/nginx/sites-enabled/xxxx

upstream test_com_http_server {
  server 127.0.0.1:xxxx fail_timeout=0;
}

server {
  listen 443 ssl http2;
  ssl_certificate /home/one/xxxx/config/ssl/server.crt;
  ssl_certificate_key /home/one/xxxx/config/ssl/server.key;
  server_name xxxx.com;
  root /home/one/xxxx/html;
  index index.html index.htm;
  charset utf-8;

  location ~* ^/api {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X_FORWARDED_PROTO $scheme;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    client_max_body_size 10M;

    proxy_pass http://test_com_http_server;
  }

  gzip            on;
  gzip_comp_level 5;
  gzip_min_length 256;
  gzip_proxied    any;
  gzip_types
    application/atom+xml
    application/javascript
    application/json
    application/ld+json
    application/manifest+json
    application/rss+xml
    application/vnd.geo+json
    application/vnd.ms-fontobject
    application/x-font-ttf
    application/x-web-app-manifest+json
    application/xhtml+xml
    application/xml
    font/opentype
    image/bmp
    image/svg+xml
    image/x-icon
    text/cache-manifest
    text/css
    text/plain
    text/vcard
    text/vnd.rim.location.xloc
    text/vtt
    text/x-component
    text/x-cross-domain-policy;

}

1.2.1 字体跨域

location ~* \.(eot|ttf|woff|woff2|svg|otf)$ {
   add_header Access-Control-Allow-Origin *;
}

1.3. 配置 Let’s encrypt

  1. 添加存储库

    sudo add-apt-repository ppa:certbot/certbot

  2. 安装 nginx 软件包

    sudo apt install python-certbot-nginx

  3. 确认 nginx 成功运行

  4. 获取 ssl 证书

    sudo certbot --nginx -d example.com

  5. 自动续订

    sudo certbot renew --dry-run

原文链接

/usr/bin/certbot /usr/sbin/nginx

2. node

1. node环境安装

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

2. 主要包安装

  1. Yarn | npm 安装

    sudo npm i -g yarn
    sudo npm i -g cnpm
    

    安装完了之后

    sudo chown -R $USER:$GROUP ~/.npm
    sudo chown -R $USER:$GROUP ~/.config
    
  2. pm2 安装

    sudo npm i -g pm2

  3. pm2 使用

    pm2 start app.js -o ./log/out.log -e ./log/err.log
    NODE_ENV=production pm2 reload      
    
  4. pm2 fork

    module.exports = {
      apps : [{
        name: 'xxxx',
        script: 'app.js',
        autorestart: true,
        watch: false,
        max_memory_restart: '1G',
        out_file: './log/out.log',
        error_file: './log/err.log'
      }]
    };
    
  5. pm2 cluster

    module.exports = {
      apps : [{
        name: 'xxxx',
        script: 'app.js',
        instances: 'max',
        autorestart: true,
        watch: false,
        max_memory_restart: '1G',
        exec_mode: 'cluster',
        out_file: './log/out.log',
        error_file: './log/err.log'
      }]
    };
    

3. Postgres sql 数据库安装

  1. 安装

    sudo apt-get install postgresql

  2. 进入管理员账号

    sudo -u postgres psql

  3. 创建用户

    CREATE USER username WITH PASSWORD '****';

  4. 创建数据库

    CREATE DATABASE databasename OWNER username

  5. 赋予创建用户数据库权限

    如果上一步指定了 OWNER则可以省略

    GRANT ALL ON DATABASE databasename TO username

4. GitHub 配置

cd ~/.ssh
vi id_rsa
chmod 600 id_rsa
ssh -T [email protected]

ssh-keygen -t rsa -b 4096 -C "[email protected]"

你可能感兴趣的:(全栈)