Chris
Chris

Reputation: 4728

How to run ng serve on port 80

In my package.json file I have:

"scripts": {
  "start": "concurrently \"ng serve --host something.mydomain.com --port 4200 --live-reload false --env=prod\" \"gulp\""
},

When I run npm start then after a few seconds I am able to access my project in my browser at http://something.mydomain.com:4200

I would prefer to access the same system by going directly to https://something.mydomain.com (i.e. without the port number)

I have amended my package.json to:

"scripts": {
  "start": "concurrently \"ng serve --host something.mydomain.com --port 80 --live-reload false --env=prod\" \"gulp\""
},

But when I run package.json I get:

Port 80 is already in use. Use '--port' to specify a different port.

If I run lsof -i :80 then nothing comes up so I am sure that port 80 is not taken. I suspect the issue is that port 80 is a protected port and as I am logged in as ec2-user then I don't have the correct permissions?

How can I run my project over port 80?

Upvotes: 8

Views: 22234

Answers (2)

Lorenzo Montanari
Lorenzo Montanari

Reputation: 968

Personally I wouldn't serve an Angular application in production using the Angular CLI (the ng serve command). A better option is to build the project (ng build) and serve the dist folder. Doing so you would have a much faster application as it wouldn't be "copiled on the fly" and it would't interact with the Angular CLI.

However if you'd like to stick with your approach, in order to run the application on a standard port (so that it hasn't to be specified; 80 for an HTTP website, 443 for an HTTPS one), you will have to modify a different file based on the Angular version you are using:

  • >= Angular 6:

    edit the angular.json file and in the serve object (under <yourProjectName> object), add this piece of code:

    "options": { "port": 80 }

  • Angular < 6.0:

    edit the angular-cli.json file and in the defaults object, add this piece of code:

    "serve": { "port": 80 }

Edit: If application is served using the ng serve --prod command, the following warning will be printed:

**************************************************************************************** 
This is a simple server for use in testing or debugging Angular applications locally. It hasn't been reviewed for security issues.

DON'T USE IT FOR PRODUCTION!
****************************************************************************************

Upvotes: 14

Michael Tsirulnikov
Michael Tsirulnikov

Reputation: 149

I will assume that you are running your project in terminal. Try adding sudo before the ng serve, because port 80 needs to be run as root.

Upvotes: 7

Related Questions