ReactJS · 7 min read · Dec 21, 2025

Ubuntu 22.04にNginxを使用してReactJSをインストールする方法

React.jsは、2011年にFacebookによって開発された無料でオープンソースのJavaScriptフレームワークです。再利用可能なUIコンポーネントを構築するために使用され、最小限のコーディングで迅速かつ効率的にリッチで魅力的なWebアプリを作成するのに役立ちます。Reactを使用すると、インタラクティブなWebインターフェースを構築し、小さく孤立した部品から複雑なUIを構成できます。アプリを高速にするVirtual DOMを使用しています。Virtual DOM、単方向データバインディング、コンポーネント、JSX、条件文など、多くの機能を提供します。

このチュートリアルでは、Ubuntu 22.04にReact.jsをインストールする方法を示します。

前提条件

  • Ubuntu 22.04を実行しているサーバー。
  • サーバーIPにポイントされた有効なドメイン名。
  • サーバーに設定されたルートパスワード。

始めに

まず、システムパッケージを最新バージョンに更新する必要があります。次のコマンドを使用して更新できます:

apt-get update -y  
apt-get upgrade -y

すべてのパッケージが最新の状態になったら、次のコマンドを実行して他の必要な依存関係をインストールします:

apt-get install curl gnupg2 -y

すべての依存関係がインストールされたら、次のステップに進むことができます。

Node.jsのインストール

次に、サーバーにNode.jsをインストールする必要があります。デフォルトでは、最新のNode.jsバージョンはUbuntu 22.04の標準リポジトリにはありません。したがって、Node.jsの公式リポジトリからインストールする必要があります。

まず、次のコマンドでNode.jsリポジトリを追加します:

curl -sL https://deb.nodesource.com/setup_18.x | bash -

次に、次のコマンドを実行してNode.jsをシステムにインストールします:

apt-get install nodejs -y

Node.jsをインストールした後、次のコマンドを使用してNPMを最新バージョンに更新します:

npm install npm@latest -g

次の出力が得られるはずです:

removed 14 packages, changed 73 packages, and audited 223 packages in 3s

14 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

次に、次のコマンドを使用してインストールされたNode.jsのバージョンを確認します:

node -v

次の出力が得られるはずです:

v18.12.1

完了したら、次のステップに進むことができます。

Create React Appツールのインストール

Create React Appは、ユーザーがセットアップと構成にかかる時間を節約するのに役立つCLIツールです。単一のコマンドを実行するだけで、Create React Appがプロジェクトを開始するために必要なすべてのツールをセットアップします。

次のコマンドを使用してCreate React Appツールをインストールできます:

npm install -g create-react-app

完了したら、次のステップに進むことができます。

サンプルReactアプリケーションの作成

このセクションでは、Create React Appツールを使用してサンプルReactアプリを作成します。

まず、ディレクトリを/optに変更し、次のコマンドで最初のプロジェクトを作成します:

cd /opt  
create-react-app reactapp

次の出力が表示されるはずです:

Success! Created reactapp at /opt/reactapp
Inside that directory, you can run several commands:

  npm start
    Starts the development server.

  npm run build
    Bundles the app into static files for production.

  npm test
    Starts the test runner.

  npm run eject
    Removes this tool and copies build dependencies, configuration files
    and scripts into the app directory. If you do this, you can’t go back!

We suggest that you begin by typing:

  cd reactapp
  npm start

Happy hacking!

次に、プロジェクトディレクトリに移動し、次のコマンドでアプリケーションを開始します:

cd /opt/reactapp  
npm start

次の出力が得られるはずです:

Compiled successfully!

You can now view reactapp in the browser.

  http://localhost:3000

Note that the development build is not optimized.
To create a production build, use npm run build.

webpack compiled successfully

CTRL+Cを押してアプリケーションを停止します。Reactアプリケーションを管理するためのsystemdサービスファイルを作成します。

ReactアプリのためのSystemdサービスの作成

次に、Reactアプリのためのsystemdサービスを作成する必要があります。次のコマンドで作成できます:

nano /lib/systemd/system/react.service

次の行を追加します:

[Service]
Type=simple
User=root
Restart=on-failure
WorkingDirectory=/opt/reactapp
ExecStart=npm start -- --port=3000

ファイルを保存して閉じたら、次のコマンドでsystemdデーモンをリロードします:

systemctl daemon-reload

次に、Reactサービスを開始し、次のコマンドを実行してシステム再起動時に自動的に開始されるようにします:

systemctl start react  
systemctl enable react

次のコマンドでReactサービスのステータスを確認できます:

systemctl status react

次の出力が得られるはずです:

