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

  • 作成日:
  • 最終更新日:2025/06/25

VPS で Express.js のアプリを起動させる

XServer VPSが、2025年7月10日(木)から、無料VPSの提供を開始しました。

有料版と遜色ないサーバー環境を利用することができます。ここでの設定は、無料VPSを使いましたが有料版でも同じ設定したので問題ないです。

https 接続ができるようにするため、ドメインを取得していることが前提です。

環境

サーバーOS AlmaLinux
プロキシサーバー Apache
プログラミング言語 Node.js
フレームワーク Express.js
証明書の発行・設定 certbot
VPS Xserver 無料VPS

Xserver VPS の設定

Xserver VPS のパネルで、ドメインの追加パケットフィルター設定レコードの設定が必要になります。

以下のページを参考にしてください。

DNS レコードについては以下も参考にしてください。

OS のアップデート、EPEL リポジトリの追加

OS をアップデートします。

sudo dnf update -y

EPEL リポジトリを追加します。

sudo dnf install epel-release -y

ユーザーの作成、グループ追加

ユーザーを作成し、パスワードの設定をします。

# ユーザーの追加
sudo useradd user_name

# パスワード設定
sudo passwd user_name

sudo を実行できるようにwheel グループに追加します。

usermod -aG wheel user_name

ユーザーについては以下のページも参考にしてください。

SSH の設定変更(ポート番号の変更、鍵認証の変更、ファイアウォールの設定変更)

SSH のポート番号変更、鍵認証への変更、ファイアウォールの設定変更については以下のページを参考にしてください。

ファイアウォール

Xserver VPS パネルからパケットフィルター設定が ONになっています。そのため、VPS にインストールされた AlamLinux にはファイアウォールがインストールされていません。

ファイアウォールをインストールするには、以下のコマンドを実行します。

# インストール
sudo dnf install firewalld -y

# 起動
sudo systemctl start firewalld

# 自動起動の有効
sudo systemctl enable firewalld

Apache の設定変更(設定ファイルの変更、ファイアウォールの設定変更)

Apache をインストールします。

# インストール
sudo dnf install httpd -y

# 起動
sudo systemctl start httpd

# 自動起動の有効化
sudo systemctl enable httpd

ファイアウォールの設定を変更します。

sudo firewall-cmd --add-service=http --zone=public --permanent
sudo firewall-cmd --add-service=https --zone=public --permanent
sudo firewall-cmd --reload

以下のページも参考にしてください。

certbot で https 接続の設定を行う

certbot とプラグインをインストールします。

# certbot と Apache 用のプラグインをインストール
sudo dnf install certbot python3-certbot-apache -y

# certbot と nginx 用のプラグインをインストール
sudo dnf install certbot python3-certbot-nginx -y

証明書のインストールするには、以下のコマンドを実行します。「 example.com 」の部分は、自分のサイトのドメイン名に置き換えます。

# Apache を利用する場合
sudo certbot --apache -d example.com

# nginx を利用する場合
sudo certbot --nginx -d example.com

以下のページも参考にしてください。

Apache をプロキシサーバーとして利用する場合の設定ファイルの例

/etc/httpd/conf.d/example.conf

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html

    RewriteEngine on

    # Let's Encryptの認証用パスはリダイレクトしない
    RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/

    # サーバー名がexample.comの場合、HTTPSへリダイレクト
    RewriteCond %{SERVER_NAME} =example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

IP アドレスで接続された場合もリダイレクトするようにするには、上記にRedirect permanent / https://jpcybers.netを追加します。

以下のファイルは、certbot で設定を行うと作成されます

/etc/httpd/conf.d/example-le-ssl.conf

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/html

    SSLEngine On
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    # 他のSSL設定も適宜

    ProxyPreserveHost On
    ProxyPass / http://localhost:4000/
    ProxyPassReverse / http://localhost:4000/

    # 必要なら追加の設定も
    <Proxy *>
        Require all granted
    </Proxy>
</VirtualHost>

Nginx をプロキシサーバーとして利用する場合の設定ファイルの例

以下のファイルは、プロキシサーバーとして利用する場合の設定ファイルの例です。

※ Nginx では、example-le-ssl.confのようなファイルは作成されません。

/etc/nginx/conf.d/example.conf

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;
    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;
    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;
}

Express アプリのダウンロード

Express.js のアプリのサンプルを GitHub からクローンするか、ご自身のアプリをローカル上からアップロードしてください。

git clone https://github.com/beginnerP765/exapp.git

サーバーにファイルを配置できれば、アプリのルートディレクトリに移動し、「 npm start 」でアプリを起動します。

ブラウザからhttps://<ドメイン名>でアクセスできるか確認します。

503 エラーになるようなら SELinux の設定を変更します。

SELinux を enforcing で利用している為、503 エラーになります。以下のコマンドを実行して設定を変更します。

sudo setsebool -P httpd_can_network_connect on

PM2 の設定

PM2 をインストールします。

npm install -g pm2

Express.js のアプリのルートディレクトリに移動し、pm2myapp.jsonファイルを作成します。

pm2myapp.json

{
    "name": "myapp",
    "script": "./bin/www",

    "env": {
        "NODE_ENV": "development"
    },

    "env_production": {
        "NODE_ENV": "production"
    }
}

Express.js のルートディレクトリのまま以下のコマンドを実行します。

pm2 start pm2myapp.json --env production

Express アプリが起動した状態のまま、次のコマンドを実行します。

pm2 startup

コマンドを実行すると次のような表示がされます。

[PM2] Init System found: systemd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/home/user/.nvm/versions/node/v20.15.0/bin /home/user/.nvm/versions/node/v20.15.0/lib/node_modules/pm2/bin/pm2 startup systemd -u user --hp /home/user

「 sudo env 」から始まる部分をコピーし、そのコマンドを実行します。

上記の場合だと、「 sudo env PATH=$PATH:/home/user/.nvm/versions/node/v20.15.0/bin /home/user/.nvm/versions/node/v20.15.0/lib/node_modules/pm2/bin/pm2 startup systemd -u user --hp /home/user 」の部分をコピーします。

以下のコマンドを実行して、設定を保存します。

pm2 save

サーバーを再起動してもアプリが起動したままか確認します。ウェブページも問題なく表示されれば PM2 の自動起動の設定は完了です。

以下のページも参考にしてください。

cron で定期的に証明書を更新

cron で証明書の更新と Apache も再起動するコマンドを設定します。以下のコマンドを実行し、設定ファイルを開きます。

sudo crontab -e

ファイルに以下の内容を追記します。毎日 3:00 に実行されますが、SSL 証明書の有効期限が30日未満の場合のみ更新されます。

# Apache
0 3 *  *  * certbot renew && systemctl restart httpd

# Nginx
0 3 * * * certbot renew systemctl reload nginx

設定を確認したい場合は、以下のコマンドを実行します。

sudo crontab -l