Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 736
Chris Avatar asked Sep 06 '18 15:09

Chris


People also ask

How do you run ng serve?

To get the application running, the ng serve command must execute within the [name-of-app] folder. Anywhere within the folder will do. The Angular CLI must recognize that it is within an environment generated with ng new . It will run provided this one condition.

What command is NG serve?

ng serve command builds and serve the application. It rebuilds the application if changes occur. Here project is the name of the application as defined in angular.


Video Answer


2 Answers

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!
****************************************************************************************
like image 132
Lorenzo Montanari Avatar answered Oct 28 '22 04:10

Lorenzo Montanari


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.

like image 42
Michael Tsirulnikov Avatar answered Oct 28 '22 04:10

Michael Tsirulnikov