CentOSのApacheでAdonisを動かす
$ pm2 start server.js$ apache proxy 設定(ドキュメントルートをhttp://localhost:3333に飛ばす)これで大体はいいはずなんだけどうまくいかない。
curl http://localhost:3333とかやると「接続を拒否されました」となる。 firewalldは使われてなくて、iptablesが使われているけど、iptablesをstopしても状況が変わらない。
ということで、仕方ないので、ローカル環境でDockerでテストしてみることにした。
CentOS8のDockerを立ち上げる
環境
- Windows10 Pro
- WSLを使っています
立ち上げる
こちらを見てやりました。
$ docker pull centos:centos8$ docker run -it -d --name centos8 centos:centos8$ docker exec -it centos8 /bin/bashCentOS8でcurlでhttp://localhost:3333にアクセスできるようにする
とりあえず、最初の状態だと、curl http://localhost でも接続できなかった。
$ yum install httpd- systemctlは使えないらしい。
- /etc/init.d/は空だし、apachectl start でもダメ。
$ apachectl startSystem has not been booted with systemd as init system (PID 1). Can't operate.Failed to connect to bus: Host is downhttpd -k startで起動したっぽい。
$ httpd -k startAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this messagehttpd (pid 185) already runningcurl http://localhostはアクセスできた。 curl http://localhost:3333は下記になる。
$ curl localhost:3333curl: (7) Failed to connect to localhost port 3333: Connection refusedAdonisサーバを立ち上げる
$ curl -sL https://rpm.nodesource.com/setup_12.x | bash -$ yum -y install git nodejs$ yum -y install npm$ npm update -g npm$ npm i -g @adonisjs/cli$ adonis new hoge$ cd hoge$ npm i -g pm2$ pm2 start server.js$ curl http://localhost:3333接続できた。
localhostにアクセスしたら、Adonisが表示されるようにしてみる
$ vim /etc/httpd/conf/httpd.conf下記を最後に追加。あとhttpd再起動時にエラーメッセージ出るので、ServerNameの設定箇所のコメントを外した。
ProxyPreserveHost OnProxyPass / http://localhost:3333proxyPassReverse / http://localhost:3333$ httpd -k restart$ curl localhsot<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" /> <title>Hello Adonis</title> <link rel="stylesheet" href="/style.css" /></head><body> <section> <div class="logo"></div> <div class="title"></div> <div class="subtitle"> <p>AdonisJs simplicity will make you feel confident about your code</p> <p> Don't know where to start? Read the <a href="https://adonisjs.com/docs">documentation</a>. </p> </div> </section></body></html>一瞬でできるし。。一瞬でできると思ってたんだよ最初は。うんこうんこ。
centos初期設定だと出来るってことか。やっぱportが空いてないのか。でもiptables止めてもできないんだよな。iptablesを設定して再起動しようと思ったら再起動方法が分からなかった。というか、adonis serveの時点で、curl localhost:3333は出来るわけで、それが出来ないっていうのは、apacheの設定とか関係ないな。
結論
- Adonis.js側のHost設定がおかしかった。localhostになってなかった。