Showing posts with label IIS. Show all posts
Showing posts with label IIS. Show all posts

Hosting Angular application in IIS Server

We can host an angular application in IIS with the following simple steps.

Build the Angular application

 First build the Angular application using the following command
ng build
Once the build completes successfully it publishes all the build artifacts to the 'dist' folder.

Create a Web application in IIS

Create a Website or Web application and point it to the 'dist' folder in the Angular application.

Install URL Rewrite Module in the IIS

We should install the URL Rewrite Module in IIS, if it not installed already, to handle the deep-linking. We can do it by using Web Platform Installer or manually download and install from https://www.iis.net/downloads/microsoft/url-rewrite

Add web.config file with URL-rewrite rule

Add a web.config file to the root folder of the Angular application (where angular.json file exists) and add the following content
<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webServer>
  <rewrite>
    <rules>
      <rule name="Angular Routes" stopProcessing="true">
        <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="./index.html" />
      </rule>
     </rules>
   </rewrite>
 </system.webServer>
</configuration>
The above URL rewrite rule will route all the requests, which are not for files or folders, to the root of the application.
As the web.config needs to add in the build, add the following to the angular.json file under "assets" section to copy the web.config file to 'dist' folder when we build the application.
"assets": [
    "app/assets",
    "app/favicon.ico",
    "web.config"
],

Update Base Href value if required

We need to update the base href value in the index.html file based on our hosting type. If we host the application as a website, the base href value should be "/". If we hosted as a web application, the value should be "/<Your web application name>/". 
We can also change this value dynamically while building the application. For that, just use
ng build --base--href "/<Your web application name>/"

Build again

Now build it again to reflect the web.config changes and open the URL in the browser
Happy Coding 😊!!


Migrating Application Pools and Sites in IIS from one Server to another Server.

Sometimes we need to migrate IIS data from one server to another server (applicatin pools,sites and applications) or else we need to restore the IIS data in the same machine after OS reinstallation. In such cases, we can export / import the application pools, site and applicaitons data using IIS appcmd command without recreating all of them again in the new server. This will also be usefull when your upgrading from IIS 7 to IIS 7.5

Exporting Application pools:

Use the below command for exporting the application pools
c:\Windows\system32\inetsrv> appcmd list apppool /config /xml > D:\apppools.xml

It will export all the application pools. We need to remove the default application pools (DefaultAppPool, Classic .NET AppPool etc) from the xml files, because those will be created automatically when IIS installed.

We can export particular app pool using the below statement
c:\Windows\system32\inetsrv> appcmd list apppool “MyAppPool” /config /xml > D:\myapppool.xml


Importing Application pools:

We can import application pools using below statement
c:\Windows\system32\inetsrv> appcmd add apppool /in < D:\apppools.xml
We can import particular app pool using the below statement
c:\Windows\system32\inetsrv> appcmd add apppool “MyAppPool” /in < D:\myapppool.xml

Exporting Websites:

Following statement exports all the websites
c:\Windows\system32\inetsrv> appcmd list site /config /xml > D:\sites.xml

This will export all the websites on your webserver. So you need to remove Default Website before importing.

Importing Websites:

We can import websites using below statement
c:\Windows\system32\inetsrv> appcmd add site /in < D:\site.xml


Exporting Applications:

Following statement exports all the applications under websites
c:\Windows\system32\inetsrv> appcmd list app /config /xml > D:\apps.xml

This will export all the applications in the websites including the default one's. So you need to remove apps with path="/"

Importing Applications:

We can import applications using below statement
c:\Windows\system32\inetsrv> appcmd add app /in < D:\apps.xml


Source

Auto Start Website on IIS 7.0 or 7.5

Some time we may need to restart application pools regularly or we may need to restart the system on certain intervals. At that time, the first user to hit the site has to wait while everything is warms up again. It may take some time may be up to 30 to 60 seconds or above. To avoid this lag time for the first user, Microsoft introduced a feature called Application Initialization. It will automatically initialize the application when application pool starts and avoid the time lag for the first user.

In IIS 8 Application Initialization is part of the IIS feature set. But in IIS 7.0 or 7.5 you need to install this feature from here . It requires a system reboot.

Once it installed, you can check the it in IIS.
  1. Open IIS and click or the server node
  2. In the details pane open the Configuration Editor under Management
  3. Inside the editor, check for the section "System.webServer/applicationInitialization" in the Section drop down list in the top. If you find the section, it is installed successfully.

Now IIS is all set but you need to tell which application pools and websites you want to start. Just open 

C:\Windows\System32\inetsrv\config

and open the applicationHost.config file and do the following changes (Make sure to take a backup of the file before you make changes).

1) Applicaton pools Section:
Find the applicationPools node and add startMode="AlwaysRunning" to the application pool which needs to be start automatically.
<add autoStart="true" managedRuntimeVersion="v4.0" name="Jobs" startMode="AlwaysRunning"/>
Check whether it's autoStart property is "true". Otherwise set it to true. If it is not  there or its value is false then will not autoStart.

2) Web site Section:

Go to the sites node and find your site to which you need to start automatically and set preloadEnabled="true".
<application applicationPool="APPLICATION POOL NAME" path="YOUR SITE NAME" preloadEnabled="true">
        <virtualDirectory path="/" physicalPath="YOUR SITE PHYSICAL PATH">
        </virtualDirectory>
</application>
That's it. Now your site will starts automatically.