Xserver に Express をデプロイする - Ubuntu

  • 作成日:
  • 最終更新日:2025/10/11

OS のアップデート

パッケージのリストを最新のものに更新します。

sudo apt update -y

SSH の設定変更

root のログイン禁止ポート番号の変更鍵認証の設定については、以下のページを参考にしてください。

Node.js のインストール

Node.js のインストールについては、以下のページを参考にしてください。

このページの設定では、NodeSource のリポジトリを使用する方法で設定しています。

Nginx の設定

以下のコマンドを実行し、Nginx をインストールします。

sudo dnf install nginx -y

ufw にルールを追加します。

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

HTTPS 化の設定

Let’s Encrypt を使って無料で本物の証明書を発行します。以下のコマンドを実行し、インストールします。

sudo apt install certbot python3-certbot-nginx

Let’s Encrypt で証明書の発行を行います。

sudo certbot certonly --nginx -d 取得しているドメイン

Nginx の設定を変更します。example.comの部分は取得しているドメインに変更します。

23行目と55行目のサーバーのIPアドレスの部分をサーバーのIPアドレスに変更します。

41行目のproxy_pass http://localhost:3000/;の部分は、Express.js のサーバーで利用しているポート番号を指定します。

server {
listen 80 default_server;
server_name _;

return 301 https://example.com$request_uri;
}

server {
listen 80;
server_name example.com;

location ^~ /.well-known/acme-challenge/ {
    root /var/www/html;
}

location / {
    return 301 https://$host$request_uri;
}
}

server {
listen 80;
server_name サーバーのIPアドレス;

return 301 https://example.com$request_uri;
}

server {
listen 443 ssl;
http2 on;
server_name example.com;

ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

location ^~ /.well-known/acme-challenge/ {
    root /var/www/html;
}

location / {
    proxy_pass http://localhost:3000/;
    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;
}
}

server {
listen 443 ssl;
http2 on;
server_name サーバーのIPアドレス;

ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

return 301 https://example.com$request_uri;
}

設定変更後は、以下のコマンドを実行し、Nginx を再起動します。

sudo systemctl restart nginx

/etc/nginx/sites-enabled/default(Nginxのデフォルト設定ファイル)にもlisten 80 default_server;があるとduplicate default serverのエラーの原因になります。