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 deploy Django Project in Linux system

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about how to deploy the Django project in the Linux system. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Deploy Django project in Linux system

Install Django, Nginx and uWSGI1. Confirm that version 2. 7 of Python; 2. 7 has been installed. Install python-devel yum install python-devel 3. Install uwsgi pip install uwsgi

Test whether uwsgi is working properly. Create a new index.py

# index.py def application (env, start_response): start_response ('200OK', [(' Content-Type','text/html')]) return "Hello World" 12342.uwsgi-http: 8000-wsgi-file index.py browser access port 8000 to see if there is hello world output Note: ensure that port 8000 can be accessed by the external network

Test whether Django is working properly $cd / var/www/ $django-admin startproject mysite $cd mysite $python manage.py runserver 0.0.0.0 var/www/ 8000 browser access port 8000 to see if there is any hello world output

Test whether uwsgi can integrate uwsgi with django-- http: 8000-- chdir=/var/www/mysite-- module mysite.wsgi` or `uwsgi-- http: 8008-- chdir / var/www/mysite-- wsgi-file weixin/wsgi.py-- master-- processes 4-- threads 2-- stats 127.0.0.1 http 9192 access port 8000 in the browser to see if django website can be accessed normally.

Parameter description:

# http: protocol type and port number # processes: number of processes opened # workers: number of processes opened, equivalent to processes (spawn the specified number ofworkers / processes in the official website) # chdir: specify the running directory (chdir to specified directory before apps loading) # wsgi-file: load wsgi-file (load .wsgi file) # stats: on the specified address Turn on the status service (enable the stats server on the specified address) # threads: run the thread. Because of the existence of GIL, I think this is really useless. (run each worker in prethreaded mode with the specified number of threads) # master: allow the main process to exist (enable master process) # daemonize: make the process run in the background and log to the specified log file or udp server (daemonize uWSGI). In fact, the most commonly used is to output the run record to a local file. # daemonize: make the process run in the background and log to the specified log file or udp server (daemonize uWSGI). In fact, the most commonly used is to output the run record to a local file. # vacuum: automatically clean up the environment when the server exits, delete the unix socket file and pid file (try to remove all of the generated file/sockets) 12345678910111213 to configure Nginx, so that Nginx can provide services for Django to create a configuration file for the mysite project under / etc/nginx/conf.d/, as shown below:

# / etc/nginx/conf.d/mysite_nginx.conf # the upstream component nginx needs to connect to upstream django {server 127.0.0.1 server 8000; # for a web port socket} # configuration of the server server {# the port your site will be served on listen 80; # the domain name it will serve for server_name .example.com; # substitute your machine's IP address or FQDN charset utf-8 # max upload size client_max_body_size 75M; # adjust to taste # static and media directories of Django # if you don't have a static or media directory, you need to create location / media {alias / var/www/mysite/media;} location / static {alias / var/www/mysite/static first } # transfer all requests for non-static files to django server for processing, where django server uses uwsgi. Location / {uwsgi_pass django; include / var/www/mysite/uwsgi_params;}} # you can copy a copy from / etc/nginx/uwsgi_params to / var/www/mysite/uwsgi_params. $cp / etc/nginx/uwsgi_params / var/www/mysite/1234567891011121314151617181920212223242526272829303132333435 needs to be added that in the / etc/nginx/nginx.conf file, the configuration on the last line is include / etc/nginx/conf.d/*.conf, that is, / etc/nginx/conf.d/mysite_nginx.conf is included in / etc/nginx/nginx.conf.

Restart the nginx server and verify the access results / etc/init.d/nginx restart access port 80 through the browser, what did you find? 502 Bad Gateway? Isn't that right? Think about it. Why? The reason is that when you access port 80, the requested resource is neither static nor media. At this time, Nginx forwards the request to 127.0.0.1 uwsgi 8000 configured by upstream django,upstream 's gateway, and 127.0.0.1 Nginx 8000 is started by uwsgi, so a 502 Bad Gateway is reported. Do you understand?

