Deploying A Django Application in Windows With Apache and Mod - Wsgi
Deploying A Django Application in Windows With Apache and Mod - Wsgi
Deploying A Django Application in Windows With Apache and Mod - Wsgi
LoadFile "c:/users/admin/appdata/local/programs/python/python38/python38.dll"
WSGIPythonHome "c:/users/admin/appdata/local/programs/python/python38"
Python
The first step is to download and install Python from the Python website.
Select Windows as operating system and pick the 32 / 64 bit version
based on your machine requirements.
Once you download the setup file, right click the setup file and select Run
as Administrator. You should see the below screen
Make sure both the checboxes at the bottom of the window are checked
(Install for all users, and Add Python 3.7 to PATH checkboxes)
Click on Install Now. Once the installation is completed, you can close
the window. To make sure the installation is successful, open
Powershell and type python. If the installation was successful, you
should see an output similar to below
MySQL
virtualenv
mkvirtualenv my_application
workon my_application
Install Django
Next, let's create a sample Django project. Type the following command
in the powershell window.
my_application
| manage.py
|
\---my_application
settings.py
urls.py
wsgi.py
__init__.py
Now let's run the server and check that it can be accessed from the
browser
cd my_application
Once the server starts running, you should see a similar output in your
powershell terminal
Now let's access the server from the browser. Go to the URL
- http://127.0.0.1:8000/ from your browser and you should see the
following screen
Now you can stop the server by pressing Ctrl + C in the powershell
terminal.
Local setup
Next step is to provide the appropriate DB settings, e.t.c for the Django
application. Open your my_application/settings.py file and replace the
DATABASES variable to the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_application',
'USER': '<my-user>',
'PASSWORD': '<my-password>',
'HOST': 'localhost',
'PORT': 3306
In production environment, Django does not serve static files (css, js and
images). Inorder for them to be server properly by Apache, let's
configure the staticfile settings in the application. Add the following at the
end of your my_application/settings.py file
Once the details are entered, create the database tables, and collect the
staticfiles by running
my_application
+---my_application
| \---__pycache__
\---static
\---admin
+---css
| \---vendor
| \---select2
+---fonts
+---img
| \---gis
\---js
+---admin
\---vendor
+---jquery
+---select2
| \---i18n
\---xregexp
Apache
You will need to restart the powershell window for this change to take
effect.
wsgi.py
activate_this =
'C:/Users/myuser/Envs/my_application/Scripts/activate_this.py'
# execfile(activate_this, dict(__file__=activate_this))
exec(open(activate_this).read(),dict(__file__=activate_this))
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('C:/Users/myuser/Envs/my_application/Lib/site-
packages')
sys.path.append('C:/Users/myuser/my_application')
sys.path.append('C:/Users/myuser/my_application/my_application')
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_application.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"my_application.settings")
application = get_wsgi_application()
mod_wsgi
mod_wsgi-express module-config
LoadFile
"c:/users/myuser/appdata/local/programs/python/python37/python37.
dll"
LoadModule wsgi_module
"c:/users/myuser/appdata/local/programs/python/python37/lib/site-
packages/mod_wsgi/serve
r/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome
"c:/users/myuser/appdata/local/programs/python/python37"
# virtual SupervisionTool
<VirtualHost *:80>
ServerName localhost
WSGIPassAuthorization On
ErrorLog "logs/my_application.error.log"
WSGIScriptAlias /
"C:\Users\myuser\my_application\my_application\wsgi_windows.py"
<Directory "C:\Users\myuser\my_application\my_application">
<Files wsgi_windows.py>
</Files>
</Directory>
</Directory>
</VirtualHost>
With this setup, you should now be able to go to the System Services
panel, start the Apache service and you should be able to access the
application at http://localhost