
ช่วงนี้มี project ต้องใช้ python (ใช้ TensorFlow สร้าง model เรียกใช้งาน model ผ่าน backend ที่เป็น python ใช้ web input ข้อมูลสำหรับ train model ใหม่อีก) solution หลายๆ อย่างเลยลงตัวที่ python platform วันนี้ เลยต้องมานั่ง set Django บน IIS
1.Install Web Platform Installer
ดาวน์โหลดตัวช่วยมาก่อนเลยครับ https://www.microsoft.com/web/downloads/platform.aspx
ค้น CGI => เลือก WFastCGI 2.1 Gateway for IIS Python 3.4 => กด Add กด Install
รอจนกระทั้ง install Python เรียบร้อย
2 Add Python Path
3 install pip (Package Manager )
download ไฟล์ https://bootstrap.pypa.io/get-pip.py
เปิด command line แล้ว run : python get-pip.py
4 install Django 1.11.2
py -m pip install Django==1.11.2
add path เพิ่ม ทำเหมือนข้อ 2
C:\Python34\Lib\site-packages\django\bin
5 สร้าง Django Project
django-admin startproject djangotest
จะได้ djangotest โฟล์เดอร์ พร้อมไฟล์ต่างๆ
ทดสอบ run django app เข้าไปที่โฟล์เดอร์ djangotest แล้วสั่ง run : manage.py runserver
6 Add Site
เปิด IIS Manager คลิกขวา เลือก Add Web Site
ตั้งชื่อ Site name : DjangoTestOnIIS
เลือก Content Directory เป็น folder djangotest (folder ที่เก็บไฟล์ manage.py)
port เลือกเป็น 8080 (เปลี่ยนเองนะครับ) แล้วก็กด ok ไปครับ
7 Add Handler Mappings
copy file C:\Python34\Scripts\wfastcgi.py ไปไว้ที่ D:\djangotest
เลือก DjangoTestOnIIS => เลือก Handler Mappings
เลือก Add Module Mapping
ตั้งค่าดังนี้
8 Test Site
run ได้แล้วครับ แต่เจอ error นิดหน่อย
9 ตั้งค่า web.config
หลังจาก add module mapping ใน djangotest project จะมีไฟล์ web.config เพิ่มเข้ามา
web.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <handlers> <add name="DjangoTestMapping" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\python34\python.exe|D:\djangotest\wfastcgi.py" resourceType="Unspecified" /> </handlers> </system.webServer> <appSettings> <!-- Required settings --> <add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()" /> <add key="PYTHONPATH" value="D:\djangotest" /> <!-- Optional settings --> <add key="WSGI_LOG" value="D:\logs\mr.log" /> <add key="WSGI_RESTART_FILE_REGEX" value=".*((\.py)|(\.config))$" /> <add key="APPINSIGHTS_INSTRUMENTATIONKEY" value="__instrumentation_key__" /> <add key="DJANGO_SETTINGS_MODULE" value="djangotest.settings" /> <add key="WSGI_PTVSD_SECRET" value="__secret_code__" /> <add key="WSGI_PTVSD_ADDRESS" value="ipaddress:port" /> </appSettings> </configuration> |
test site
สำเร็จแล้ว ยินดีต้อนรับสู่โลกของ Django
**คำถาม manage.py runserver ก็ run server ได้ทำไมต้องมา set iis ให้ยุ่งยากด้วย
django สร้าง manage.py ให้ run test django ได้ง่ายๆ ไม่ได้สร้างมาเพื่อ production ดั้งนั้น เรื่อง security กับ performance ก็จะสู้ web server ไม่ได้ นั้นเอง อีกอย่าง django เป็น web framework ไม่ใช่ web server
https://docs.djangoproject.com/en/1.11/ref/django-admin/