deploy django on windows

First of all, I am a Tencent cloud server of Windows server2012, and I deploy it through IIS+wfastcgi. The version of python is 3.7.0, and the version of mysql used is 8.0.

Before we start, let me explain the problem of the GDAL package. Originally, I used the GDAL3.4.1 version on the host, but if I also use this version on the server, the error shown in the figure below will appear.

So I downloaded the GDAL3.0.4 version later, and it can be used normally after the installation is complete. By the way, it is really troublesome to install the GDAL library on ubuntu18.04 without using conda. If you must use django and GDAL is indispensable, deploying with Windows is a good choice.

The following officially begins the deployment. First open your server and choose to add services and features.

Then keep going to the next step until this interface appears. I have installed IIS here, and it should be selected when I first open it. After opening, there will be a server role function, select the application on that interface, expand it, select CGI, and click Install. Remember to install the IIS management tools .

Then open the IIS management tool and select Add Website.

At this point, the installation of wfastcgi can start. I did not choose to install it in the virtual environment, but installed it outside.

 After the installation is complete, find the location of your wfastcgi.py file, copy and paste it to your project (your name may be different from mine) directory,

 Once you've completed this step, you can start adding sites. The website name can be chosen by yourself, and the physical path is as follows:

C:\inetpub\wwwroot\cesium_project

That is, the root directory where your project is located (the one with manage.py).

It is best to assign an IP address, and it should not matter if it is not assigned.

 Once this step is done, start the next step, click Handler Mapping

 After opening, fill in the following.

That executable looks like this:

C:\cesium_venv\Scripts\python.exe|C:\inetpub\wwwroot\cesium_project\wfastcgi.py

 |The former is the location of the python.exe file in your virtual environment, and the latter is the location of the wfastcgi.py you just copied and pasted.

Uncheck the request limit.

After completion, create a new web.config document in the root directory of your project, the contents of which are as follows:

<configuration>
  <appSettings>
    <!-- Required settings -->
    <add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()" />
    <add key="PYTHONPATH" value="C:\inetpub\wwwroot\cesium_project" />
    <add key="DJANGO_SETTINGS_MODULE" value="cesium_project.settings" />

    <!-- Optional settings -->
    <add key="WSGI_LOG" value="C:\inetpub\wwwroot\cesium_project\wsgi.log" />
    <add key="WSGI_RESTART_FILE_REGEX" value=".*((\.py)|(\.config))$" />
  </appSettings>
</configuration>

The content in the red box needs to be changed, pay special attention to the wsgi.log, create the file in your folder in advance, and then change its permissions.

 Select the IIS user, check the write permission, and click OK.

Generally installed here, you can already access your django project by opening your URL. (Remember to restart your website in IIS). If 500 appears, you can read the last part of this article.

Next, I will introduce the processing method of static files. There are many methods on the Internet. I tried it and it seems that it doesn’t work. Attaching my own method:

First, create a static_collected folder in your project root directory, then open the settings file in your project, and add the following sentence:

Then open your urls file and add these three sentences:

 Your project root directory should now look like this:

 Then open CMD to enter the virtual environment, cd to the root directory of your project, enter the command python manage.py collectstatic, and start static file collection. After completion, all your static files will be saved in the static_collected folder you just created.

Open this interface and add a virtual directory

The alias is generally static, and the physical path is your static_collected directory. 

 

Now restart the website, and it should be able to open normally when you visit again. Since I don't have the need for the meida directory, I won't introduce it here. Anyone who knows can tell me in the comment section.

Finally, fill in the pits I encountered:

If there is a 500...Fastcgi unexpected termination error when visiting the website, open your IIS application pool and click Advanced Settings

 Make sure your place is LocalSystem

 If not, restart the website after making changes, and you should be able to access normally.

All the steps here (except media) have been completed.

Guess you like

Origin blog.csdn.net/XFIRR/article/details/125093942