Skip to content

Instantly share code, notes, and snippets.

@GanterPenguin
Last active September 12, 2021 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GanterPenguin/5ce98204b00c82791945e55a6e8a4539 to your computer and use it in GitHub Desktop.
Save GanterPenguin/5ce98204b00c82791945e55a6e8a4539 to your computer and use it in GitHub Desktop.

Деплой nuxt проекта на сервер

  1. cd ~/web/
  2. Склонировать проект git clone <project_link>
  3. Переходим в папку проекта cd <project_name>
  4. Устанавливаем зависимости yarn
  5. Собираем проект yarn build
  6. В папке проекта должен находится ecosystem.config.js если его нет то необходимо создать его, содержимое должно быть таким:
module.exports = {
   apps: [
      {
         name: '<project_name>_<port>',
         exec_mode: 'cluster',
         instances: 'max',
         script: './node_modules/nuxt/bin/nuxt.js',
         args: 'start',
         env: {
            NODE_ENV: 'production',
            PORT: <port>
         }
      }
   ]
}

project_name - название проекта

port - любое число 4300 - 4399, должен быть уникальным для каждого проекта

Чтобы убедиться что порт свободен вводим команду pm2 list

Пример вывода команды ниже, из него видно что порты 4300 и 4301 уже заняты (столбец name)

┌─────┬───────────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name                      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼───────────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 2   │ new.francesco.ru_4301     │ default     │ 2.15.3  │ cluster │ 121487   │ 25m    │ 343  │ online    │ 0%       │ 82.4mb   │ admin    │ disabled │
│ 3   │ new.francesco.ru_4301     │ default     │ 2.15.3  │ cluster │ 121502   │ 25m    │ 336  │ online    │ 0%       │ 79.9mb   │ admin    │ disabled │
│ 0   │ new.shoes-bags.ru_4300    │ default     │ 2.14.12 │ cluster │ 121467   │ 25m    │ 1065 │ online    │ 0%       │ 95.1mb   │ admin    │ disabled │
│ 1   │ new.shoes-bags.ru_4300    │ default     │ 2.14.12 │ cluster │ 121473   │ 25m    │ 1068 │ online    │ 0%       │ 102.2mb  │ admin    │ disabled │
└─────┴───────────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
  1. Запускаем приложение pm2 start ecosystem.config.js
  2. Обновляем список процессов pm2 pm2 save
  3. Создаем конфиг nginx nano /etc/nginx/sites-available/<project_name>.conf

Содержимое конфига:

map $sent_http_content_type $expires {
    "text/html"                 epoch;
    "text/html; charset=utf-8"  epoch;
    default                     off;
}

server {
    listen          80;             # the port nginx is listening on
    server_name     <project_domain>;    # setup your domain here

    gzip            on;
    gzip_types      text/plain application/xml text/css application/javascript;
    gzip_min_length 1000;

    location / {
        expires $expires;

        proxy_redirect                      off;
        proxy_set_header Host               $host;
        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_read_timeout          1m;
        proxy_connect_timeout       1m;
        proxy_pass                          http://127.0.0.1:<port>; # set the address of the Node.js instance here
    }
}

project_domain - Домен приложения

port - Порт который указан в ecosystem.config.js

  1. Активируем созданный конфиг

sudo ln -s /etc/nginx/sites-available/<project_name>.conf /etc/nginx/sites-enabled/<project_name>.conf

  1. Деплоим HTTPS sudo certbot --nginx -d <project_domain>

project_domain - Домен приложения

11.1. Если сертификаты были получены успешно то будет выдан запрос на создание редиректа на онли HTTPS, нажимаем 2 если нужен редирект

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment