LoginSignup
1
3

More than 1 year has passed since last update.

[nuxt.js] Amazon Linux2 への Nuxt.js の導入と systemd でのデーモン化

Last updated at Posted at 2022-02-09

インストール

Node.js のインストール

$ curl --silent --location https://rpm.nodesource.com/setup_14.x | sudo bash -
$ sudo yum install nodejs
$ node --version
v14.19.0
$ npm --version
6.14.16

vue-cli のインストール

$ sudo npm install -g @vue/cli
$ vue --version
@vue/cli 4.5.15

Nuxt.js アプリケーションの作成

$ pwd
/home/ec2-user
$ npx create-nuxt-app my-project

Nuxt.js アプリケーションの build と動作確認

$ pwd
/home/ec2-user/my-project
$ npm run build
$ npm run start

> my-project@1.0.0 start /home/ec2-user/my-project
> nuxt start


   ╭───────────────────────────────────────╮
   │                                       │
   │   Nuxt @ v2.15.2                      │
   │                                       │
   │   ▸ Environment: production           │
   │   ▸ Rendering:   server-side          │
   │   ▸ Target:      static               │
   │                                       │
   │   Listening: http://localhost:3000/   │
   │                                       │
   ╰───────────────────────────────────────╯

ℹ Serving static application from dist/                    
$ curl -i http://localhost:3000/
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: public, max-age=0
Last-Modified: Thu, 09 Dec 2021 04:12:21 GMT
ETag: W/"227ef-17d9d646088"
Content-Type: text/html; charset=UTF-8
Content-Length: 141295
Vary: Accept-Encoding
Date: Wed, 09 Feb 2022 00:34:50 GMT
Connection: keep-alive
Keep-Alive: timeout=5

<!doctype html>
<html data-n-head-ssr>
  <head>
 :

systemd でのデーモン化

以下のファイルを /etc/systemd/system/nuxt.service として作成

/etc/systemd/system/nuxt.service
[Unit]
Description=nuxt app # アプリケーションの説明(任意)
After=syslog.target network.target

[Service]
Type=simple
ExecStart=/usr/bin/npm run start
WorkingDirectory=/home/ec2-user/my-project
KillMode=process
Restart=always
User=ec2-user
Group=ec2-user

[Install]
WantedBy=multi-user.target

上記のファイルの内容を修正した場合は daemon-reload すること

$ sudo systemctl daemon-reload

起動、終了、再起動

systemctl コマンドで実施する

起動
$ sudo systemctl start nuxt.service
終了
$ sudo systemctl stop nuxt.service
再起動
$ sudo systemctl restart nuxt.service
自動起動登録
$ sudo systemctl enable nuxt.service

稼働状況の確認

動作しているかの確認は systemctl status

状況確認
$ sudo systemctl status nuxt.service
● nuxt.service - nuxt app
   Loaded: loaded (/etc/systemd/system/nuxt.service; enabled; vendor preset: disabled)
   Active: active (running) since 水 2022-02-09 09:44:07 JST; 8s ago
 Main PID: 16440 (npm)
   CGroup: /system.slice/nuxt.service
           ├─16440 npm
           └─16451 node /home/ec2-user/my-project/node_modules/.bin/nuxt start
 :

ログは journalctl で確認可能

$ sudo journalctl -u nuxt.service
-- Logs begin at 金 2021-09-24 18:13:40 JST, end at 水 2022-02-09 09:47:07 JST. --
 2月 08 14:20:09 ip-0-0-0-0.ap-northeast-1.compute.internal systemd[1]: Started nuxt app.
 2月 08 14:20:09 ip-0-0-0-0.ap-northeast-1.compute.internal systemd[1]: nuxt.service: main process exited, code=exited, status=1/FAILURE
 2月 08 14:20:09 ip-0-0-0-0.ap-northeast-1.compute.internal systemd[1]: Unit nuxt.service entered failed state.
 2月 08 14:20:09 ip-0-0-0-0.ap-northeast-1.compute.internal systemd[1]: nuxt.service failed.
 2月 08 14:20:09 ip-0-0-0-0.ap-northeast-1.compute.internal systemd[1]: nuxt.service holdoff time over, scheduling restart.
 2月 08 14:20:09 ip-0-0-0-0.ap-northeast-1.compute.internal systemd[1]: Stopped nuxt app.
 2月 08 14:20:09 ip-0-0-0-0.ap-northeast-1.compute.internal systemd[1]: Started nuxt app.
 :

参考にした記事

AmazonLinux2へNuxt.js導入手順
centos7で標準のsystemdを使いnode.js製サーバーをデーモン化する

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3