Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to host Django applications on Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to host Django applications on Linux". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to host Django applications on Linux.

Introduction

Hosting a Django Web application is fairly simple, although it is a little more complex than a standard PHP application. There are many ways to get a Web server to dock with Django. Gunicorn is one of the simplest.

Gunicorn (short for Green Unicorn) is used as an intermediary server between your Web server Django, in this case, the Web server is Nginx. Gunicorn serves the application, while Nginx handles static content.

Gunicorn

Installation

Installing Gunicorn using Pip is super easy. If you have built your Django project using virtualenv, then you have Pip and should be familiar with how Pip works. So, install Gunicorn in your virtualenv.

$pip install gunicorn

Configuration

One of the most attractive things about Gunicorn is that its configuration is very simple. The way to handle configuration * is to create a folder called Gunicorn in the root directory of the Django project. Then within that folder, create a configuration file.

In this tutorial, the configuration file name is gunicorn-conf.py. In this file, create a configuration similar to the following:

Import multiprocessing bind = 'unix:///tmp/gunicorn1.sock' workers = multiprocessing.cpu_count () * 2 + 1 reload = True daemon = True

In the above configuration, Gunicorn creates a Unix socket called gunicorn1.sock in the / tmp/ directory. Some worker processes are also started, which is equal to twice the number of CPU kernels. It also automatically reloads and runs as a daemon.

Running

The running command for Gunicorn is a bit long, specifying some additional configuration items. The most important part is to point the Gunicorn to the .wsgi file of your project.

Gunicorn-c gunicorn/gunicorn-conf.py-D-error-logfile gunicorn/error.log yourproject.wsgi

The above command should be run from the root directory of the project. The-c option tells Gunicorn to use the configuration file you created. -D specifies gunicorn as the daemon again. The * section specifies the location of the error log file for Gunicorn in the folder you created Gunicorn. The end of the command is to specify the location of the .wsgi file for Gunicorn.

Nginx

Now that Gunicorn is configured and running, you can set up Nginx to connect to it and provide services for your static files. This guide assumes that you have configured Nginx and that the site you host through it uses separate server blocks. It will also include some SSL information.

If you want to know how to get a free SSL certificate for your website, please check out our Let'sEncrypt guide.

# Connect to Gunicorn upstream yourproject-gunicorn {server unix:/tmp/gunicorn1.sock fail_timeout=0;} # redirect unencrypted traffic to the encrypted website server {listen 80; server_name yourwebsite.com; return 301 https://yourwebsite.com$request_uri;} # main service block server {# sets the listening port and specifies the listening domain name listen 443 default ssl; client_max_body_size 4G Server_name yourwebsite.com; # specify log location access_log / var/log/nginx/yourwebsite.access_log main; error_log / var/log/nginx/yourwebsite.error_log info; # tell nginx your ssl certificate ssl on; ssl_certificate / etc/letsencrypt/live/yourwebsite.com/fullchain.pem; ssl_certificate_key / etc/letsencrypt/live/yourwebsite.com/privkey.pem # set the root directory root / var/www/yourvirtualenv/yourproject; # specify the static file path for Nginx location / static/ {# Autoindex the files to make them browsable if you want autoindex on; # The location of your files alias / var/www/yourvirtualenv/yourproject/static/; # Set up caching for your static files expires 1M; access_log off Add_header Cache-Control "public"; proxy_ignore_headers "Set-Cookie";} # specify the path for Nginx to upload your files location / media/ {Autoindex if you want autoindex on; # The location of your uploaded files alias / var/www/yourvirtualenv/yourproject/media/; # Set up aching for your uploaded files expires 1m; access_log off Add_header Cache-Control "public"; proxy_ignore_headers "Set-Cookie";} location / {# Try your static files first, then redirect to Gunicorn try_files $uri @ proxy_to_app;} # pass the request to Gunicorn location @ proxy_to_app {proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for Proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://njc-gunicorn;} # cache HTML, XML and JSON location ~ *\. (html? | xml | json) ${expires 1h;} # cache all other static resources location ~ *\. (jpg | jpeg | png | ico | css | js | ttf | woff2) ${expires 1m; access_log off Add_header Cache-Control "public"; proxy_ignore_headers "Set-Cookie";}}

The configuration file is a bit long, but it can be longer. The focus is on the upstream block pointing to the Gunicorn and the location block that passes traffic to the Gunicorn. Most other configuration items are optional, but you should configure them in a certain form. The comments in the configuration should help you understand the details.

After saving the file, you can restart Nginx to allow the modified configuration to take effect.

# systemctl restart nginx

Once Nginx takes effect online, your site can be accessed through the domain name.

At this point, I believe you have a deeper understanding of "how to host Django applications on Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report