strAPIをyarnで導入してみた

色々とあってstrAPIというHeadlessCMSを試してみたいという話が某所で起こったので、まぁ頑張ってください。ローカルで試すんですよねぇ。と思っていたら。
なんか何人かで検証したいのでみんなが触れるところに入れられないかという話があったんでサクッとサーバ立てることにしました。まぁいつも通りAWSEC2構成です。
ひとまずは最小構成で構わないとのことなのでt3で行こうと思ったら間違ってt3aでAMI展開しちゃいました。

では、お約束の手順となります。ほぼコピペのみでやってます。先人の偉大な方々に感謝です。調べたサイトでみんなnpmでやってて、yarnでやってる人がまったくいなかったので困りました。(自分がゴミレベルなだけだからかもしれませんが、、、

■手順

1.事前準備

# ホスト名設定
$ sudo hostnamectl set-hostname starapisv

# アップデート(パッチ類適用)
$ sudo apt -y update
$ sudo apt -y upgrade

# ubuntuユーザをwww-dataグループに追加
$ sudo usermod -aG www-data ubuntu

2.ミドルウェア類設定

# nginxインストール
$ sudo apt-get -y install nginx
$ nginx -v
nginx version: nginx/1.14.0 (Ubuntu)

# node,npmインストール(nパッケージで導入)
$ sudo apt install -y nodejs npm
$ sudo npm install n -g
$ sudo apt install -y nodejs npm
$ sudo npm install n -g
$ sudo n stable
$ sudo apt purge -y nodejs npm
$ node -v
v12.14.0
$ npm -v
6.13.4
$ exec $SHELL -l

# yarnインストール
$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
$ sudo apt update
$ sudo apt -y install yarn
$ yarn -v
1.21.1

#mongoDBインストール
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
$ sudo apt update
$ sudo apt install -y mongodb-org
$ mongod --version
db version v4.0.14
git version: 1622021384533dade8b3c89ed3ecd80e1142c132
OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1804
    distarch: x86_64
    target_arch: x86_64
$ mongo --version
MongoDB shell version v4.0.14
git version: 1622021384533dade8b3c89ed3ecd80e1142c132
OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1804
    distarch: x86_64
    target_arch: x86_64
$ sudo systemctl start mongod
$ sudo systemctl enable mongod

#mongoDB認証設定
$ mongo
> use strapi
> db.createUser({user: "db",pwd: "password",roles: [{ role: "userAdmin", db: "strapi" },{ role: "dbAdmin", db: "strapi" },{ role: "readWrite", db: "strapi" }]> exit

$ sudo vi /etc/mongod.conf
※以下を追記
----------------------------
security:
  authorization: enabled
----------------------------
$ sudo systemctl restart mongod
$ mongo
> use strapi
> db.auth("db", "password")

3.strAPIインストール

#strapiインストール
$ cd /var/www
$ sudo yarn create strapi-app strapi
※下矢印キーで?マーク遷移するので該当選択肢を選ぶ、もしくは入力
? Choose your installation type Custom (manual settings)
? Choose your default database client mongo
? Database name: strapi
? Host: 127.0.0.1
? +srv connection: false
? Port (It will be ignored if you enable +srv): 27017
? Username:db
? Password: password
? Authentication database (Maybe "admin" or blank): strapi
? Enable SSL connection: No
#$ sudo yarn strapi install graphql   ←入れると不具合生じます。
~略~

Available commands in your project:

  yarn develop
  Start Strapi in watch mode.

  yarn start
  Start Strapi without watch mode.

  yarn build
  Build Strapi admin panel.

  yarn strapi
  Display all available commands.

You can start by doing:

  cd /var/www/strapi
  yarn develop
$

#pm2のインストール後デーモン化(自動起動設定)
$ cd strapi
$ sudo yarn global add pm2
$ sudo pm2 start yarn --interpreter bash --name strapi -- develop
$ sudo pm2 save
$ sudo pm2 startup systemd

4.Webサーバ連携

# Let'sEncryptインストール
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt update
$ sudo apt -y install python-certbot-nginx
$ sudo certbot --nginx certonly
~略~
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): mymail@example.com
~略~
※対話モードに従って作業
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel:A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: mydomain.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1

#cron設定
$ sudo crontab -e
※以下の通り追記
-------------
#certbot
00 07 * * 1 sudo certbot renew >/dev/null 2>&1

# nginx設定
$ sudo vi /etc/nginx/nginx.conf
※以下の通り修正
----------------
http {
        ##
        # Basic Settings
        ##
++        server_tokens off;
   keepalive_timeout 160;  ←修正

$ sudo vi /etc/nginx/conf.d/strapi.conf
※以下の通り新規作成
----------------
server {
     listen [::]:80;
     listen 80;
     server_name mydomain.example.com;
     
     if ($http_x_forwarded_proto != https) {
            return 301 https://$host$request_uri;
     }
}
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl on;
    server_name mydomain.example.com;

    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_session_timeout 5m;
    ssl_certificate /etc/letsencrypt/live/mydomain.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.example.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_prefer_server_ciphers on;
    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains" always;

    location / {
         #auth_basic "Please Input ID and Password";
         #auth_basic_user_file /etc/nginx/.htpasswd;
         proxy_pass http://localhost:1337;
         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;
         add_header X-Xss-Protection "1; mode=block" always;
         add_header X-Frame-Options "SAMEORIGIN" always;
         add_header X-Content-Type-Options "nosniff" always;
         add_header Access-Control-Allow-Origin "https://mydomain.example.com";
         add_header Referrer-Policy "origin-when-cross-origin" always;
         add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
     }
}
----------------

#nginx起動
$ sudo service nginx restart

■参考サイト


コメント

このブログの人気の投稿

証券外務員1種勉強(計算式暗記用メモ)

GASでGoogleDriveのサブフォルダとファイル一覧を出力する

マクロ経済学(IS-LM分析)