ubuntu服務器從零開始搭建 nginx https node pm2 postgres 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]"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章