Ubuntu 18.04 + Nuxt 2 + Nginx + PM2 설정
0. 환경
sudo npm install -g npm
sudo hash -d npm
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs
- Ubuntu 18.04(AWS EC2의 기본 AMI)
- Nuxt 2.13
- Nginx 1.10
- PM2
코드는 git으로 관리한다.
1. 패키지 설치
git clone 후 npm install로 패키지를 설치하려고 하면 nuxt가 설치되지 않는다.
기본으로 설치되어 있는 npm 버전(3.5.2)이 낮기 때문이라고 한다.
https://askubuntu.com/questions/562417/how-do-you-update-npm-to-the-latest-version를 참조해서 버전을 업그레이드 해준다.
npm -v 로 확인해보면 여전히 3.5.2인데 bash cache가 남아있어서 그렇다고 한다.
https://askubuntu.com/questions/1036278/npm-is-incorrect-version-on-latest-ubuntu-18-04-installation를 참조해서 캐시를 지운다.
이후 다시 패키지를 설치하면 정상적으로 설치된다.
sudo npm install
2. nuxt 실행
뭐하나 넘어가는 법이 없다. nuxt가 실행되지 않으면 node 버전을 업그레이드한다.
8.xx 이상이면 된다고 했던것 같은데 왜 업그레이드해야하는지는 모르겠다.
이전에도 참고했던 https://github.com/nodesource/distributions/blob/master/README.md를 보고 10.xx로 업그레이드한다.
sudo apt-get install -y nodejs
추가로 npm run dev는 되는데 빌드 후 npm start가 안된다면 nuxt.config.js의 target이 server인지 확인해보자.(https://nuxtjs.org/api/configuration-target 참고)
+210910 수정
"An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://dl.yarnpkg.com/debian"
이 오류 발생 시 아래 페이지 참조
https://askubuntu.com/questions/1306111/why-yarn-key-update-failes-all-the-time-gpg-error-https-dl-yarnpkg-com-debia
"The repository 'http://apt.postgresql.org/pub/repos/apt trusty-pgdg Release' does not have a Release file."
이 오류 발생 시
/etc/apt/source.list랑 /etc/apt/source.list.d/postgresql.list 파일에서 trusty-pgdg 가 있는 라인을 주석처리하든지 지우면 된다.
3. nginx 설정
사이트 주소로 들어오면 3000포트로 연결하도록 설정하면 된다.
nginx경로/sites-enabled/default(파일 이름 변경 가능)에 아래 내용을 추가/수정했다.
listen 80 default_server;
listen [::]:80 default_server;
server_name 서버이름;
charset utf-8;
add_header Access-Control-Allow-Headers Origin,X-Requested-With,Content-Type,Accept;
사이트 주소로 들어오면 3000포트로 연결하도록 설정하면 된다.
nginx경로/sites-enabled/default(파일 이름 변경 가능)에 아래 내용을 추가/수정했다.
listen 80 default_server;
listen [::]:80 default_server;
server_name 서버이름;
charset utf-8;
add_header Access-Control-Allow-Headers Origin,X-Requested-With,Content-Type,Accept;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
4. pm2
아래 명령어로 pm2를 설치한다
npm install pm2 -g
1) 설정파일 없이 실행
프로젝트 루트 디렉터리에서 아래 명령어를 입력하면 dev 모드로 돌아간다
pm2 start "npm run dev" --name 이름
이후에는 pm2 start 이름만 하면 알아서 dev 모드로 돌아간다.
production 모드로 실행시키고 싶으면 빌드 후
pm2 start "npm start" --name 이름
2) 설정파일에 정의
ecosystem.config.js 라는 이름의 파일을 만든다
module.exports = {
apps: [
{
name: '이름',
script: 'npm'
}
]
}
dev 모드로 돌리고 싶으면 pm2 start 이름 -- run dev, production 모드로 돌리고 싶으면 빌드 후 pm2 start 이름
5. 기타 에러
1) ERR_CONTENT_LENGTH_MISMATCH
실행하다가 아무 짓도 안했는데 이 에러가 발생하면 인스턴스 용량이 다 차서 그런 걸 수 있다.