Flask Development in Apache Server in Windows

Dinesh Jinjala
5 min readJul 13, 2019

To install Apache Server

goto this link: https://www.apachelounge.com/download/

Make sure that you have the latest visual c++ redistributables. If not then you can download that from the above link. You can download the version that suits your operating system. Then extract the files then you will get three files:

  1. Apache24
  2. — Win64 VS16 —
  3. ReadMe

Copy the Apache24 and paste it to the C drive: So now the path of the bin folder will be: C:\Apache24\bin

You have to add this path to the Environment Variables.

To add into Environment Variables:

  1. Right-click on- This PC and select properties.
  2. Click on Advanced system settings
  3. Click on Environment Variables
  4. Find Path in System variables
  5. Click on Path, which leads to a new window where we can add a new path by clicking “New” and paste the path: C:\Apache24\bin

Now open CMD in administrator mode, and type

C:\WINDOWS\system32> httpd -h

This will give some output like this:

Usage: httpd [-D name] [-d directory] [-f file]
[-C “directive”] [-c “directive”]
[-w] [-k start|restart|stop|shutdown] [-n service_name]
[-k install|config|uninstall] [-n service_name]
[-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X]

Now we have to install the service of the apache, so to install the service run:

httpd -k install

Now we have to restart the service:

httpd -k restart

If this fails or gives some error

AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using fe80::f060:7f88:c94b:5bb9. Set the ‘ServerName’ directive globally to suppress this message

then we have to go to C:\Apache24\conf.

And we have to open the httpd.conf file in the desired text editor.

In httpd.conf, we have to set the ServerName, so to set ServerName: find where is ServerName is there in that file. Most probably it will be commented.

So there you can change it like :

ServerName localhost:80

After setting this run :

httpd -k restart

Now it will not give the error.

Now open the browser and type: localhost:80 and response will be like :

It works!

then you have installed apache server perfectly.

Now create the flask app:

Create a directory where ever you want:

I named my directory :

mkdir flaskapp
cd flaskapp/
virtualenv -p python .venv

After running these three commands in command prompt (Make sure that I have gitbash installed and I use gitbash to do this), the output will be like this:

Running virtualenv with interpreter F:\Program Files\Python3\python.exe
Already using interpreter F:\Program Files\Python3\python.exe
Using base prefix ‘F:\\Program Files\\Python3’
New python executable in D:\LEARNING\Blogs\Medium\Flask development using Apache Server\flaskapp\.venv\Scripts\python.exe
Installing setuptools, pip, wheel…
done.

Now activate the environment:

source .venv/Scripts/activate

( Make sure that I have used gitbash)

Now the virtual environment is activated.

Create a new *.py file called:

vim hello.py 

This will open the vim and then you can paste the below code in that.

from flask import Flask
app = Flask(__name__)
@app.route(“/”)
def hello():
return “Hello World!”
if __name__ == “__main__”:
app.run()

and save it and get exit from that.

Now you can run this flask app but first, we need to install flask:

To install flask:

(.venv) 
Dinesh@VEDANTA MINGW64 /d/LEARNING/Blogs/Medium/Flask development using Apache Server/flaskapp
$ pip install flask

(.venv) — shows that our virtual environment is activated.

after installation, you can run the script:

python hello.py

This will show some output like this :

$ python hello.py
* Serving Flask app “hello” (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Now go to browser and paste:

http://127.0.0.1:5000/

It will show the output something like this.

Hello World!

So now we have created one flaskapp on a development server. But our main aim for this blog is to create the app on the production server using WSGI and apache.

To deploy to the production:

Activate the virtual environment and then install mod_wsgi package.

pip install mod_wsgi

Now we have to configure this mod_wsgi in Apache server. To do that run following command:

mod_wsgi-express module-config

if it gives an error like:

bash: mod-wsgi-express: command not found

In that case, you can visit: https://stackoverflow.com/questions/45110264/mod-wsgi-express-error-invalid-command-was-specified

If it is successful then it will give the output like:

LoadFile “F:/Program Files/Python3/python36.dll”
LoadModule wsgi_module “d:/learning/blogs/medium/flask development using apache server/flaskapp/.venv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd”
WSGIPythonHome “d:/learning/blogs/medium/flask development using apache server/flaskapp/.venv”
  • Copy the output of the above command and paste it in <Apache24-Home>\conf\httpd.conf. We can paste at the very end of the file.
  • Deactivate flask-app-env and close the command prompt.

Make the Apache Server Listen to a Desired Port

The first thing we need to do is to make the Apache Server listen to the port that we plan to expose the Flask App through. By default, Apache Server listens to the port 80 (the default HTTP port).

However, if we want to make Apache Server listen to a particular port do the following;

  • Add “Listen <port>” to <Apache24 root>/conf/httpd.conf

I have added below line httpd.conf file:

Listen 5000

After that when I run

localhost:5000

It fails!!! So I have to restart the server by opening cmd in administrator mode and run below command:

httpd -k restart

Whenever you do any changes in httpd.conf file or other .conf file you must restart the server.

Configure a Virtual Host for the Flask app:

In Apache 2.4 Server virtual host configurations are placed in the <Apache24-Home>\conf\extra\httpd-vhosts.conf file. Thus we need to edit that particular file.

  • Open <Apache24-Home>\conf\extra\httpd-vhosts.conf for editing.
  • Add the following virtual host configuration segment with corresponding changes into <Apache24-Home>\conf\extra\httpd-vhosts.conf.
# virtual SupervisionTool
<VirtualHost *:5000>
ServerName localhost:5000
WSGIPassAuthorization On
ErrorLog "logs/my_application.error.log"
CustomLog "logs/my_application.access.log" combined
WSGIScriptAlias / "D:\LEARNING\Blogs\Medium\Flask development using Apache Server\flaskapp\wsgi.py"
<Directory "D:\LEARNING\Blogs\Medium\Flask development using Apache Server\flaskapp">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
# end virtual SupervisionTool

After pasting this you must make wsgi.py file in our app directory. The path of that file will be (You can verify that you are going in the right direction or not) :

“D:\LEARNING\Blogs\Medium\Flask development using Apache Server\flaskapp\wsgi.py”

Content of the wsgi.py fille

# File name: wsgi.py
import sys, os, logging
logging.basicConfig(stream=sys.stderr)
PROJECT_DIR = "D:/LEARNING/Blogs/Medium/Flask development using Apache Server/flaskapp"
virtual_env = "D:/LEARNING/Blogs/Medium/Flask development using Apache Server/flaskapp/.venv"
activate_this = os.path.join(virtual_env, 'Scripts', 'activate_this.py')
exec(open(activate_this).read(),dict(__file__=activate_this))
sys.path.append(PROJECT_DIR)
from hello import app as application

Now restart the server:

httpd -k restart

Now go to localhost:5000 in a browser and see the output is coming or not.

My output:

Hello World!

That’s all. Happy Falsking on Windows! :D

--

--

Dinesh Jinjala

I am an AI Scientist with interest in painting, playing carom and to do work with awareness.