? react.service
     Loaded: loaded (/lib/systemd/system/react.service; static)
     Active: active (running) since Sun 2022-11-20 11:18:50 UTC; 6s ago
   Main PID: 76129 (npm start --por)
      Tasks: 30 (limit: 2242)
     Memory: 250.9M
        CPU: 4.663s
     CGroup: /system.slice/react.service
             ??76129 "npm start --port=3000" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             ??76140 sh -c "react-scripts start --port=3000"
             ??76141 node /opt/reactapp/node_modules/.bin/react-scripts start --port=3000
             ??76148 /usr/bin/node /opt/reactapp/node_modules/react-scripts/scripts/start.js --port=3000

Nov 20 11:18:54 ubuntu2204 npm[76148]: (node:76148) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddl>
Nov 20 11:18:54 ubuntu2204 npm[76148]: (Use `node --trace-deprecation ...` to show where the warning was created)
Nov 20 11:18:54 ubuntu2204 npm[76148]: (node:76148) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMid>
Nov 20 11:18:54 ubuntu2204 npm[76148]: Starting the development server...
Nov 20 11:18:55 ubuntu2204 npm[76148]: Compiled successfully!
Nov 20 11:18:55 ubuntu2204 npm[76148]: You can now view reactapp in the browser.
Nov 20 11:18:55 ubuntu2204 npm[76148]:   http://localhost:3000
Nov 20 11:18:55 ubuntu2204 npm[76148]: Note that the development build is not optimized.
Nov 20 11:18:55 ubuntu2204 npm[76148]: To create a production build, use npm run build.
Nov 20 11:18:55 ubuntu2204 npm[76148]: webpack compiled successfully

完了したら、次のステップに進むことができます。

Nginxをリバースプロキシとして構成する

ReactアプリのためにNginxをリバースプロキシとしてインストールし構成するのは良いアイデアです。これにより、ポート80を介してアプリにアクセスできます。

まず、次のコマンドを使用してNginxパッケージをインストールします:

apt-get install nginx -y

Nginxがインストールされたら、新しいNginx仮想ホスト構成ファイルを作成します:

nano /etc/nginx/sites-available/reactjs.conf

次の行を追加します:

upstream backend {
  server localhost:3000;
}

server {
    listen 80;
    server_name reactjs.example.com;

    location / {
        proxy_pass http://backend/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forward-Proto http;
        proxy_set_header X-Nginx-Proxy true;

        proxy_redirect off;
    }
}

ファイルを保存して閉じたら、次のコマンドでNginx仮想ホストを有効にします:

ln -s /etc/nginx/sites-available/reactjs.conf /etc/nginx/sites-enabled/

次に、次のコマンドを実行してNginxに構文エラーがないか確認します:

ginx -t

次の出力が得られるはずです:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

最後に、次のコマンドを実行してNginxサービスを再起動し、変更を適用します:

systemctl restart nginx

次のコマンドでNginxサービスのステータスを確認することもできます:

systemctl status nginx

次の出力が得られるはずです:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-11-20 11:20:17 UTC; 6s ago
       Docs: man:nginx(8)
    Process: 76760 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 76762 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 76763 (nginx)
      Tasks: 2 (limit: 2242)
     Memory: 2.6M
        CPU: 32ms
     CGroup: /system.slice/nginx.service
             ??76763 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ??76764 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Nov 20 11:20:17 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
Nov 20 11:20:17 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.

この時点で、Nginxはインストールされ、Reactアプリを提供するように構成されています。次のステップに進むことができます。

Let’s EncryptでReact.jsを保護する

次に、Let’s Encrypt SSLをインストールおよび管理するためにCertbotクライアントパッケージをインストールする必要があります。

まず、次のコマンドを使用してCertbotをインストールします:

apt-get install python3-certbot-nginx -y

インストールが完了したら、次のコマンドを実行してウェブサイトにLet’s Encrypt SSLをインストールします:

certbot --nginx -d reactjs.example.com

有効なメールアドレスを提供し、以下のように利用規約に同意するよう求められます:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for reactjs.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/fuel.conf

次に、HTTPトラフィックをHTTPSにリダイレクトするかどうかを選択します:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

2を入力してEnterを押してインストールを完了します。次の出力が表示されるはずです:

Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/fuel.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://reactjs.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=reactjs.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/reactjs.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/reactjs.example.com/privkey.pem
   Your cert will expire on 2023-02-20. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all*
   of your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

 - We were unable to subscribe you the EFF mailing list because your
   e-mail address appears to be invalid. You can try again later by
   visiting https://act.eff.org.

これで、あなたのReact.jsウェブアプリケーションはLet’s Encrypt SSLで保護されました。

ReactアプリのWebインターフェースにアクセスする

今、ウェブブラウザを開いてURL https://reactjs.example.com を入力します。次の画面でReact.JsのWebインターフェースにリダイレクトされます:

結論

おめでとうございます!あなたはUbuntu 22.04サーバーにReact.Jsを正常にインストールして構成しました。これで、自分のReactベースのアプリケーションを構築し、プロダクション環境でホストを開始できます。質問があればお気軽にお尋ねください。

Share: X/Twitter LinkedIn

新しい投稿を受信箱で受け取る

スパムはありません。いつでも購読を解除できます。