简体   繁体   English

如何在没有`gcloud app deploy`的情况下在GCP上部署Node.js express服务器?

[英]How to deploy Node.js express server on GCP whithout `gcloud app deploy`?

I do not want to use the command gcloud app deploy to deploy my Node.js Express server.我不想使用命令gcloud app deploy来部署我的 Node.js Express 服务器。

Ideally, I want to:理想情况下,我想:

  1. Clone the repo on the VM Instance在 VM 实例上克隆存储库
  2. Run npm install运行npm install
  3. Run npm start which will start the node server at port 5000.运行npm start这将在端口 5000 启动节点服务器。

What are the firewall rules for such configuration?这种配置的防火墙规则是什么? Will I use the external IP of the VM to send the requests to my server or something else?我会使用 VM 的外部 IP 将请求发送到我的服务器或其他东西吗? What is the role, if any, of NGINX here? NGINX在这里的作用是什么?

you could just config the regular http and https ports on Google cloud and use nginx as a proxy to route the data on to your app.您可以在 Google 云上配置常规的 http 和 https 端口,并使用 nginx 作为代理将数据路由到您的应用程序。

Nginx conf example: Nginx 配置示例:

server {
listen 80;

 location / {
     proxy_pass http://yourAppAddress:5000/;
 }
}

Although I would recommend using Docker to deploy your application.虽然我建议使用 Docker 来部署您的应用程序。

To install and run Node.js express sercer on GCP instance follow these steps (tested on Debian9 VM):要在 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

After which you should see之后你应该看到

> m1@0.0.0 start /home/wbogacz/m1
> node ./bin/www

Regarding firewall - add a rule to allow TCP traffic on port 3000 to this machine;关于防火墙 - 添加规则以允许端口 3000 上的 TCP 流量到这台机器; see example below;见下面的例子;

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

This assumes that your instance has a tag myvm .这假设您的实例具有标签myvm

After which you should be able to go to the external IP of your VM and see in the browser page with Welcome to Express message.之后,您应该能够将 go 连接到 VM 的外部 IP 并在浏览器页面中看到Welcome to Express消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM