@manhnguyenv

Welcome to my blog!

Deploy IIS

October 16, 2018 08:56

Deploy IIS (edit)

1) The IIS Admin Service

NET START IISADMIN
NET STOP IISADMIN

2) World Wide Web Publishing Service

NET START  W3SVC
NET STOP W3SVC

Deployment of a Website on IIS

Application Pools on IIS 6.0

Deploying ASP.NET Websites on IIS 7.0

How To Deploy a Web App in IIS

Recycle Application Pool(s) in IIS 7 Server Using C# Code

Windows XP IIS Manager v1.7

Programmatically Manage IIS

Use C# to manage IIS

Beginner's Guide: Exploring IIS 6.0 With ASP.NET

Windows Service Management

Controlling installed services via ServiceController in WinForms

Using NET STOP and NET START commands to force IIS services to re-read the registry

https://support.microsoft.com/en-ie/help/236166/using-net-stop-and-net-start-commands-to-force-iis-services-to-re-read

Common IIS-related services

Service Name Display Name
Iisadmin IIS Admin Service
Msftpsvc FTP Publishing Service
Nntpsvc Microsoft NNTP Service
Smtpsvc Microsoft SMTP Service
W3svc World Wide Web Publishing Service

(c) Microsoft Corporation 2000, All Rights Reserved. Contributions by Kevin Zollman, Microsoft Corporation.

https://tecadmin.net/restart-iis-via-command-line/

3) Add Website

https://blogs.msdn.microsoft.com/rakkimk/2007/06/08/iis7-sample-code-for-addingdeleting-a-website-programmatically-c-example/

You can add a website using Add method that is present in the Sites collection of ServerManager. Below is the code snippet which creates an application Pool, and then creates a site, also enables the FREB and commit the changes to the IIS7 configuration file:

ServerManager serverMgr = new ServerManager();
Site mySite = serverMgr.Sites.Add("MySiteName", "C:\\inetpub\\wwwroot", 8080);
serverMgr.ApplicationPools.Add("MyAppPool");
mySite.ApplicationDefaults.ApplicationPoolName = "MyAppPool";
mySite.TraceFailedRequestsLogging.Enabled = true;
mySite.TraceFailedRequestsLogging.Directory = "C:\\inetpub\\customfolder\\site";
serverMgr.CommitChanges();

Now, let's try to delete the same website created. As you know earlier, in IIS7, you need to give an unique name for each website (which was not the case in IIS 6.0).

Site site = serverMgr.Sites["MySiteName"]; // you can pass the site name or the site ID

serverMgr.Sites.Remove(site);

serverMgr.CommitChanges();

4) Command Lines

https://www.codeproject.com/Articles/545019/ParsedCommandLine

IIS & System.DirectoryServices

October 10, 2018 22:23

IIS & System.DirectoryServices (edit)

https://stackoverflow.com/questions/24124644/to-get-the-hosted-web-sites-names-from-iis

Note: Not working with Windows 10 and Server 2012 R2

Xcopy + Appcmd + Cacls + iCacls

October 9, 2018 21:58

iCACLS.exe (edit)

List files and their permissions (access) in command line: 

iCACLS C:\inetpub\wwwroot\Sites\Site1

How to reset NTFS permissions with iCACLS

iCACLS * /T /Q /C /RESET

iCACLS (link)

c:\windows\system32\icacls
c:\windows\system32\icacls c:\folder /grant "domain\user":(OI)(CI)M c:\windows\system32\icacls c:\folder /grant "everyone":(OI)(CI)M c:\windows\system32\icacls c:\folder /grant "Authenticated Users":(OI)(CI)M

icacls systax for recursively adding permissions for Administrators to a folder without altering existing permissions?

icacls "<root folder>" /grant "Domain Admins":F /t

would add Full Access to the "Domain Admins" group to the "root folder" and every folder within.

If you add ":r" after Grant then the permissions would be replaced instead of being added.

icacls "<root folder>" /grant:r "Domain Admins":F /t

The basic permissions are:

Full Control (F)

Modify (M)

