IIS (Internet Information Services) là web server tích hợp sẵn trong Windows Server, dùng để host website ASP.NET, PHP, HTML tĩnh. Miễn phí, không cần cài thêm phần mềm.
Bước 1: Cài IIS trên Windows Server
Cách 1 — Server Manager (giao diện đồ họa):
1. Mở Server Manager → Add roles and features
2. Nhấn Next liên tục đến bước Server Roles
3. Tích vào Web Server (IIS) → Add Features → Next
4. Ở bước Role Services, tích thêm:
- Common HTTP Features (mặc định đã chọn)
- Application Development → ASP.NET 4.8, CGI (nếu cần PHP)
- Security → Basic Authentication, Windows Authentication
- Management Tools → IIS Management Console
5. Nhấn Install → đợi xong
Cách 2 — PowerShell (nhanh hơn):
Install-WindowsFeature -Name Web-Server -IncludeManagementTools
Install-WindowsFeature -Name Web-Asp-Net45
Install-WindowsFeature -Name Web-CGI
Install-WindowsFeature -Name Web-Basic-Auth
Install-WindowsFeature -Name Web-Windows-Auth
# Kiểm tra đã cài xong
Get-WindowsFeature | Where-Object {$_.Name -like "Web-*" -and $_.Installed}
Bước 2: Kiểm tra IIS đang chạy
Mở trình duyệt, truy cập http://localhost → thấy trang "IIS Windows Server" là thành công.
# Kiểm tra service
Get-Service W3SVC
# Khởi động IIS
Start-Service W3SVC
Set-Service W3SVC -StartupType Automatic
Bước 3: Tạo website mới trong IIS
1. Mở IIS Manager (Win + R → inetmgr)
2. Bên trái, chuột phải vào Sites → Add Website
3. Điền thông tin:
- Site name: tên website (ví dụ: mysite)
- Physical path: thư mục chứa code (ví dụ: C:\inetpub\mysite)
- Binding: IP = All Unassigned, Port = 80, Host name = domain của bạn
4. Nhấn OK
# Tạo thư mục web
New-Item -Path "C:\inetpub\mysite" -ItemType Directory
# Tạo file index test
Set-Content "C:\inetpub\mysite\index.html" "
Hello from IIS!
"
Bước 4: Cài PHP cho IIS
Dùng Web Platform Installer hoặc cài thủ công:
# Tải PHP (Non-Thread Safe) từ https://windows.php.net/download/
# Giải nén vào C:\PHP
# Copy php.ini
Copy-Item "C:\PHP\php.ini-production" "C:\PHP\php.ini"
# Thêm PHP vào PATH
[Environment]::SetEnvironmentVariable("PATH", $env:PATH + ";C:\PHP", "Machine")
Thêm PHP vào IIS qua Handler Mappings:
1. IIS Manager → chọn site → Handler Mappings → Add Module Mapping
2. Request path: *.php, Module: FastCgiModule, Executable: C:\PHP\php-cgi.exe
Bước 5: Cấu hình HTTPS với SSL
# Tạo self-signed cert (test)
New-SelfSignedCertificate -DnsName "yourdomain.com" -CertStoreLocation "cert:\LocalMachine\My"
Hoặc import cert từ file .pfx:
IIS Manager → chọn server → Server Certificates → Import → chọn file .pfx → nhập password.
Thêm HTTPS binding cho site: chuột phải site → Edit Bindings → Add → HTTPS, port 443, chọn certificate.
Bước 6: Mở firewall cho port 80 và 443
New-NetFirewallRule -DisplayName "HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow
New-NetFirewallRule -DisplayName "HTTPS" -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow
Bước 7: Các lệnh quản lý IIS thường dùng
# Restart IIS
iisreset
# Restart 1 site cụ thể
Import-Module WebAdministration
Stop-WebSite "mysite"
Start-WebSite "mysite"
# Xem tất cả site
Get-Website
# Xem application pool
Get-WebConfiguration system.applicationHost/applicationPools/add | Select-Object name,state
# Xem log IIS (mặc định)
Get-Content "C:\inetpub\logs\LogFiles\W3SVC1\*.log" | Select-Object -Last 20
Cấu trúc thư mục IIS mặc định:
C:\inetpub\wwwroot— thư mục web mặc địnhC:\inetpub\logs— log filesC:\Windows\System32\inetsrv— IIS binariesC:\Windows\System32\inetsrv\config\applicationHost.config— config chính
Lỗi thường gặp:
- 403 Forbidden: Thiếu file index.html/index.aspx, hoặc sai quyền thư mục → chuột phải thư mục → Properties → Security → thêm IIS_IUSRS với quyền Read
- 500 Internal Server Error: Lỗi code hoặc thiếu .NET version → kiểm tra Event Viewer → Windows Logs → Application
- Port 80 đã bị dùng: Skype hoặc phần mềm khác chiếm port →
netstat -ano | findstr :80tìm PID rồi kill