Raising the Django stack on MS Windows

Raising the Django stack on MS Windows

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

It should be noted that installation and configuration will be carried out in MS Windows 10 with 32 bits. Also 32 bit reaction will be universal and will work on 64 bit architecture. If you need a 64-bit installation, repeat the same steps for 64-bit software distributions, the sequence of actions will be identical.

As a Django project, we will use the Severcart program. It is designed to manage 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. Location doesn't matter.

Python

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

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

Raising the Django stack on MS Windows

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

Raising the Django stack on MS Windows

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

Raising the Django stack on MS Windows

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

Raising the Django stack on MS Windows

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.

Raising the Django stack on MS Windows

Installing mod_wsgi

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

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

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

Raising the Django stack on MS Windows

Apache

Downloading the distribution kit from the site https://www.apachelounge.com/download/.
The latest 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".

We unpack the Apache distribution into the C: severcartApache24 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 on the command line

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

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

Raising the Django stack on MS Windows

Install the Apache service, to do this, execute the instruction on the command line as Administrator:

C:severcartApache24bin>httpd.exe -k install -n "Apache24"

Next, we will connect the mod_wsgi module to Apache. To do this, execute the instruction on the command line

C:Windowssystem32>mod_wsgi-express module-config

This will print the following lines 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:severcartApache24confextrahttpd-wsgi.conf and copy-paste the printed lines above there.

We 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.

Raising the Django stack on MS Windows

Raising the Django stack on MS Windows

Raising the Django stack on MS Windows

Raising the Django stack on MS Windows

Raising the Django stack on MS Windows

Raising the Django stack on MS Windows

Raising the Django stack on MS Windows

Raising the Django stack on MS Windows

Raising the Django stack on MS Windows

Raising the Django stack on MS Windows

Installation steps are presented above and do not need comments. Installation is extremely simple.

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

C:severcartpostgresqlbin>psql -h 127.0.0.1 -U postgres -W

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

Raising the Django stack on MS Windows

The DB has been created. Now let's 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 to C:severcartapp directory

Raising the Django stack on MS Windows

We make changes to the main configuration file C: severcartappconfsettings_prod.py to specify the database connection details

Raising the Django stack on MS Windows

Python dictionary DATABASES contains database connection details. Read more about setup here. https://docs.djangoproject.com/en/3.1/ref/databases/#connecting-to-the-database

Installing Python Feature Packs to Run Applications Inside a Django Project

C:severcartapptkinstaller>python install.py

Raising the Django stack on MS Windows

During the script's operation, the database will be initialized with tables, constructs, indexes, and others, and it will be proposed to create a user on whose behalf work will be performed in the program.

We connect the Django application to the Apache server, for this we supplement the configuration file
httpd-wsgi.conf 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

Raising the Django stack on MS Windows

That's all. Thank you for reading.

In the next article, we will create an installation self-extracting archive in InnoSetup to quickly deploy 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.

Source: habr.com

Add a comment