Bringing up the Django stack on MS Windows

image



This article will provide detailed instructions for installing and configuring Apache, Python and PostgreSQL programs to ensure the operation of a Django project on MS Windows. Django already includes a lightweight development server for testing your code locally, but production tasks require a more secure and powerful web server. We'll configure mod_wsgi to interact with our project and configure Apache as a gateway to the outside world.



It is worth noting that installation and configuration will be carried out in MS Windows 10 with 32 bit. Also 32 bit react will be universal and will work on 64 bit architecture. If you need a 64-bit installation, repeat the same steps for 64-bit distributions of programs, the sequence of actions will be identical.



We will use the Severcart program as a Django project. It is designed to control the movement of cartridges, accounting for printing equipment and supply and service contracts. All programs and modules will be installed in the C: \ severcart directory. The location is not important.



Python



The first step is to download and install Python from the Python website. We choose Windows as the operating system and 32-bit version. At the time of this writing, the current version is 3.9.0rc2.



After downloading the setup file, right-click the setup file and select "Run as administrator". You should see the screen below.







Set the checkboxes opposite the "Install launcher for add user (recomended)" and "Add Python 3.9 to PATH" checkboxes and click on "Customize installation".







Set the checkboxes against "pip", "py launcher", "for all users (requires elevation)" and click "Next".







Select all the input fields as in the picture above and click on "Install".







To verify that the installation was successful, open cmd and type python. If the installation was successful, you should see a prompt similar to the one below







Install mod_wsgi



Download the compiled mod_wsgi package from

www.lfd.uci.edu/~gohlke/pythonlibs . The module acts as an intermediary between the Apache server and the Django project. The most recent package will be named mod_wsgi-4.7.1-cp39-cp39-win32.whl. Note that the package is compiled for 32 bit Windows CPython version 3.9. It is also worth noting that the obvious installation of the pip install mod_wsgi module will most likely fail because the installation process requires the Visual Studio C ++ compiler. We consider it impractical to install the compiler entirely for the sake of one Python package on Windows.



Install the module using the standard package manager pip in cmd or powershell:



pip install -U mod_wsgi-4.7.1-cp39-cp39-win32.whl








Apache



Download the distribution kit from https://www.apachelounge.com/download/ .

The most recent version of the web server is Apache 2.4.46 win32 VS16. Also, for the program to work, you need a pre-installed package "Visual C ++ Redistributable for Visual Studio 2019 x86".



Unpack the Apache distribution into the C: \ severcart \ Apache24 directory, then change the line with the number 37 to our own



Define SRVROOT "C:/severcart/Apache24"




We check the operation of Apache by executing in the command line



C:/severcart/Apache24/bin> httpd.exe




As a result, you should see the line "It works!" In the browser at 127.0.0.1 .







Install the Apache service, for this we execute the instruction on the command line on behalf of the Administrator:



C:\severcart\Apache24\bin>httpd.exe -k install -n "Apache24"




Next, let's connect the mod_wsgi module to Apache. To do this, execute the instruction on the command line

C:\Windows\system32>mod_wsgi-express module-config




As a result, the following lines will be printed to standard output:

LoadFile "c:/severcart/python/python39.dll"
LoadModule wsgi_module "c:/severcart/python/lib/site-packages/mod_wsgi/server/mod_wsgi.cp39-win32.pyd"
WSGIPythonHome "c:/severcart/python"




Create a file C: \ severcart \ Apache24 \ conf \ extra \ httpd-wsgi.conf and copy and paste the printed lines above there.



Connect the new configuration to the main httpd.conf file

Include conf / extra / httpd-wsgi.conf



Save changes, restart Apache services

Net stop Apache24
Net start Apache24




PostgreSQL



Install PostgreSQL taken from the site https://postgrespro.ru/windows . The current version of the software product is 12. The advantages of the Russian distribution over the canonical one are presented on the same site.











































The installation steps are presented above and need no comments. Installation is extremely simple.



We create a database in postgres, where the data structures of the Django project will be stored later



C:\severcart\postgresql\bin>psql -h 127.0.0.1 -U postgres -W

CREATE DATABASE severcart WITH ENCODING='UTF8' OWNER=postgres CONNECTION LIMIT=-1 template=template0;








The database is created. Now we deploy the Django project.



Installing the web application



To do this, download the zip archive from the site https://www.severcart.ru/downloads/ and unpack it into the C: \ severcart \ app \ directory. Make







changes to the main configuration file C: \ severcart \ app \ conf \ settings_prod.py to indicate DB connection details







Python dictionary DATABASES contains DB connection details. For details on setting up, read here https://docs.djangoproject.com/en/3.1/ref/databases/#connecting-to-the-database



Install Python value packages for running applications inside the Django project



C:\severcart\app\tkinstaller>python install.py








In the course of the script operation, the database will be initialized with tables, constraints, indexes and others, and it will be proposed to create a user on whose behalf the program will be used.



We connect the Django application to the Apache server for this we supplement the

httpd-wsgi.conf configuration file with the following text



Alias /static "c:/severcart/app/static"

Alias /media "c:/severcart/app/media"

<Directory "c:/severcart/app/static">
    # for Apache 2.4
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<Directory "c:/severcart/app/media">
    # for Apache 2.4
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>


WSGIScriptAlias / "c:/severcart/app/conf/wsgi_prod.py"
WSGIPythonPath "c:/severcart/python/"

<Directory "c:/severcart/app/conf/">
<Files wsgi_prod.py>
    Require all granted
</Files>   
</Directory>



Restart the Apache service and test the application







. That's all. Thanks for reading.



In the next article, we will create an installation self-extracting archive in InnoSetup for quickly deploying a Django project on a customer's computer. For those who want to repeat all the steps on Yandex.Disk all used distributions are loaded.



All Articles