Skip to content

Windows Server

一、Windows Server 基础

1. Windows Server 版本

主要版本:

  • Windows Server 2016 - 长期支持版本
  • Windows Server 2019 - 长期支持版本(推荐)
  • Windows Server 2022 - 最新长期支持版本
  • Windows Server Core - 无 GUI 版本,更轻量

版本选择:

  • Standard - 标准版,适合中小型企业
  • Datacenter - 数据中心版,适合大型企业
  • Essentials - 基础版,适合小型企业

2. Windows Server 角色和功能

常见角色:

  • Active Directory Domain Services (AD DS) - 域服务
  • DNS Server - DNS 服务
  • DHCP Server - DHCP 服务
  • IIS (Internet Information Services) - Web 服务器
  • File and Storage Services - 文件服务
  • Hyper-V - 虚拟化

安装角色:

powershell
# 安装 IIS
Install-WindowsFeature -Name Web-Server -IncludeManagementTools

# 安装 AD DS
Install-WindowsFeature -Name AD-Domain-Services -IncludeManagementTools

# 安装 DNS
Install-WindowsFeature -Name DNS -IncludeManagementTools

二、IIS(Internet Information Services)

1. IIS 基础

IIS 是什么?

  • Windows Server 的 Web 服务器
  • 支持 ASP.NET、PHP 等
  • 提供 HTTP、HTTPS、FTP 等服务

2. 网站配置

创建网站:

powershell
# 使用 PowerShell
New-Website -Name "MySite" -Port 80 -PhysicalPath "C:\inetpub\wwwroot\mysite"

应用程序池:

  • .NET CLR 版本 - 选择 .NET Framework 版本
  • 托管管道模式 - Integrated(推荐)或 Classic
  • 启动模式 - AlwaysRunning(推荐)

配置示例:

xml
<!-- web.config -->
<system.webServer>
    <applicationPool>
        <processModel idleTimeout="00:20:00" />
    </applicationPool>
</system.webServer>

3. 虚拟目录

创建虚拟目录:

powershell
New-WebVirtualDirectory -Site "MySite" -Name "api" -PhysicalPath "C:\api"

4. URL 重写

安装 URL Rewrite 模块:

  • 下载并安装 IIS URL Rewrite 模块

配置示例:

xml
<rewrite>
    <rules>
        <rule name="Redirect to HTTPS" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTPS}" pattern="off" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
    </rules>
</rewrite>

5. SSL 证书配置

导入证书:

powershell
# 导入 PFX 证书
Import-PfxCertificate -FilePath "cert.pfx" -CertStoreLocation Cert:\LocalMachine\WebHosting

绑定 HTTPS:

powershell
New-WebBinding -Name "MySite" -Protocol https -Port 443 -SslFlags 1

三、Active Directory

1. AD DS 基础

Active Directory Domain Services (AD DS) 是 Windows Server 的目录服务。

核心概念:

  • 域(Domain) - 管理单元
  • 域控制器(DC) - 管理域的服务器
  • 组织单位(OU) - 组织单位
  • 用户账户 - 域用户
  • 计算机账户 - 加入域的计算机
  • 组(Group) - 用户组

2. 创建域

使用 PowerShell:

powershell
# 安装 AD DS
Install-WindowsFeature -Name AD-Domain-Services -IncludeManagementTools

# 创建新域
Install-ADDSForest -DomainName "example.com" -DomainNetbiosName "EXAMPLE" -InstallDns

使用图形界面:

  • 服务器管理器 → 添加角色和功能 → Active Directory 域服务

3. 用户管理

创建用户:

powershell
New-ADUser -Name "John Doe" -SamAccountName "jdoe" -UserPrincipalName "jdoe@example.com" -Enabled $true

修改用户:

powershell
Set-ADUser -Identity "jdoe" -Department "IT" -Title "Developer"

删除用户:

powershell
Remove-ADUser -Identity "jdoe"

4. 组管理

创建组:

powershell
New-ADGroup -Name "Developers" -GroupScope Global -GroupCategory Security

添加用户到组:

powershell
Add-ADGroupMember -Identity "Developers" -Members "jdoe"

四、PowerShell 管理

1. PowerShell 基础

常用命令:

powershell
# 获取服务
Get-Service

# 启动服务
Start-Service -Name "W3SVC"

# 停止服务
Stop-Service -Name "W3SVC"

# 获取进程
Get-Process

# 获取网络连接
Get-NetTCPConnection

# 获取磁盘使用情况
Get-PSDrive -PSProvider FileSystem

2. 远程管理

启用 PowerShell 远程:

powershell
Enable-PSRemoting -Force

远程执行命令:

powershell
Invoke-Command -ComputerName "Server01" -ScriptBlock { Get-Service }

3. 脚本示例

备份脚本:

powershell
# 备份 IIS 配置
Copy-Item "C:\Windows\System32\inetsrv\config\applicationHost.config" -Destination "C:\Backup\applicationHost.config.backup"

# 备份网站文件
Compress-Archive -Path "C:\inetpub\wwwroot\*" -DestinationPath "C:\Backup\website.zip"

五、Windows 服务

1. 服务管理

查看服务:

powershell
Get-Service | Where-Object {$_.Status -eq "Running"}

启动/停止服务:

powershell
Start-Service -Name "W3SVC"
Stop-Service -Name "W3SVC"
Restart-Service -Name "W3SVC"

设置服务自动启动:

powershell
Set-Service -Name "W3SVC" -StartupType Automatic

2. 创建 Windows 服务

使用 .NET:

csharp
using System.ServiceProcess;

public class MyService : ServiceBase
{
    protected override void OnStart(string[] args)
    {
        // 服务启动逻辑
    }
    
    protected override void OnStop()
    {
        // 服务停止逻辑
    }
}

安装服务:

bash
sc create MyService binPath= "C:\MyService.exe" start= auto

六、性能监控

1. 性能计数器

查看性能计数器:

powershell
Get-Counter "\Processor(_Total)\% Processor Time"
Get-Counter "\Memory\Available MBytes"
Get-Counter "\Web Service(_Total)\Current Connections"

2. 任务管理器

查看资源使用:

  • CPU 使用率
  • 内存使用率
  • 磁盘 I/O
  • 网络 I/O

3. 资源监视器

resmon.exe - 详细的资源监控工具

4. 事件查看器

查看系统日志:

powershell
Get-EventLog -LogName System -Newest 10
Get-EventLog -LogName Application -Newest 10

七、安全配置

1. 防火墙

配置防火墙规则:

powershell
# 允许端口
New-NetFirewallRule -DisplayName "Allow HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow

# 阻止端口
New-NetFirewallRule -DisplayName "Block Port 3389" -Direction Inbound -Protocol TCP -LocalPort 3389 -Action Block

2. 用户权限

最小权限原则:

  • 使用普通用户运行服务
  • 避免使用管理员账户
  • 使用服务账户

3. 更新管理

Windows Update:

powershell
# 检查更新
Get-WindowsUpdate

# 安装更新
Install-WindowsUpdate

4. 审核策略

启用审核:

powershell
auditpol /set /category:"Logon/Logoff" /success:enable /failure:enable
auditpol /set /category:"Object Access" /success:enable /failure:enable

八、备份和恢复

1. Windows Server Backup

安装备份功能:

powershell
Install-WindowsFeature -Name Windows-Server-Backup

创建备份:

powershell
wbadmin start backup -backupTarget:E: -include:C: -allCritical

2. 系统还原

创建还原点:

powershell
Checkpoint-Computer -Description "Before Update" -RestorePointType "MODIFY_SETTINGS"

3. 文件备份

使用 Robocopy:

powershell
robocopy C:\Source D:\Backup /MIR /R:3 /W:5

九、常见面试题

Q1: IIS 和 Apache 的区别?

特性IISApache
平台Windows跨平台
性能良好优秀
配置图形界面 + 配置文件配置文件
成本商业授权开源
.NET 支持原生支持需要模块

Q2: 如何优化 IIS 性能?

  1. 应用程序池配置

    • 设置合适的进程数
    • 配置回收条件
    • 使用 AlwaysRunning 模式
  2. 启用压缩

    • 启用动态压缩和静态压缩
  3. 缓存配置

    • 配置输出缓存
    • 配置内核缓存
  4. 连接限制

    • 设置最大并发连接数

Q3: Active Directory 的作用?

  1. 集中管理 - 统一管理用户和计算机
  2. 单点登录 - 一次登录访问所有资源
  3. 策略管理 - 组策略统一配置
  4. 安全性 - 集中认证和授权

Q4: 如何实现 IIS 负载均衡?

方案:

  1. Application Request Routing (ARR) - IIS 的负载均衡模块
  2. 网络负载均衡 (NLB) - Windows Server 的负载均衡功能
  3. 硬件负载均衡器 - F5、Citrix 等

Q5: Windows Server 的虚拟化?

Hyper-V:

  • Windows Server 的虚拟化平台
  • 支持创建和管理虚拟机
  • 支持动态内存、实时迁移等高级功能

十、最佳实践

  1. 定期更新 - 安装安全补丁
  2. 最小权限 - 使用普通用户运行服务
  3. 启用防火墙 - 只开放必要端口
  4. 监控性能 - 定期检查资源使用
  5. 备份数据 - 定期备份重要数据
  6. 日志审计 - 启用审核日志
  7. 安全配置 - 禁用不必要的服务
  8. 文档记录 - 记录配置和变更
  9. 不要使用默认密码 - 修改默认配置
  10. 不要忽略日志 - 定期查看事件日志

基于 VitePress 构建 | Copyright © 2026-present