Read & Execute (RX)

List Folder Contents (X,RD,RA,REA,RC)

Read (R)

Write (W)

Advanced permissions are:

Full Control (F)

Traverse folder / execute file (X)

List folder / read data (RD)

Read attributes (RA)

Read extended attributes (REA)

Create file / write data (WD)

Create folders / append data (AD)

Write attributes (WA)

Write extended attributes (WEA)

Delete subfolders and files (DC)

Delete (D)

Read permissions (RC)

Change permissions (WDAC)

Take ownership (WO)

You can also specify the inheritance for the folders:

This folder only

This folder, subfolders and files (OI)(CI)

This folder and subfolders (CI)

This folder and files (OI)

Subfolders and files only (OI)(CI)(NP)(IO)

Subfolders only (CI)(IO)

Files only (OI)(IO)

Xcopy.exe

xcopy /s /y "C:\inetpub\wwwroot\Sites\Site1\*.*" "D:\Sites\Site1\"

https://support.microsoft.com/en-us/help/323007/how-to-copy-a-folder-to-another-folder-and-retain-its-permissions

xcopy /S /Q /Y /F    "C:\inetpub\wwwroot\Sites\Site1" "D:\Sites\Site1"
xcopy /S /Q /Y /F    "C:\inetpub\wwwroot\Sites\Site1" "D:\Sites\Site1"
xcopy /S /I /Q /Y /F "C:\inetpub\wwwroot\Sites\Site1" "D:\Sites\Site2"
xcopy /S /I /Q /Y /F "C:\inetpub\wwwroot\Sites\Site1" "D:\Sites\Site2"

/E - Copies folders and subfolders, including empty ones. 
/H - Copies hidden and system files also. 
/K - Copies attributes. Typically, Xcopy resets read-only attributes.
/O - Copies file ownership and ACL information.
/X - Copies file audit settings (implies /O).

/X - Copies file audit settings and system access control list (SACL) information (implies /o).
/O - Copies file ownership and discretionary access control list (DACL) information.

/X – Copies file audit settings and file ownership and ACL information.
/H – Copies hidden and system files.
/E – Copies directories and subdirectories, including empty ones.
/V – Verifies each new file.
/D – Copies files changed on or after the specified date (D:m-d-y).If no date is given, copies only those files whose source time is newer than the destination time.
/Y – Suppresses prompting to confirm you want to overwrite an existing destination file.

Windows Server 2016

  • Windows Server 2016
  • IIS 10 (Windows 10 and Windows Server 2016)
  • MS SQL Server 2017
  • Office 365 Account
5.0 Built-in component of Windows 2000. Windows 2000
5.1 Built-in component of Windows XP Professional. Windows XP Professional
6.0 Built-in component of Windows Server 2003. WIndows Server 2003
7.0 Built-in component of Windows Vista and Windows Server 2008. Windows Vista and WIndows Server 2008
7.5 Built-in component of Windows 7 and Windows Server 2008 R2. Windows 7 and Windows Server 2008 R2
8.0 Built-in component of Windows 8 and Windows Server 2012. Windows 8 and Windows Server 2012
8.5 Built-in component of Windows 8.1 and Windows Server 2012 R2. Windows 8.1 and Windows Server 2012 R2
10.0 Built-in component of Windows 10 and Windows Server 2016. Windows 10 and Windows Server 2016

IIS version?

https://gallery.technet.microsoft.com/how-to-obtain-versions-of-7875ac84

Windows Server 2016 Security Guide

https://download.microsoft.com/download/5/8/5/585DF9E9-D3D6-410A-8B51-81C7FC9A727C/Windows_Server_2016_Security_Guide_EN_US.pdf

Permissions

https://support.microsoft.com/en-us/help/271071/how-to-set-minimum-ntfs-permissions-and-user-rights-for-iis-5-x-or-iis

https://theitbros.com/using-icacls-to-list-folder-permissions-and-manage-files/

AppCmd.exe

http://techgenix.com/configuring-iis-7-command-line-appcmdexe-part1/

