LoginSignup
3
8

More than 3 years have passed since last update.

Apache経由でExpressに接続する方法

Last updated at Posted at 2019-12-01

環境

  • CentOS 7.7
  • Apache 2.4.6
  • node v12.13.1
  • express-generator 4.16.1

イメージ

apache-node.png

設定手順

1. httpdの設定ファイルを編集する

サーバにhttpdがインストールされていない場合は、以下のコマンドでインストールし、有効化します。

$ sudo yum update
$ sudo yum install httpd

$ sudo systemctl enable httpd
$ sudo systemctl start httpd

/etc/httpd/conf.d/の直下に、hoge.confというファイルを作ります。(hogeは変更可能)

今回はApacheをプロキシのような中継サーバとして使うので、proxy.confとつけました。

/etc/httpd/conf.d/proxy.conf
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/

3000はExpressが使用するデフォルトのポート番号です。

httpdを再起動し、設定を反映します。

$ sudo systemctl restart httpd

2. Expressサーバを起動する

nodeがインストールされていない場合は、以下の手順でインストールします。

(2019年12月現在LTSのv12系を使います。)

$ curl -sL https://rpm.nodesource.com/setup_12.x | bash -
$ sudo yum install nodejs

処理が終了したら、バージョンを確認して正常にインストールされたか確認します。

$ node --version
v12.13.1

express-generatorがインストールされていない場合は、以下の手順でインストールします。

$ npm install express-generator -g

expressの新しいプロジェクトを作成します。

$ express exp-app

$ cd exp-app
$ npm install

$ npm start &

以降もターミナルで操作を続けるために、npm start&を付けてバックグラウンドで実行します。

http://localhost:3000とブラウザに入力すると、以下のようなページが表示されます。
express_welcom.PNG
コマンドラインなら、curlで確認しても良いです。

$ curl http://localhost:3000
<!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express</p></body></html>

Expressが正常に起動していることを確認できました。

3. httpでアクセスする

HTTPでローカルからリモートにアクセスするにあたり、以下の2つの設定を変更します。

  • FWの設定
  $ sudo firewall-cmd --add-service=http --zone=public --permanent
  $ sudo firewall-cmd --reload

HTTPリクエストを受け付けます。

  • SElinuxの設定
  $ sudo setsebool -P httpd_can_network_connect on

httpdがネットワーク通信できるように設定します。

4.アクセス確認

ローカルのブラウザでhttp://x1.y1.z1.w1(リモートのIPアドレス)と入力し、Expressのウェルカムページが表示されることを確認します。

以上で、ApacheとExpressの連携が完了しました!

注意

参考

3
8
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
3
8