Next JS и Nginx

Всем привет! Решил сделать небольшую инструкцию как запустить проект Next JS на сервере. Я использовал Ubuntu 22.04 и Nginx.

1. Инсталим nginx

1

2

sudo aptget update

sudo aptget install nginx

редактируем nginx/sites-available/default

1

2

3

4

5

6

7

8

9

10

11

server{

  server_name domen.ru;

  location/{

    include proxy_params;

    

    proxy_pass http://127.0.0.1:3000;

  }

  listen80;

}

2. Инсталим Node JS

1

2

3

4

sudo apt install curl

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

source~/.profile

install18.4.0

3. Инсталим и настраиваем GIT

1

2

3

4

5

6

7

apt install git

git init

git remote add origin https://наш_гит.ru/репа.git

git configglobalcredential.helper store

git fetch

git pull origin master

4. Авто старт

Создаем службу /etc/systemd/system/next.service

1

2

3

4

5

6

7

8

9

10

11

[Unit]

Description=Next JS

After=network.target

  

[Service]

User=root

ExecStart=/var/www/наш_проект/start.sh

Restart=always

  

[Install]

WantedBy=multiuser.target

В каталоге с проектом создаем файлик start.sh

1

2

3

4

5

6

7

#!/bin/bash

export NVM_DIR=«$HOME/.nvm»

[s«$NVM_DIR/nvm.sh»]&&\.«$NVM_DIR/nvm.sh»  # This loads nvm

[s«$NVM_DIR/bash_completion»]&&\.«$NVM_DIR/bash_completion»  # This loads nvm bash_completion

cd/var/www/наш_проект/

/root/.nvm/versions/node/v18.4.0/bin/npm run start

5. Автодеплой

Создаем deploy.sh, его можно запускать через webhook того же gitea.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#!/bin/sh

export NVM_DIR=«$HOME/.nvm»

[s«$NVM_DIR/nvm.sh»]&&\.«$NVM_DIR/nvm.sh»  # This loads nvm

[s«$NVM_DIR/bash_completion»]&&\.«$NVM_DIR/bash_completion»  # This loads nvm bash_completion

cd/var/www/наш_проект/

sete

echo«Deploying application …»

sudo systemctl stop next

echo«Get from git»

git pull origin master

echo«Building «

/root/.nvm/versions/node/v18.4.0/bin/npm install

/root/.nvm/versions/node/v18.4.0/bin/npm run build

sudo systemctl start next

echo«Application deployed!»