如何在没有“gcloud app deploy”的情况下在 GCP 上部署 Node.js express 服务器?
How to deploy Node.js express server on GCP whithout `gcloud app deploy`?
我不想使用命令 gcloud app deploy
来部署我的 Node.js Express 服务器。
理想情况下,我想:
- 克隆 VM 实例上的存储库
- 运行
npm install
- 运行
npm start
这将在端口 5000 启动节点服务器。
这种配置的防火墙规则是什么?我会使用 VM 的外部 IP 将请求发送到我的服务器或其他东西吗? NGINX 在这里的作用是什么?
要在 GCP 实例上安装和 运行 Node.js express sercer,请按照以下步骤操作(在 Debian9 VM 上测试):
sudo apt update
sudo su -
curl -sL https://deb.nodesource.com/setup_12.x | bash -
apt install -y nodejs
curl -L https://npmjs.org/install.sh | sudo sh
npm install -g express-generator
logout
express myproj1
cd myproj1
npm install
npm start
之后你应该会看到
> m1@0.0.0 start /home/wbogacz/m1
> node ./bin/www
关于防火墙 - 添加规则以允许端口 3000 上的 TCP 流量到达此机器;请参阅下面的示例;
gcloud compute --project=myproject firewall-rules create express_rule --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp:3000 --source-ranges=0.0.0.0/0 --target-tags=myvm
这假设您的实例有一个标签 myvm
。
之后您应该能够转到您的 VM 的外部 IP 并在浏览器页面中看到 Welcome to Express
消息。
您可以在 Google 云上配置常规的 http 和 https 端口,并使用 nginx 作为代理将数据路由到您的应用程序。
Nginx 配置示例:
server {
listen 80;
location / {
proxy_pass http://yourAppAddress:5000/;
}
}
尽管我建议使用 Docker 来部署您的应用程序。
我不想使用命令 gcloud app deploy
来部署我的 Node.js Express 服务器。
理想情况下,我想:
- 克隆 VM 实例上的存储库
- 运行
npm install
- 运行
npm start
这将在端口 5000 启动节点服务器。
这种配置的防火墙规则是什么?我会使用 VM 的外部 IP 将请求发送到我的服务器或其他东西吗? NGINX 在这里的作用是什么?
要在 GCP 实例上安装和 运行 Node.js express sercer,请按照以下步骤操作(在 Debian9 VM 上测试):
sudo apt update
sudo su -
curl -sL https://deb.nodesource.com/setup_12.x | bash -
apt install -y nodejs
curl -L https://npmjs.org/install.sh | sudo sh
npm install -g express-generator
logout
express myproj1
cd myproj1
npm install
npm start
之后你应该会看到
> m1@0.0.0 start /home/wbogacz/m1
> node ./bin/www
关于防火墙 - 添加规则以允许端口 3000 上的 TCP 流量到达此机器;请参阅下面的示例;
gcloud compute --project=myproject firewall-rules create express_rule --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp:3000 --source-ranges=0.0.0.0/0 --target-tags=myvm
这假设您的实例有一个标签 myvm
。
之后您应该能够转到您的 VM 的外部 IP 并在浏览器页面中看到 Welcome to Express
消息。
您可以在 Google 云上配置常规的 http 和 https 端口,并使用 nginx 作为代理将数据路由到您的应用程序。
Nginx 配置示例:
server {
listen 80;
location / {
proxy_pass http://yourAppAddress:5000/;
}
}
尽管我建议使用 Docker 来部署您的应用程序。