As we all know Flask, Flask is a web microframework written in python. Flask has a lot of advantages including (for myself) is an easy and agile way of developing web services. Unfortunately, not really agile in a way of deploying into production. That’s why I decided to make this article to tell people how difficult and hideous it is to deploy a Flask app to XAMPP Apache web service and another reason is to make sure I won’t get trouble if I had to deploy Flask again.

So, to deploy our Flask app, our main module to use is called _mod-wsgi. _This library is maintained by someone called Graham Dumpleton (even though I wrote this article without asking his approval), and I’d like to say thank you so much for maintaining this wonderful tools for us.

Now let’s jump into technical things. Before I know anything about mod-wsgi, I found a tutorial by Thilina Madumal on how exactly to serve a Flask app on Apache. And before you read this article any further, I suggest you read that article first in here.

Prerequisites

As mentions by Madumal in his article above, you’re going to need to prepare some installations.

  1. Visual C++ Redistributable for Visual Studio
  2. Visual C++ Build Tools (https://visualstudio.microsoft.com/visual-cpp-build-tools/)
  3. python3 (Installed for all user, so make sure installation directory doesn’t go to “C:/Users”)
  4. python3-pip
  5. XAMPP Apache

And this is going to be very important in windows machine

MAKE SURE YOU INSTALLED PYTHON, APACHE, AND VISUAL STUDIO AS THE SAME BINARY. For example, all of them are 32-bit version OR all of them are 64-bit version.

Because if you didn’t make all of that with the same binary, you’re going to face a lot of errors. This statement also mentioned by Graham in this link. As for me myself, I used the 64-bit version of Python 3.8, and 64-bit version of XAMPP Apache (yes, there is a 64-bit version of it).

After you installed all of that to your machine, I’m going to assume you have installed python and apache in an easy directory.

For Python directory, I’m assuming it will be residing in the program files directory, so it’s going to be something like “C:/Program Files/Python”. Because that’s the default path for all user installation. We are going to call that directory for this article.

And for XAMPP Apache, it would be something like “C:/XAMPP/Apache” and we’ll call that directory.

Run Apache as a Windows Service

Now, I can’t tell you right now on why would you need to install it as a windows service. Because I personally don’t understand it either. But from my personal experience, using apache as a windows service can help me understanding an occurring error. To do that, open up your command prompt as administrators.

Change your directory to /bin

cd C:/XAMPP/Apache/bin

After that type a command

httpd.exe -k install

This is what I mean by understanding errors in apache. Because as you might not know, the whole process of serving Flask in apache won’t show most errors in the error logs. And because of that, I was having a hard time understanding what’s going on with my apache. Using it as windows services I can control most of what’s going on with my apache.

Now after you installed it as windows services, you can use another command including start, stop, restart the server, or even uninstalling apache from windows service. Use the command like

#deployment #windows #apache-server #flask #python

Serve and Deploy Flask App With XAMPP Apache on Windows
48.15 GEEK