New version of Django Deployment under Windows and Linux __linux

Source: Internet
Author: User

Original post address: https://zhuanlan.zhihu.com/p/27690113

With the fiery development of AI and deep learning, Python has become a very popular language. We can see that the popular depth learning framework provides Python interfaces, some of which are even Python interfaces, which in some ways drives the popularity of Python. Of course, after we've completed our model training, we always have to build a demo platform that is either open to the user or tested in-house.
Although Python is a well-known glue language that can interoperate with many languages and frameworks, it simplifies the process of writing a server if you can deploy it directly using a framework written by Python. The most famous web site framework under Python is Django, so today's new version of Django's deployment method is introduced, mainly with Windows and Linux as an example. Then why do you write this article? Because Django has changed a lot over the years, but there are a lot of old deployment tutorials on the web that will disrupt our deployment process, so I think it's necessary to teach you some of the deployment experiences. Of course, before deploying, use the Python manage.py runserver to check to see if the server is working properly.

Let's take a look at how the Django deployment is done under Windows, using the WFASTCGI library in the Django template in Azure and Microsoft's own server platform IIS. The process is as follows:

If you are installing IIS, be sure to check the CGI in the server technology.

Install wfastcgi

Pip Install wfastcgi

Executes the instruction to obtain the fastcgi handler parameters.

Wfastcgi-enable

Will get a string like C:\ANACONDA2\PYTHON.EXE|C:\ANACONDA2\LIB\SITE-PACKAGES\WFASTCGI.PYC, which is actually the path of Python and the path of handler | A string that is connected, recorded, and used later.

In the site root directory, create a new web.config and fill in the following

<configuration> <system.webServer>  

The above section is the sample Web.config file, excerpted from the official introduction page of wfastcgi, we need to make some changes, first scriptprocessor to the string we recorded above, then some necessary parameters, Like Pythonpath to the site's root directory path, the last is some optional parameters, such as Wsgi_log is the path of log files, as well as django_settings_module need to change the name of their project, like Appinsights_ Instrumentationkey,wsgi_ptvsd_secret,wsgi_ptvsd_address these are the parameters on Azure and we can delete them.

In IIS, create a new site, point to the root of the Web site, and note that the ports do not repeat.

Open the browser to access, no accident has been OK. If there is still a problem, is generally a permission issue, check the Anaconda directory for Iis_iusrs whether there is readable, executable, list directory permissions.

The above deployment process has been tested in IIS 6 and above, with no major problems. And through the above set of Wsgi_restart_file_regex parameters, you can achieve automatic restart, it is not too cool.

After the deployment under Linux, I thought it would be much simpler than windows, but since I experienced the Digitalocean one-click Deployment Mirror, I feel like I'm too simple. Remind you not to use do in the Ubuntu16.04 of the Django one-click Mirror, do not know why suddenly hung up, and one can not use domain name access. Ubuntu14.04 's Django One-click mirror is fine, and the Django version is too old to be upgraded and hung up. So I start from scratch, this time mainly using Nginx and Uwsgi to deploy, the process is as follows:

Follow the Nginx official tutorial for nginx installation, generally download the certificate and add it to this machine, refresh the cache of the library installation software, and then install such a process. Here, for example, Ubuntu/debian in bash:

wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add Nginx_signing.key

Then modify/etc/apt/sources.list and add the following two lines

Deb Http://nginx.org/packages/system/codename nginx
deb-src http://nginx.org/packages/system/codename nginx

System is replaced with Debian or ubuntu,codename, which can be obtained by lsb_release-a, and then continue execution

Apt-get Update
apt-get install Nginx

Make sure that Python and Pip are installed and can be installed with the following instructions.

Apt-get Install Python-dev Python-pip

Install Uwsgi

Pip Install Uwsgi

As above, create a new profile Uwsgi.ini under the root directory, and fill in the following.

[Uwsgi]
Socket = 127.0.0.1:8001
chdir =/path/to/your/project/
wsgi-file = project_name/wsgi.py
master = True
processes = 1
vacuum = true
optimize = 2
stats = 127.0.0.1:9191 daemonize
=/var/log/uwsgi/myapp.log

ChDir modified to the root of the project, Project_Name modified to its own project name, Daemonize is the path of the log file, the port of the socket can be flexibly adjusted.

Execute command to run UWSGI server

Uwsgi Uwsgi.ini

According to Nginx's tutorial, we modify the Nginx configuration file, the new version of the path is generally in/etc/nginx/conf.d/default.conf.

Upstream Django {
    server 127.0.0.1:8001;
}

server {
    listen;
    server_name myapp.example.com;

    root/var/www/myapp/html;

    Location/{
        index index.html;
    }

    location/static/  {
        alias/var/django/projects/myapp/static/;
    }

    Location/main {
        include uwsgi_params;
        Uwsgi_pass Django;

        Uwsgi_param Host $host;
        Uwsgi_param x-real-ip $remote _addr;
        Uwsgi_param x-forwarded-for $proxy _add_x_forwarded_for;
        Uwsgi_param x-forwarded-proto $http _x_forwarded_proto
    }
}

Ensure that the ports on the upstream Django route are compatible with the previous socket settings, and that the static directory and other configurations can be flexibly adjusted. If you run directly now, you can find that the site can run normally, but the static file is problematic. This is also a permission issue because Nginx does not run with root, and he does not have access to the static directory. There are two solutions, one is to give Nginx user access to the static directory, and one is to let Nginx run as root user. Take the second example, which is to modify the user Nginx in/etc/nginx/nginx.conf to user root, and then use service Nginx restart Restart services. If you make a change to the project, he will not automatically take effect, you need to first kill the previous UWSGI process, run step 5 again.

Above, is the entire content of the article, if the feeling is still not enough, you can give my GitHub home page add a follow or something (funny), later may also share some relevant experience.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.