# Windows Server

{% hint style="info" %}
**Good to know:** on Windows, the Automail web application runs on IIS and the workflows as scheduled tasks in the Task Scheduler.
{% endhint %}

## Prerequisites

Install the following software on the server:

* Python 3.9 or later (official 64-bit version installed in C:\Program Files\Python).
* Notepad++ for editing settings.py.
* Git and GitHub Desktop.
* MySQL Community Server (version 8.0) and MySQL Workbench.
* [Internet Information Services (IIS)](#user-content-fn-1)[^1] on the server with the CGI feature activated. The IIS installation is done via the Server Manager.

<figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FaGZdFJH1NYb4fxRUiAPQ%2Fimage.png?alt=media&#x26;token=55602e16-1df8-480d-8a0c-8cf8c1e1e3e6" alt=""><figcaption><p>Server Manager</p></figcaption></figure>

## Installation

{% hint style="info" %}
The below installation is done in the C drive. If the server has a separate drive for storing the data use that drive instead.
{% endhint %}

### Automail Web App

#### Base

The following steps are the same as for the [local DEV](https://docs.lineverge.com/automail/installation/dev) installation:

1. Create a Lineverge folder in C:\Lineverge\Automail Forks\\\[forked repo name], with the sub-folders **ve**, **repo**, **backup,** and **staging**
2. Navigate to **ve** and create a virtual environment with `virtualenv automail`
3. Activate the ve with `automail\Scripts\activate.bat`
4. Navigate to **repo** and clone the forked repo with `git clone https://github.com/Lineverge/[forked repo name].git .` (don't forget the dot!)
5. Navigate to C:\Lineverge\Automail Forks\\\[forked repo name]\software and run `pip install -r requirements.txt`
6. Create a backup of your local database and restore it in MySQL Workbench
7. Navigate to C:\Lineverge\Automail Forks\\\[forked repo name]\software\project
8. Create [settings.py](https://docs.lineverge.com/automail/configurations/settings) in the project folder and adjust the paths and database constants to match the server.
9. Run `python automail.py runserver` to test that the installation was successful.

#### IIS

The following steps are IIS-specific:

1. Run `python automail.py collectstatic --noinput`to move all static files in a folder that will be used by IIS to server static content to the users (HTML, CSS, JS, images, etc.).&#x20;
2. Run `pip install wfastcgi` and move the wfastcgi.py file from site-packages in the ve to C:\Lineverge\Automail Forks\\\[forked repo name]\staging\server.
3. Open the **IIS Manager**.
4. Access the FastCGI Settings.&#x20;

   <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FfvaxBMw6FXgdxpCK4d1M%2Fimage.png?alt=media&#x26;token=ab6d8c12-9c94-4590-8670-231feb25f298" alt=""><figcaption><p>FastCGI Settings</p></figcaption></figure>
5. Click **Add Application** and set the Full Path, Arguments, and Environment Variables.

   <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FMRc8hXDkYSAsCJ7Mv90S%2Fimage.png?alt=media&#x26;token=e145a80e-900e-4963-a485-9ff8d4d3ffc6" alt=""><figcaption><p>Add FastCGI Application</p></figcaption></figure>

   \
   **Full Path:** C:\Lineverge\Automail Forks\\\[forked repo name]\ve\Scripts\python.exe\
   **Arguments:** C:\Lineverge\Automail Forks\\\[forked repo name]\staging\server\wfastcgi.py<br>

   <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FobdzQyRAp9uhzNWNQfO9%2Fimage.png?alt=media&#x26;token=c8eab8c8-7077-4756-80a3-0618a493ea05" alt="" width="563"><figcaption><p>Environment Variables Collection</p></figcaption></figure>

   **Collection:**\
   DJANGO\_SETTINGS\_MODULE: project.settings \
   PYTHONPATH: C:\Lineverge\Automail Forks\\\[forked repo name]\repo\software \
   WSGI\_HANDLER: django.core.wsgi.get\_wsgi\_application()
6. Go to **Sites** and **Add Website**.

   <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FD3xPwAZmL3tNwXOd5fA1%2Fimage.png?alt=media&#x26;token=6e445683-c5ec-445a-8d23-a143ef2740b0" alt=""><figcaption><p>Add Website</p></figcaption></figure>

   **Site name:** \[forked repo name]\
   **Physical path:** C:\Lineverge\Automail Forks\[forked repo name]\repo\software\
   **Host name:** \[Automail instance domain] (e.g. test.lineverge.com)
7. Click on the newly created site, then double-click on **Handler Mappings**, then **Add Module Mapping...** under **Actions**.&#x20;

   <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FPasHG2JO4KIQlDce4icS%2Fimage.png?alt=media&#x26;token=adc11d7b-c014-4188-8344-86e12ff01934" alt=""><figcaption><p>Handler Mapping</p></figcaption></figure>
8. Input all parameters.

   <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FsaEISOrwjswju317joQN%2Fimage.png?alt=media&#x26;token=3bb17471-276e-4903-b17c-d86d8a896dc7" alt=""><figcaption><p>Add Module Mapping</p></figcaption></figure>

   **Request path:** \*\
   **Module:** FastCgiModule\
   **Executable:** "C:\Lineverge\Automail Forks\[forked repo name]\ve\Scripts\python.exe"|"C:\Lineverge\Automail Forks\[forked repo name]\staging\server\wfastcgi.py"\
   **Name:** \[forked repo name]\
   **Requests Restrictions:** Uncheck *Invoke handler only if request is mapped to:* checkbox and click **OK**

   <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FXfr09iDV1Pv7I82gtoDE%2Fimage.png?alt=media&#x26;token=6bcf0659-bac6-4d33-b111-c34c57af4a79" alt="" width="563"><figcaption><p>Request Restrictions</p></figcaption></figure>
9. Click **OK**, then **No** in the popup.

   <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FuDGJuuOY762Ct9QYy1lS%2Fimage.png?alt=media&#x26;token=9188541b-9440-4546-83b3-bc0db9cd54e2" alt="" width="375"><figcaption></figcaption></figure>
10. Right-click on the site, then **Add Virtual Directory...**<br>

    <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FlIPgY2NxXu8AHAt4iRLw%2Fimage.png?alt=media&#x26;token=d6f5ea07-1386-4740-a3f9-173501beeb3c" alt="" width="257"><figcaption></figcaption></figure>
11. Set the parameters for a virtual directory and click **OK**.<br>

    <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FeEZjCZyavHwXmwrmcE4T%2Fimage.png?alt=media&#x26;token=3f465e34-3e11-42f2-9dcf-99fb8ecdc1cd" alt="" width="334"><figcaption></figcaption></figure>

    **Alias:** static\
    **Physical path:** C:\Lineverge\Automail Forks\[forked repo name]\repo\software\static
12. Unfold the site, click on the static folder, then double-click on **Handler Mapping**.

    <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FQCC7GESFcU1R5H6qGhDn%2Fimage.png?alt=media&#x26;token=7e414289-1d32-4b4a-bb7e-b8efc5c237c7" alt=""><figcaption></figcaption></figure>
13. Select **View Ordered List...**

    <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2Fg2LXXawYzQ9eAPq9QdCz%2Fimage.png?alt=media&#x26;token=556c07ca-57fe-404a-8c1b-9b3a11c69b4a" alt=""><figcaption></figcaption></figure>
14. Then move StaticFile to the top using the **Move Up** button under **Actions**.

    <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2Fzqlpanb6qdUBJAjWXGeM%2Fimage.png?alt=media&#x26;token=04234355-8010-4c55-9fe9-4e1bc644e483" alt=""><figcaption></figcaption></figure>
15. Go to C:\Lineverge\Automail Forks\\\[forked repo name]\repo\software and make a copy of the web.config file.
16. Replace the current web.config file with the web.config in C:\Lineverge\Automail Forks\\\[forked repo name]\repo\deployment\iis\web.config.
17. Click on the site. Then **Start** under **Manage Website**.<br>

    <figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2F8Xwd67WT7otdLjNWPB2p%2Fimage.png?alt=media&#x26;token=29b458e3-db80-4fb5-8484-229062aa3589" alt=""><figcaption></figcaption></figure>

Automail will now be accessible at online under hostname defined above \[Automail instance domain] (e.g. test.lineverge.com).

{% hint style="info" %}
Any change in the source code of the fork needs to be pulled on the server using `git pull`.\
If the change involves new migration files, then these need to be migrated to the production database by running `python automail.py migrate` from the project folder.

If the change involves new or changed static files, then these need to be moved to the production static folder by running `python automail.py collectstatic --noinput` from the project folder.
{% endhint %}

### Scheduled Tasks

Background jobs are scheduled through the **Task Scheduler** in Windows.\
Scheduled tasks can be created programmatically from the **CMD** using the `schtasks` command.

```batch
schtasks /create /tn "Automail-LV-Test\automail_test" /sc WEEKLY /d MON /st 09:00 /tr "C:\Lineverge\Automail Forks\[forked repo name]\ve\Scripts\python.exe C:\Lineverge\Automail Forks[forked repo name]\repo\software\project\automail_test.py" /RU automail /RP password
```

<figure><img src="https://3317070279-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F21lWxRGfp98mDu2HVXvf%2Fuploads%2FHcdn4tGjLeJ8eXJ86A36%2Fimage.png?alt=media&#x26;token=c9276adb-3454-4144-8c4d-9c061a28cdbc" alt=""><figcaption><p>Windows Task Scheduler</p></figcaption></figure>

In this example `schtasks` command, the parameters are as follows:

* `/create`: Creates a new scheduled task.
* `/tn "Automail-LV-Test\automail_test"`: Sets the task name to "Automail-LV-Test\automail\_test".
* `/sc WEEKLY`: Sets the schedule type to WEEKLY.
* `/d MON`: Sets the day of the week when the task runs to Monday.
* `/st 09:00`: Sets the start time of the task to 9:00 AM.
* `/tr "C:\Lineverge\Automail Forks\[forked repo name]\ve\Scripts\python.exe C:\Lineverge\Automail Forks[forked repo name]\repo\software\project\automail_test.py"`: Sets the command to execute for the task, which runs a Python script using a specified Python interpreter.
* `/RU automail`: Specifies the user account under which the task runs as "automail".
* `/RP password`: Specifies the password for the "automail" user account.

Other available parameters to customize a scheduled task programmatically include:

* `/sd`: Specifies the start date for the task.
* `/ed`: Specifies the end date for the task.
* `/ri`: Specifies the repetition interval in minutes for the task.
* `/du`: Specifies the duration for which the task should repeat, in the format HH:mm.
* `/k`: Indicates that the task should stop when the repetition duration is reached.
* `/it`: Allows the task to run interactively only if the user is logged on.
* `/np`: Disables printing of the scheduler status messages.
* `/z`: Marks the task for deletion after it runs.
* `/f`: Forces the task to be created, even if the specified task name already exists.
* `/xml`: Specifies the XML file containing the task definition.

For a complete list of parameters, refer to the [official Microsoft documentation](https://docs.microsoft.com/en-us/windows/win32/taskschd/schtasks).

[^1]: Internet Information Services (IIS) is a flexible, general-purpose web server from Microsoft that runs on Windows systems to serve requested HTML pages or files. An IIS web server accepts requests from remote client computers and returns the appropriate response.
