{"id":9113,"date":"2023-12-13T10:26:29","date_gmt":"2023-12-13T10:26:29","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9113"},"modified":"2023-12-13T10:26:29","modified_gmt":"2023-12-13T10:26:29","slug":"get-angular-cli-to-ng-serve-over-https","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/angular\/get-angular-cli-to-ng-serve-over-https","title":{"rendered":"Get angular-cli to ng Serve Over HTTPS"},"content":{"rendered":"<p>To run an Angular application over HTTPS using ng serve, you can use the &#8211;ssl and &#8211;ssl-key options to specify the SSL key file, and the &#8211;ssl-cert option to specify the SSL certificate file.<br \/>\nHere are the steps you need to follow to serve your application over HTTPS using angular-cli:<\/p>\n<h3>1. Generate SSL Certificate and Key<\/h3>\n<p>You need an SSL certificate and key to enable HTTPS. You can generate a self-signed certificate using tools like OpenSSL.<br \/>\nIf you don&#8217;t have OpenSSL installed, you can install it or use an online service to generate a certificate.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">openssl req -x509 -newkey rsa:2048 -keyout server.key -out server.crt -days 365 -nodes\r\n<\/pre>\n<p>This will generate server.key (private key) and server.crt (certificate) files.<\/p>\n<h3>2. Serve Angular Applications with HTTPS<\/h3>\n<p><strong>Angular CLI v7.0.0 and above:<\/strong><\/p>\n<p>Starting from Angular CLI version 7.0.0, HTTPS is supported natively.<br \/>\nYou can use the below common line options to serve your Angular application over HTTPS.<br \/>\n&#8211;ssl &#8211; boolean to whether enable or disable SSL; defaults to false<br \/>\n&#8211;ssl-key &#8211; Relative path to the private key file<br \/>\n&#8211;ssl-cert &#8211; options to serve<br \/>\nOpen a terminal in your Angular project directory and run:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ng serve --ssl true --ssl-key \"path\/to\/server.key\" --ssl-cert \"path\/to\/server.crt\"     \r\n<\/pre>\n<p>Replace path\/to\/server.key and path\/to\/server.crt with the actual paths to your SSL key and certificate files.<br \/>\nIf you do not want to use command line arguments with ng serve then you can add the following configs in <strong>angular.json<\/strong> and run the ng serve command on the terminal.<\/p>\n<p><strong>Angular CLI 6+<\/strong><br \/>\n<img decoding=\"async\" class=\"alignnone size-large wp-image-9114\" src=\"https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Angular-CLI-6--1024x558.png\" alt=\"Angular CLI 6+\" width=\"1024\" height=\"558\" srcset=\"https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Angular-CLI-6--1024x558.png 1024w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Angular-CLI-6--300x163.png 300w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Angular-CLI-6--768x418.png 768w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Angular-CLI-6-.png 1026w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>If you want to turn on and off the ssl as per your choice then you can remove the &#8220;ssl&#8221;: true line from the options and add the &#8211;ssl flag to your command when running ng serve over HTTPS.<br \/>\n<strong>Angular CLI v6.0.0 to v6.2.9<\/strong><br \/>\nFor Angular CLI versions 6.0.0 to 6.2.9, HTTPS is not supported natively with ng serve. However, you can use the ng serve &#8211;ssl workaround:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ng serve --ssl\r\n<\/pre>\n<p>The Angular CLI will automatically generate a self-signed certificate for you.<\/p>\n<p><strong>Angular CLI &lt; 6<\/strong><br \/>\nThe Angular CLI configuration format is different from versions 5 to 6. Prior to Angular CLI version 6, the config would be added as follows:<br \/>\n<img decoding=\"async\" class=\"alignnone size-large wp-image-9115\" src=\"https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Angular-CLI-6-1-1-1024x526.png\" alt=\"Angular CLI &lt; 6\" width=\"1024\" height=\"526\" srcset=\"https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Angular-CLI-6-1-1-1024x526.png 1024w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Angular-CLI-6-1-1-300x154.png 300w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Angular-CLI-6-1-1-768x394.png 768w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Angular-CLI-6-1-1-1536x788.png 1536w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Angular-CLI-6-1-1.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h3>3. Access the Application<\/h3>\n<p>Your Angular application should now be accessible over HTTPS. Open your web browser and visit https:\/\/localhost:4200 (or the port you specified with &#8211;port if it&#8217;s different from the default 4200).<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Access-the-Application-1024x691.png\" alt=\"Access the Application\" width=\"1024\" height=\"691\" class=\"alignnone size-large wp-image-9116\" srcset=\"https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Access-the-Application-1024x691.png 1024w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Access-the-Application-300x203.png 300w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Access-the-Application-768x519.png 768w, https:\/\/www.bacancytechnology.com\/qanda\/wp-content\/uploads\/2023\/12\/Access-the-Application.png 1238w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p><strong>Note:<\/strong> Since you are using a self-signed certificate, your browser may display a warning that the connection is not secure. This is expected for self-signed certificates, and you can proceed to view the site.<br \/>\nRemember that using a self-signed certificate is unsuitable for production environments. For production, you should obtain a valid SSL certificate from a trusted certificate authority.<br \/>\nIf you want to use a custom domain or need a more robust solution, consider setting up a reverse proxy (e.g., using Nginx or Apache) to handle HTTPS termination in front of your Angular development server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To run an Angular application over HTTPS using ng serve, you can use the &#8211;ssl and &#8211;ssl-key options to specify the SSL key file, and the &#8211;ssl-cert option to specify the SSL certificate file. Here are the steps you need to follow to serve your application over HTTPS using angular-cli: 1. Generate SSL Certificate and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9117,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-9113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9113"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=9113"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9113\/revisions"}],"predecessor-version":[{"id":9120,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9113\/revisions\/9120"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9117"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}