How and what to deploy Angular2 to IIS How and what to deploy Angular2 to IIS angular angular

How and what to deploy Angular2 to IIS


Setting up Angular 2 for me was quite an issue because the HTML5 routes (without a hashbang) weren't working.

To get an Angular2 project working on an IIS environment without serving it using the Angular-CLI (you still need it to build it!):

  1. After you're finished with development on your project, you'll need to build (or compile) it to a different environment. If you need to set that up, read this.

The shortest build command you need is:

ng b

  1. In your build folder (if you didn't add an external/different folder, this will be the 'dist' folder in your project), copy the contents to your IIS server.

  2. Ensure the folder of your IIS server has the needed permissions for the IIS_IUSRS group and IUSR user to access it. (Right click on the folder -> Properties -> Security -> Edit -> Add, and type those in. You can click the 'Check Name' button to ensure it's the correct ones you're typing in)

  3. The next issue you need to tackle is getting a web.config file to put in your server folder to fix routing issues.

If we were working with Apache, we would need an .htaccess, but for IIS, we're using a web.config. The one found here worked for me if your application is routing from the root directory of your server. (Note: As reminded by @Demortes, this will require an additional module to be added to your IIS environment called URLRewrite)

This (web.config) file goes in the root directory of your server.

Hope this helps anyone who had similar issues to me :)

Cheers.


  1. Download and install IIS rewrite plugin https://www.iis.net/downloads/microsoft/url-rewrite

  2. Create application under default website.

    • Create a folder in c:\inetpub\wwwroot to host the application
    • After step 8 copy dist folder contents to c:\inetpu\wwwroot\
  3. Before build in index.html change base href="/" to base href="//"

    • To skip steps 1,2 and 3 for every new build check out step 9 as alternative to step 8
  4. Web config structure.

    <configuration>    <system.webServer>        <rewrite>        <rules>            <rule name="Main Rule" >                    <match url=".*" />                    <conditions logicalGrouping="MatchAll">                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />                    </conditions>                    <action type="Rewrite" url="/<appname subfolder>/" />                </rule>            </rules>        </rewrite>    </system.webServer></configuration>
    • Note: Create a Web.config in src folder and add a reference to it in angular-cli.json in assets section so that it gets copied over during each build. -
    • Otherwise you will have manually create this file every time ng build is used.
  5. not applicable

  6. In angular-cli.json put web.config in assets block

            "assets": [            "assets",            "favicon.ico",            "Web.config"        ],
  7. In angular-cli.josn put custom css path example so that it will be packaged in styles..bundle.cs

        "styles": [        "../node_modules/font-awesome/css/font-awesome.min.css",        "../node_modules/bootstrap/dist/css/bootstrap.min.css",        "assets/site.css",        "assets/page.css",        "assets/menu.css",        "styles.css",        "../node_modules/primeng/resources/primeng.min.css",        "../node_modules/primeng/resources/themes/omega/theme.css"            ],

    If you have custom scripts put those path under scripts section example

        "scripts": [            "../node_modules/jquery/dist/jquery.js",                    "index.js"             ],
  8. ng build --prod

    • Note: ng build --prod starts AOT (ahead of time compilation) by default in latest version of angular-cli
    • Note: ng build command deletes dist folder and recreates that folder every time you ng build command
  9. Alternative build command:

    ng build --prod --output-path 'C:\inetpub\wwwroot\<appname subfolder>' --base-href /<appname subfolder>'/
    • a- if you don't want to manually update base-href in index.html
    • b- if you don't want to copy dist folder and wwwroot folder of app.

    • Note1: Following command will only work if you open visual code (or any terminal app) with administrative privileges. Otherwise mkdir command to create output folder in wwwroot will fail.

    • Note2: You still need to update Web.config with . See step 4
    • Note3: Checkout slash / at both starting and end of --base-href /'/
  10. Check direct quote from one of the posting. Not sure if changing security privileges of IIS_IUSRS group and IUSR user for ...wwwroot\as described in one of the web links is required. May be it is not required but I am highlighting it over here for you to keep in mind.

Direct quote from another use : " Ensure the folder of your IIS server has the needed permissions for the IIS_IUSRS group and IUSR user to access it. (Right click on the folder -> Properties -> Security -> Edit -> Add, and type those in. You can click the 'Check Name' button to ensure it's the correct ones you're typing in)"

References : - How and what to deploy Angular2 to IIS- https://www.youtube.com/watch?v=K0XORWxG11k


The webapp itself needs no server intelligence, as it is just static files - web assets ( *.js, *.html files etc). The static files are what angular2-quickstart generates as output of its build process, which you run in your dev environment (probably locally on your personal computer). The dev environment will need node (+ npm). And infact, you can test this tutorial on your local dev environment without the need for any external server.

edit: If u look in the package.json u can see it has lite-server:

  "scripts": {    "start": "npm run lite",    "lite": "lite-server"  },

Lite server is a small server that simulates a simple (web) file server.

Lightweight development only node server that serves a web app, opens it in the browser, refreshes when html or javascript change, injects CSS changes using sockets, and has a fallback page when a route is not found.

To give you an answer, to serve your app with IIS, you only need http://docs.asp.net/en/latest/fundamentals/static-files.html