前提
Ubuntuサーバーを建てて、SSHでログイン済であること。
手順
ざっと以下
- aptを更新する
- nginxを入れる
- Node.jsを入れる
- npmを入れる
- Node.jsのWebアプリを作成する
- nginxの設定ファイルを編集する
- nginxを再起動する
- Node.jsアプリを起動する
aptを更新する
sudo apt-get update
sudo apt-get upgrade
nginxを入れる
sudo apt install nginx -y
Node.jsを入れる
sudo apt install nodejs -y
npmを入れる
sudo apt install npm -y
NodejsのWebアプリを作成する
home/ユーザ名/nodejs/SampleApp
にindex.js
を作成し、以下を記載し保存。
const http = require("http");
const port = 3000;
const server = http.createServer(function (req, res) {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Hello World");
});
server.listen(port);
nginxの設定ファイルを編集する
/etc/nginx/sites-enabled
のdefault
をエディタで開き、location
部分を編集する。
location / {
proxy_pass http://localhost:3000; # ★追加
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
}
nginxを再起動する
sudo service nginx restart
Node.jsアプリを起動する
node index.js
あとはアクセスすれば「Heelo World」が表示されるはず。
困ったポイント
随時更新(しない)
- AWSの場合、インバウンドルールを設定しなければいけない。
AWSの場合、インバウンドルールを設定しなければいけない。
自分の場合、SSHの22番ポートにしかアクセスを受け付けない設定だったので、HTTPの80番ポートのアクセスも許可するようにした。
コメント