It took me about 3 days to figure out how to deploy a flask application on a windows server using Apache so I thought I should write a tutorial to help someone else and for my own reference in the feature. Please let me know if you have any questions because I know this is not an easy task to understand. If you have any questions feel free to ask.

  1. Install python for all users. Note install it for all users.
  2. Set python as the environmental variable like soEnvironmental Variables

3. Open the comandline and Run pip install virtualenv like so

            C:\>pip install virtualenv

4. Create a directory called myapp

            C:\>mkdir myapp

5. Run

            C:\>cd myapp

6. Run virtualenv Flask

          C:\myapp>virtualenv Flask

7. Run

           i. C:\myapp>cd  /Flask/Scripts

8. Run

         C:\myapp\Flask\Scripts>pip install flask

Now you have installed your flask infrastructure

We are going to create a file in our root directory called index.py using any text editor like notpad or notepad++

9. Past the content below into it

from flask import Flask
 app = Flask(__name__)
 
 @app.route("/")
 def hello():
     return "Hello from FastCGI via IIS!"
 
 if __name__ == "__main__":
     app.run()

It will look like this

index one

10. Now go to the command prompt and run

        C:\myapp\Flask\Scripts>activate

11. Run

      i. (flask) C:\myapp\Flask\Scripts>cd ..

      ii. (flask) C:\myapp\Flask>cd ..

iii. (flask) C:\myapp>cd app

       iii. And Run (flask) C:\myapp\app>python index.py

It will look like this

command line

12. Now open your browser and type in  http://localhost:5000

localhost 5000

We have now setup flask to run on the development server.

GOOD READ: HOW TO RUN PYTHON FLASK FROM IIS

Now we have to configure apache to run python and serve it to the world.

13. Go to https://httpd.apache.org/docs/2.4/platform/windows.html and download wamp or apache standaloan from here or https://www.apachelounge.com/download/

14. Extract the files and copy Apache24 to  C:\Apache24

15. Navigate to C:\Apache24\bin and run or click on httpd.exe to start apache

Start apache

16. Go to your browser and type in http://localhost

localhost

We have now setup a development server for apache.

Now lets configure the system to run python from apache. But before we start, go here and download mod_wsgi. Without it you cannot run python flask on windows.

https://github.com/GrahamDumpleton/mod_wsgi/releases/download/4.4.12/mod_wsgi-windows-4.4.12.tar.gz

Note: if you are using 64bit system download 64bit Apache,64bit python and use the 64bit mod_wsgi.so  and if you are using a 32bit system use everything that is 32bit.

Read more on this from here https://github.com/GrahamDumpleton/mod_wsgi

17. Look for the right mod_wsgi.so file and copy it to C:\Apache24\modules and past it.

18. Navigate to C:\Apache24\conf and open httpd.conf in a text editor

19.Add LoadModule wsgi_module modules/mod_wsgi.so to the modules

20. Remove the # from LoadModule cgi_module modules/mod_cgi.so

21. Add +ExecCGI to your Options it should look like this

<Directory "c:/Apache24/htdocs">
   Options Indexes FollowSymLinks +ExecCGI
   AllowOverride None
   Require all granted
</Directory>

22. Locate the tag <IfModule mime_module> and inside it, add

 AddHandler cgi-script .cgi
 AddHandler cgi-script .py

23. Locate this tag <IfModule dir_module> and inside it add index.py and it will look like this

<IfModule dir_module>
 DirectoryIndex index.php index.php3 index.html index.htm index.cgi index.py
</IfModule>

24. Now create another file called index.py inside the root directory here C:\myapp>

25. Add this code to it

#!C:\Python34\python.exe
import cgitb
cgitb.enable()
print("Content-Type: text/html;charset=utf-8")
print()
print("Hello World!")

26. Restart apache

27. Go to your browser and type http://localhost/index.py. It will say Hello World meaning you are now running python scripts on apache server

Hello World

28. Now find out how to use virtual host in apache if you have no idea. Add the code below to the virtual host file

<VirtualHost *:80>
 ServerName localhost
 WSGIScriptAlias / "C:/myapp/app/web.wsgi"
 DocumentRoot C:/myapp/app
 <Directory "C:/myapp/app/">
 Options +Indexes +Includes +FollowSymLinks +MultiViews +ExecCGI
 AllowOverride All
 Require local
 </Directory>
</VirtualHost>

29. Create a file called web.wsgi in the root of your application like so

import sys
 
sys.path.append('C:/myapp/app/')
 
from app import app as application

 

30. Now create another file in the root of your application called app.py and add the following code.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
 return "Hello from FastCGI via IIS!"
if __name__ == "__main__":
 app.run()

31. Restart appache

32. Navigate to http://localhost and you will see this

apache server is running python

Useful links

Configure server to compile mod_wsgi – https://wiki.python.org/moin/WindowsCompilers

Download mod_wsgi.so file – http://grapevine.dyndns-ip.com/download/folder.asp?eid=33&folder=%2Fdownload%2Fapache%2Fmod_wsgi-windows-4.4.6%2Fmod_wsgi-windows-4.4.6%2Fapache24-win64-vc10%2Fmodules%2F

Download mod_wsgi.so files – https://github.com/GrahamDumpleton/mod_wsgi/releases/download/4.4.12/mod_wsgi-windows-4.4.12.tar.gz

 

 

If you enjoyed this article, share or tell a friend and be on the lookout for more from thetallbay.com

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *