FLASK ON IIS

http://netdot.co/2015/03/09/flask-on-iis/

While Windows Server is usually not my preferred OS for deploying Python based web[sites|services] sometimes the situation dictates it. For situations where that is the case I’ve usually reached for Apache with mod_python until now. FastCGI is relatively straightfoward to setup on Windows Server 2012R2 and IIS using the Microsoft Web Platform Installer.

(NOTE: I’m using Flask, but any WSGI compatible Python app will work the same way)

Installing IIS

You’ll need to make sure that IIS is installed and configured with the CGI role service (this enables FastCGI as well).

Go to Server manager -> Manage -> Add Roles and Features:

Roles and Features

Choose Role-based or feature-based installation:

Role based

Select your server:

Server selection

Choose “Web Server (IIS)”:

Web Server (IIS)

Click “Add Features”:

Add Features

Accept the defaults until you reach the “Role Services” screen. At this point make sure “CGI” is selected:

Select CGI

Click next, accept defaults and click “Install”. You should see this screen:

IIS Installing

Launch the IIS Manager:

IIS Manager

If this message pops up, click “Yes” - otherwise you’ll need to download and install the Web Platform Installer if you haven’t already installed it:

Web Platform

Run the installer

Web Platform Installer

Search for WFastCGI

WFastCGI

Select the appropriate Python version (3.4 or 2.7.9). We’ll choose 3.4. Click “Add”, then click “Install”.
Read and accept the license (if you want to continue):

Add Install

Once you’re done - you should see this screen:

Install Complete

Setup your site - copy the wfastcgi.py from C:\Python34 (may be named C:\Python34_x86 if you had an existing Python34 directory) to your Flask application root:

wfastcgi.py in app root

Double click “Handler Mappings”

handler_mappings

Click “Add Module Mapping”

module mapping

Click “Request Restrictions”. Make sure “Invoke handler only if request is mapped to:” checkbox is unchecked:

request_restrictions

Click “Yes” here:

click_yes

Go to the root server settings and click “FastCGI Settings”:

fastcgi_settings

Double click it, then click the “…” for the Environment Variables collection to launch the EnvironmentVariables Collection Editor:

fastcgi_env_vars

Set the PYTHONPATH variable:

python_path

And the WSGI_HANDLER (my Flask app is named app.py so the value is app.app - if yours is named site.py it would be site.app or similar):

wsgi_handler

Click OK and browse to your site:

flask_site

Flask code below:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello from FastCGI via IIS!"

if __name__ == "__main__":
    app.run()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值