Typically, each of these commands would be used with an object type that you are asking AppCmd.exe to perform the requested function on.

Possibilities of the object type are:

  • Site – IIS virtual site
  • App – IIS application
  • Vdir – IIS virtual directory
  • Apppool – IIS application pool
  • Config – IIS general configuration
  • Backup – IIS server configuration backups (and the restore command is also available)
  • Wp – IIS worker processes
  • Request – active HTTP requests
  • Module – IIS server administration modules
  • Trace – IIS server trace logs

AppCmd.exe

AppCmd is the built-in CLI tool for configuring and managing IIS. You can use it to create sites and app pools, link virtual directories, and edit configurations. Let’s look at a few things it can do.

First of all, add %systemroot%\system32\inetsrv\ to your path so that you can run appcmd from a command prompt in any location.

Run the command prompt as administrator.

setx PATH "%PATH%;%systemroot%\system32\inetsrv\"

https://www.hitsubscribe.com/wp-content/uploads/2018/04/CLI_AppCmd_2-e1524627559701-300x243.png

Try the following commands to explore appcmd:

  • Run appcmd /? to see the help text
  • See what apps are running with appcmd list app
  • Use appcmd list backup to see backups of your IIS config

Add site

appcmd add site /name:"Dummy Site" /id:10 /bindings:http/*:81:

Now list apps again. You won’t see the new site you’ve added because it isn’t considered an app. If you go to the GUI and refresh your sites, you’ll see the new site there, but it’ll be broken. We need to add an app using appcmd.

appcmd add app /site.name:"Dummy Site" /path:"/"

This will only add the app to the site at the root. It will create an app named “Dummy Site/”. We still need to link the app to a virtual directory then point that to a physical path.

appcmd add vdir /app.name:"Dummy Site/" path:"/"

appcmd set vdir "Dummy Site/" /physicalPath:"c:\inetpub\wwwroot"

This is the verbose way to use appcmd to create an IIS site. There’s an easier way.

The EZ way

You can save a lot of keystrokes while creating the site if you set the physicalPath parameter in the first command. This will do the whole thing in one shot:

appcmd add site /name:"Dummy Site" /id:10 /bindings:http/*:81: /physicalPath:"c:\inetpub\wwwroot"

But knowing the other commands gives you a better idea of how an IIS app really works under the hood. In the past, I’ve used appcmd in the post-build script of ASP.NET proj files to ensure the site was set up locally on new developer machines. You can also do a backup and restore of IIS config using appcmd.

Deploying updates

To give you one more idea about using appcmd, consider doing the following:

  1. Create a “sites” folder.
  2. Create a subfolder for each site.
  3. Deploy versions to subfolders under each of those.
  4. Stage new versions.
  5. Use appcmd to update the site to use the new folder.

Given an app named “Hello World” pointing to C:\Sites\HelloWorld\1.0.0  and a new build “1.0.1” that’s been staged in C:\Sites\HelloWorld\1.0.1 , when it’s time to go live, then you can use the following command to flip the site to the new version:

appcmd set vdir "Dummy Site/" /physicalPath:"c:\Sites\HelloWorld\1.0.1"

appcmd recycle apppool /apppool.name:defaultapppool

And if you need to roll back your site, run the following:

appcmd set vdir "Dummy Site/" /physicalPath:"c:\Sites\HelloWorld\1.0.0"

appcmd recycle apppool /apppool.name:defaultapppool

Here’s a great guide from Microsoft with more information on using AppCmd.

IIS reset

“iisreset” is a separate executable used to stop, start, restart IIS or event to reboot the computer (/REBOOT). You can pass it a “computername” parameter to have it control IIS on another computer. You will need to run this as admin. Many developers and system admins like to use this after a deployment, and that’s not a bad idea either!

References

https://stackify.com/iis-web-server/

iCACLS - Set Permissions for IIS_IUSRS group (IIS7)

icacls "D:\Website\nopcommerce\App_Data" /grant IIS_IUSRS:(OI)(CI)F
icacls "D:\Website\nopcommerce\bin" /grant IIS_IUSRS:(OI)(CI)F
icacls "D:\Website\nopcommerce\Content" /grant IIS_IUSRS:(OI)(CI)F
icacls "D:\Website\nopcommerce\Content\Images" /grant IIS_IUSRS:(OI)(CI)F
icacls "D:\Website\nopcommerce\Content\Images\Thumbs" /grant IIS_IUSRS:(OI)(CI)F
icacls "D:\Website\nopcommerce\Content\Images\Uploaded" /grant IIS_IUSRS:(OI)(CI)F
icacls "D:\Website\nopcommerce\Content\files\ExportImport" /grant IIS_IUSRS:(OI)(CI)F
icacls "D:\Website\nopcommerce\Plugins" /grant IIS_IUSRS:(OI)(CI)F
icacls "D:\Website\nopcommerce\Plugins\bin" /grant IIS_IUSRS:(OI)(CI)F
icacls "D:\Website\nopcommerce\Global.asax" /grant IIS_IUSRS:(OI)(CI)F
icacls "D:\Website\nopcommerce\web.config" /grant IIS_IUSRS:(OI)(CI)F

 

IIS Create Application and Set Application Pool (C#)

September 12, 2018 11:48

IIS - Create Application and Set Application Pool (C#) (edit)

Chú ý:

+ Mở Visual Studio 2017 với quyền Administrator

+ Cài đặt Web Management Tools

Input

+ Tên Root Application: TSR

+ Tên Application: LightServices

+ Tên Customer: 412687

Output

+ Tạo Application nếu chưa tồn tại.

+ Sử dụng XCopy để copy toàn bộ files từ một nguồn cho trước đến Physical Path của Application vừa tạo.

+ Hoặc Unzip từ một nguồn là 1 file .zip cho trước.

+ Set ApplicationPool trùng với ApplicationPool của Root Application.

Solution

public class IISHelper
{
    private static string AppLightServices = "LightServices";
    private static string AppServices = "Services";
    private static string AppWebadmin = "Webadmin";
    private static string ApplicationPoolName = string.Empty;       // TSR
    private static string AppPath = string.Empty;                   // D:\TSRLight\412687\LightServices\
    private static string VirtualRootPhysicalPath = string.Empty;   // D:\TSRLight\
    private static bool IsLightServicesNotExists = true;            // False

    // Return 0: Not run
// Return 1: Run success
// Return -1: Throw exception public static int CreateApplicationLightServices(string customerCode, string siteName = "TSR", string sourceAppPath = "") { try { using (ServerManager manager = new ServerManager()) { foreach (Site s in manager.Sites) { foreach (Application app in s.Applications) { if (s.Name.ToLower().Equals(siteName.ToLower()) && app.Path.Contains(customerCode) && app.Path.Contains(AppServices)) { ApplicationPoolName = app.ApplicationPoolName; var applicationRoot = s.Applications.Where(a => a.Path == "/").Single(); var virtualRoot = applicationRoot.VirtualDirectories.Where(v => v.Path == "/").Single(); VirtualRootPhysicalPath = virtualRoot.PhysicalPath; } if (s.Name.ToLower().Equals(siteName.ToLower()) && app.Path.Contains(customerCode) && app.Path.Contains(AppWebadmin)) { ApplicationPoolName = app.ApplicationPoolName; var applicationRoot = s.Applications.Where(a => a.Path == "/").Single(); var virtualRoot = applicationRoot.VirtualDirectories.Where(v => v.Path == "/").Single(); VirtualRootPhysicalPath = virtualRoot.PhysicalPath; } if (s.Name.ToLower().Equals(siteName.ToLower()) && app.Path.Contains(customerCode) && app.Path.Contains(AppLightServices)) { IsLightServicesNotExists = false; } } } if (IsLightServicesNotExists && !string.IsNullOrWhiteSpace(ApplicationPoolName) && !string.IsNullOrWhiteSpace(VirtualRootPhysicalPath) && Directory.Exists(VirtualRootPhysicalPath)) { AppPath = Path.Combine(VirtualRootPhysicalPath, customerCode, AppLightServices); if (!string.IsNullOrWhiteSpace(AppPath) && !Directory.Exists(AppPath)) { Directory.CreateDirectory(AppPath); if (!string.IsNullOrWhiteSpace(sourceAppPath) && Directory.Exists(sourceAppPath)) { ProcessXcopy(sourceAppPath, AppPath); } else { var filePath = Path.Combine(Directory.GetCurrentDirectory(), "LightServices.zip"); if (File.Exists(filePath)) { ZipFile.ExtractToDirectory(filePath, AppPath); } } Site defaultSite = manager.Sites[siteName]; var app = defaultSite.Applications.Add($"/{customerCode}/{AppLightServices}", AppPath); app.ApplicationPoolName = ApplicationPoolName; manager.CommitChanges(); return 1; } else { return 0; } } else { return 0; } } } catch { return -1; } } /// /// /// private static void ProcessXcopy(string sourceDirectory, string targetDirectory) { // Use ProcessStartInfo class ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = false; startInfo.UseShellExecute = false; //Give the name as Xcopy startInfo.FileName = "xcopy"; //make the window Hidden startInfo.WindowStyle = ProcessWindowStyle.Hidden; //Send the Source and destination as Arguments to the process startInfo.Arguments = "\"" + sourceDirectory + "\"" + " " + "\"" + targetDirectory + "\"" + @" /e /y /I"; try { // Start the process with the info we specified. // Call WaitForExit and then the using statement will close. using (Process exeProcess = Process.Start(startInfo)) { exeProcess.WaitForExit(); } } catch (Exception exp) { throw exp; } } }

Tham khảo

Sử dụng thư viện Microsoft.Web.Administration.dll

Sử dụng thư viện System.IO.Compression.dllSystem.IO.Compression.FileSystem.dll

1) https://forums.iis.net/t/1207235.aspx?How+to+convert+a+physical+folder+into+an+Asp+NET+Application+using+Microsoft+Web+Administration+C+API

2) https://stackoverflow.com/questions/3228813/how-to-get-websites-physical-path-on-local-iis-server-from-a-desktop-app

3) http://www.anotherchris.net/csharp/6-ways-to-get-the-current-directory-in-csharp/

4) https://dailydotnettips.com/different-ways-of-getting-path/

5) https://www.c-sharpcorner.com/UploadFile/jawedmd/xcopy-using-C-Sharp-to-copy-filesfolders/

6) https://stackoverflow.com/questions/32236796/fastest-way-to-copy-files-from-one-directory-to-another (có sử dụng dữ liệu trong Database và Sql Server Connection)

https://www.c-sharpcorner.com/UploadFile/satisharveti/programmatically-create-iis-website-and-application-pool-usi/

https://stackoverflow.com/questions/16417578/iis-application-creation-by-using-microsoft-web-administration-library-creates-t

https://www.c-sharpcorner.com/UploadFile/225740/introduction-to-application-pool-in-iis/

https://stackoverflow.com/questions/12147721/change-apppool-of-application-programatically

Cấu hình IIS cho ứng dụng ASP.NET

June 28, 2017 22:01

Bước 01: Vào Start / Control Panel

Bước 02: Chọn Program and Features

Bước 03: Chọn Turn Windows features on or off , Chọn IIS (Internet Information Services) và chờ trong giây lát để Windows cài đặt dịch vụ

Bước 04: Đăng ký asp.net vào hệ thống với mã lệnh “aspnet_regiis –i” với quyền Administrator



Bước 05: Mở IIS bằng cách vào Start / Control Panel / Administrative Tools / chọn dịch vụ Internet Information Services (IIS) Manager

Bước 06: Tạo Application Pools phù hợp với .NET Framework v4.0.30319

Bước 07: Tạo Application để trỏ tới website ASPNET. Lưu ý: Website đã được Publish thành tập tin .DLL



Bước 08: Chạy website từ trình duyệt với 2 cách: thông qua IIS, thông qua trình duyệt Friefox, Google Chrome, …

Chúc bạn học tập tốt.

Categories

Recent posts