Note: the command for CentOS 7 to start the service is systemctl restart nginx.service

Start uwsgi, verify the results again, execute the following command, and start uwsgi. Uwsgi-- socket: 8000-- chdir=/var/www/mysite-- module mysite.wsgi restart the Nginx service / etc/init.d/nginx restart, and try accessing port 80 through the browser again. Did you succeed?

Note: the command for CentOS 7 to start the service is systemctl restart nginx.service

How do I make uwsgi run as a configuration file? Configuring uWSGI to run with a .ini file creates a mysite_uwsgi.ini file with the following contents:

[uwsgi] socket=:8000 chdir = / var/www/mysite # wsgi-file = mysite/wsgi.py module=mysite.wsgi:application processes = 10 threads = 2 # django execute the command uwsgi-- ini mysite_uwsgi.ini to run how to run in Emperor mode? What is Emperor mode? The official website makes it very clear, as follows: uWSGI can run in 'emperor' mode. In this mode it keeps an eye on a directory of uWSGI config files, and will spawn instances ('vassals') for each one it finds.Whenever a config file is amended, the emperor will automatically restart the vassal. Follow these steps to run uwsgi in Emperor mode:\ 1. Create a directory for the vassals sudo mkdir / etc/uwsgi sudo mkdir / etc/uwsgi/vassals\ 2. Symlink from the default config directory to your config file sudo ln-s / path/to/your/mysite/mysite_uwsgi.ini / etc/uwsgi/vassals/\ 3. Run the emperor uwsgi-- emperor / etc/uwsgi/vassals-- uid nginx-- how does gid nginx create a uwsgi service? In Linux, a service is actually a shell script. In CenOS6, service scripts are typically located in the / etc/init.d/ directory. First, we create a uwsgi file under the / etc/initd/ directory, which reads as follows: #! / bin/sh # BEGIN INIT INFO # Provides: uwsgi # Required-Start: $syslog $remote_fs # Should-Start: $time ypbind smtp # Required-Stop: $syslog $remote_fs # Should-Stop: ypbind smtp # Default-Start: 3 5 # Default-Stop: 0 126 # END INIT INFO # Source function library. . / etc/rc.d/init.d/functions # Check for missing binaries (stale symlinks should not happen) UWSGI_BIN= "/ usr/local/bin/uwsgi" UWSGI_EMPEROR_MODE=true UWSGI_VASSALS= "/ etc/uwsgi/vassals/" UWSGI_OPTIONS= "- uid nginx-- gid nginx-- logto / var/log/uwsgi/uwsgi.log" lockfile=/var/lock/subsys/uwsgi UWSGI_OPTIONS= "$UWSGI_OPTIONS-- autoload" if ["$UWSGI_EMPEROR_MODE" = "true"] Then UWSGI_OPTIONS= "$UWSGI_OPTIONS-- emperor $UWSGI_VASSALS" fi case "$1" in start) echo "Starting uWSGI..." Daemon $UWSGI_BIN $UWSGI_OPTIONS &;; stop) echo "Shutting down uWSGI..." Killproc $UWSGI_BIN;; restart) $0 stop $0 start;; status) echo-n "Checking for service uWSGI" status $UWSGI_BIN;; *) echo "Usage: $0 {start | stop | status | restart}" exit 1 Esac exit 01234567891011121314151617181920212223242627282930313334353637383940414243444546 then we can use this script to manage uwsgi as follows: / etc/init.d/uwsgi start / etc/init.d/uwsgi stop / etc/init.d/uwsgi restart / etc/init.d/uwsgi status it is important to note that the ownership of the log folder should belong to the user nginx $chown nginx.nginx / var/log/uwsgi-R specified in the configuration file. Just add the command to start uwsgi to the "/ etc/rc.local" file. Multisite deployment problem # Simple HTTP server server {listen 80; root / usr/share/nginx/www; server_name host1.example.com;} # Django server server {listen 80; server_name host2.example.com; #... upstream config... } above is how to deploy the Django project in the Linux system shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report