How to deploy a simple Angular2 app on a shared ho

2020-02-11 06:59发布

I'd like to deploy my Angular2 app on my shared hosting. I tried transferring the files via ssh, but the app doesn't run. I guess there is something to do like the ng serve on local.

What do are the steps to follow ? I couldn't find them on the internet :/

Thanks

EDIT

I have a very simple app with a few routes and components. No serverside code since I'm calling my REST api for my needs.

3条回答
爷、活的狠高调
2楼-- · 2020-02-11 07:20

Of course you have to serve it on the server. There are a few steps you have to take:

  • Transfer your files to the server (think this is done)
  • Login via SSH and execute a npm install inside your app's directory.
  • Change your app.js (or whatever) to bind to your internet interface. The default is to have it bind to localhost so you won't be able to access it from outside your local.
  • You will also need to open the port on the server. This involves using a reverse proxy (like nginx) to listen for a standard port (80) and redirect it to the port your app is listening. If you would like your app to listen on port 80 directly you would need to run it under root user, which is not recommended. Try open the port with a iptables command like: iptables -A INPUT -p tcp --dport 9000 -j ACCEPT
  • Inside your app's directory execute your ng serve (or whatever)

This instructions are for development, not for production use.

查看更多
ら.Afraid
3楼-- · 2020-02-11 07:24

You will have to build the app, have a look in package.json in the app root. You will normally have build dev/prod or something along those lines. Then just copy the contents of the dist folder up to the server and make sure that scrips src are correct in the index. file.

查看更多
聊天终结者
4楼-- · 2020-02-11 07:25

On your local development machine do an ng build --prod or ng build --prod --aot

This will build your app into the dist folder. Copy the contents of your dist folder to the public directory of your shared hosting.

Depending on your hosting you may need to configure a web server (nginx, Apache, IIS, etc) to serve your files. Don't run ng serve on your shared hosting.

Unless you plan on running your application in Universal mode, where it rerenders pages server side, do not copy your entire app to your shared host. Do not do an npm install on your shared host. Just copy the built application files in the dist folder.

查看更多
登录 后发表回答