AYUSHI SHARMA
May 17, 2019
·
10 min read
Though many Django Developer might find it might consider it blasphemous, sometimes it is actually necessary to deploy Django applications on Windows/IIS, especially when working with a client that has based its infrastructure around the Windows ecosystem.
This short, focused tutorial walks you through the basic setup of a Django project on Windows. It covers the installation of Python, activating environment.
- Download the latest Windows x86–64 executable installer from https://python.org
- Double-click the Python installer. If you get a security warning at this point, click “Run” to continue.
- In the Python Setup dialog that appears, click “Customize Installation”
4. On the Optional Features step, leave the default settings and click “Next”
5. On the Advanced Options step, make the following changes:
- Check the “Install for all users” box (note that this will also check the “Precompile standard library” box)
- Check “Add Python to environment variables”
- In the input box below “Customize install location” change the value to the following: C:\Python35
6. Click “Install”
7. When the installation is complete, click “Close”
Confirm the Python Installation
- Open a new Command Prompt. Note that if you already had a command prompt open you will need to close and reopen it, as the Python installation process added new environment variables and any open command prompts will not have the new environment variables available.
- Type python and press Enter. This should open a Python interpreter:
If instead of seeing the Python interpreter you see the error “‘python’ is not recognized as an internal or external command …” this likely means the “Add Python to environment variables” box was not checked on the Advanced Options step of the installation process. If this occurs you can uninstall and reinstall Python and make sure to check the “Add Python to environment variables” checkbox, or add the following to the System environment “Path” variable:
C:\Python35;C:\Python35\Scripts
- Exit the Python interpreter by typing exit() and pressing Enter.
- Close the Command Prompt.
Python virtual environments are used to isolate application-specific Python environments from the system-wide Python environment. Even if you will only be running one application on a server, it’s good practice to create a virtual environment for your application to keep the virtual environment free from any unanticipated side effects of system updates.
1. Open a Command Prompt.
2. Navigate to the root of the C: drive: cd C:\ [then press Enter]
3. Create a new directory in which to store your virtual environments:
mkdir virtualenvs [then press Enter]
4. Navigate to the newly created virtualenvs directory:
cd virtualenvs [then press Enter]
5. Create a new virtual environment. We’ll use the name foo for the purposes of illustration. Note that if you change this, you’ll have to adjust the rest of the steps in this post accordingly. python -m venv Eric [then press Enter]
Activate the Virtual Environment and Upgrade pip
With the virtual environment created, the next steps are to activate the virtual environment and update pip, Python’s package installer.
First, we’ll activate the virtual environment:
- Open a Command Prompt
- Navigate to the virtual environment’s directory:
cd C:\virtualenvs\Eric [then press Enter] - Activate the virtual environment:
Scripts\activate.bat [then press Enter] - At this point you will see (Eric) at the beginning of the command prompt, indicating the virtual environment is active:
Once a virtual environment is active, all Python-related commands such as python, pip, etc. will be executed against the active virtual environment. For example, with the virtual environment active if a package is installed with pip, that package will only be available in the virtual environment, not in the system’s global Python environment.
Next, to ensure we don’t run into installation or security issues with pip, let’s upgrade it:
- In the same Command Prompt, you opened above, with the virtual environment activated, run the following command:
python -m pip install — upgrade pip [then press Enter] - If an update for pip is available, it will be downloaded and installed.
- Then install all the dependent modules and packages related to the project using command and make sure you are running it in same path where requirement.txt file is present by- pip install -r requirement.txt.
- Install gitbash from https://git-scm.com/download/win for 64-bit
- Open the gitbash and go to the path where you want to copy your project.
- Download the project from git using command in git bash: git clone https://github.com/ayushi21095/User_details.git
- Go to the path where your project file is located in command prompt where virtual environment was activated.
- In the Command Prompt with the virtual environment activated, install Django: pip install wfastcgi [then press Enter]
- Start the Django development server: python manage.py runserver [then press Enter]
- You should see the following output:
- In a browser on the server, go to http://localhost:8000
You should see the following:
- Go back to the Command Prompt and press Ctrl-C to stop the Django development server.
Even if you already have IIS installed, you may need to go through these steps to install the CGI feature if it wasn’t installed as part of the original IIS installation.
- Open the Control Panel
- In the search box in the top right, type “windows features” (without the quotes)
- In the search results under “Programs and Features” click “Turn Windows features on or off.” This launches the Add Roles and Features Wizard.
- Make sure to check CGI box.
Verify the IIS Installation
- Open a web browser on the server.
- Enter http://localhost in the address bar and press Enter. You should see the default IIS page.
- If you don’t see the default IIS page:
1. Open Control Panel
2. Type “services” in the search box
3. Under “Administrative Tools” click “View local services”
- Scroll to the bottom of the list and ensure you see “World Wide Web Publishing Service” listed, and that the status is “Running”.
Thus far we’ve accomplished the following:
- Installed Python
- Installed IIS with CGI
- Created and activated a Python virtual environment
- Installed Django and wfastcgi in a Python virtual environment
- Created a new Django project
- Ran the Django project using the Django development server
With all the underlying infrastructure in place, the last step is to configure IIS to serve Django applications. The Django development server is not designed for production use, so using a production-quality web server such as IIS is mandatory for production applications.
As with previous steps you’ll need to adjust accordingly for the actual application you want to deploy, but for the purposes of this documentation we’ll configure IIS to serve the test Django application we created in the previous steps.
- Open the IIS Manager by clicking the Windows button, clicking on Administrative Tools, and double-clicking Internet Information Services (IIS) Manager
- Click on the name of the server in the list on the left. If you see the following dialog box, check the box “Do not show this message” and click “No.” (You can always get to this later if necessary by clicking “Get New Web Platform Components” under “Actions” on the right-hand side of IIS Manager.)
- Double-click the “FastCGI Settings” icon
- Under “Actions” on the right-hand side click “Add application …”
- In the Add FastCGI Application dialog, in the “Full Path” box, type the path to the Python executable for the application’s virtual environment:
C:\virtualenvs\Eric\Scripts\python.exe - In the Arguments input box, type the path to the wfastcgi.py file in the application’s virtual environment:
C:\virtualenvs\Eric\Lib\site-packages\wfastcgi.py - At this point your settings in the Add FastCGI Application dialog should look like this:
- With the Add FastCGI Application dialog box still open, under the “General” section, click on the “Environment Variables” line, then click the gray “…” button that appears next to (Collection) on the right-hand side of the line. This opens the Environment Variables Collection Editor dialog.
- In the Environment Variables Collection Editor dialog, click “Add”
- In the “Name properties” section on the right, click the input box to the right of “Name,” remove the “Name” text that is already in the input box, and enter DJANGO_SETTINGS_MODULE (note that this MUST be entered in ALL CAPS)
- Click the input box to the right of “Value” and enter Ericsson.settings
- Click “Add” again and enter the following:
Name: PYTHONPATH
Value: C:\User_details\Ericsson - Click “Add” again and enter the following:
Name: WSGI_HANDLER
Value: Ericsson.wsgi.application - At this point you will have three environment variables:
NOTE: All of these settings are CASE-SENSITIVE. They must be entered with exactly the case indicated here in order to work.
- Click “OK” to close the EnviromentVariables Collection Editor.
- Click “OK” to close the Add FastCGI Application dialog.
Next we need to create a new web site in IIS for the Django application, and add a Handler Mapping to the newly created web site so that requests coming to the IIS web site are passed off to the Django application for processing.
- Open IIS Manager
- On the left-hand side under Connections, expand the tree under the server name by clicking on the arrow to the left of the server name
- Right-click on the Sites folder and click “Add Website …”
- For the site name enter Ericsson
- For the physical path, type the following: C:\User_details\Ericsson
- For the purposes of this example configuration, change the Port to 81, since the Default site is running on port 80. For a real-world application you’ll likely want to use name-based virtual hosting by adding bindings and run the site on port 80.
You may leave the “Host name” blank. At this point the Add Website dialog should look like this:
- Click “OK”
Next, we’ll add a FastCGI handler mapping to this site so the requests coming into the site will be processed by the Django application.
- In IIS Manager, expand the Sites folder on the left-hand side and click on the site
- On the right, double-click “Handler Mappings”
- On the right, under “Actions,” click “Add Module Mapping”
- In the “Request path” box enter an asterisk: *
- Click the arrow on the right-hand side of the “Module” box and select “FastCgi Module”
NOTE: Make sure to select FastCgi Module, NOT CgiModule
- In the “Executable” box, enter the following:
C:\virtualenvs\Eric\Scripts\python.exe|C:\virtualenvs\Eric\Lib\site-packages\wfastcgi.py
Note that the character after python.exe is a pipe (|), which is entered by pressing Shift-\ on your keyboard
- In the “Name” box, enter Django Handler (you can call this whatever you want; it’s merely a friendly name for the module mapping)
- The Add Module Mapping dialog should now look like this:
- Click the “Request Restrictions” button and uncheck the “Invoke handler only if request is mapped to:” checkbox
- Click “OK” to close the Request Restrictions dialog
- Click “OK” to close the Add Module Mapping dialog
- When prompted “Do you want to create a FastCGI application for this executable?” click “No” since we created the application earlier.
Note that you CAN have it create the FastCGI application for you from the module mapping, but the settings seem to be different and the end result isn’t fully editable. I also detailed how to create the FastCGI application to be as thorough as possible with all the various pieces involved.
- You will now see the Django Handler listed in the Handler Mappings for the foo website:
At this point everything should be working, so verify by loading the application in a browser.
- Open a browser on the server
- Browse to http://localhost:81
You should see the following:
This is the same result as when we ran the Django test server earlier, but now the requests are being handled by IIS.
Make sure in setting.py file under you project folder you do:
1. DEBUG = False
2. ALLOWED_HOSTS= [‘localhost’,’server ip’]