4

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.

Community
  • 1
  • 1
Fab
  • 668
  • 9
  • 24

3 Answers3

14

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.

Martin
  • 15,820
  • 4
  • 47
  • 56
  • This worked for me. – Olakunle Awotunbo Jul 26 '17 at 16:24
  • @Martin What would you do differently then if you wanted to run in Universal mode? – cs_pupil Apr 13 '19 at 19:08
  • 1
    @cs_pupil you would still want to build and deploy, the difference being you would want to build a browser and server set of bundles. Then you will need a small nodejs Express server that consumes the server bundles using ngUniversal. Though this is outside the scope of this answer, it is well documented on angular.io – Martin Apr 18 '19 at 17:02
  • great work ,it worked for me, Thanks – syed mahroof Sep 14 '19 at 06:30
0

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.

rtn
  • 2,012
  • 4
  • 21
  • 39
0

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.

Jorge
  • 540
  • 6
  • 21
  • 2
    And heeeeere goes the downvote. I would be very glad to know why my answer is not ok. – Jorge Nov 28 '16 at 22:21
  • I'm sure the downvoter didn't think about isomorphic. – Jorge Nov 28 '16 at 22:22
  • 3
    I upvoted the answer but the guy who downvoted it your answer, did that probably because you cannot run npm install on shared hosting, the majority of shared hosting don't have node on their servers and you cannot install anything that requires root privileges – Lynob Mar 14 '18 at 10:44