Deploying a Django Project under Linux
Description: The environment used in this article is CentOS 6+python2.7+django1.11
Installing Django, Nginx, and Uwsgi
1. Make sure that the 2.7 version of Python is already installed;
2. Installing Python-devel
Yum Install Python-devel
3. Installing UWSGI
Pip Install Uwsgi
Test whether the UWSGI is working properly
1. Create a new index.py;
# index.pydef application(env, start_response): start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)]) return "Hello World"
2.uwsgi–http:8000–wsgi-file index.py
The browser accesses port 8000 to see if there is a Hello world output
Note: Make sure that port 8000 is accessible by the extranet
Test whether Django works
$ cd /var/www/
$ django-admin startproject mysite
$ cd mysite
$ python manage.py runserver 0.0.0.0:8000
The browser accesses port 8000 to see if there is a Hello world output
Test whether the UWSGI can be integrated with Django
uwsgi --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:9192
Access the 8000 port in your browser to see if you can access the Django website normally.
Parameter description:
# http: protocol type and port number# processes: Number of open processes# Workers: Number of open processes, equivalent to processes (the official website says spawn the specified numbers ofworkers/processes)# chdir: Specify Run directory (chdir to specified directory before apps loading) # wsgi-file: Load wsgi-file (load. wsgi file) # stats: At the address specified, Turn on the status service (enable the stats server on the specified address) # Threads: Running the thread. Because of the Gil's existence, I feel that this sincerity is useless. (Run each worker in prethreaded mode with the specified number of threads) # Master: Allow main process Exists (enable master process) # daemonize: Causes the process to run in the background and logs the log to the specified log file or UDP server (daemonize UWSGI). In fact, the most common thing is to output the running record to a local file. # daemonize: Causes the process to run in the background and hits the log to the specified log file or UDP server (Daemonize uwsgi). In fact, the most common thing is to output the running record to a local file. # Vacuum: Automatically cleans up the environment when the server exits, removes UNIX socket files and PID files (try to remove all of the generated file/ sockets)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
Configure Nginx to provide Django with the service
Create a configuration file for the MySite project under/etc/nginx/conf.d/, as detailed below:
#/etc/nginx/conf.d/mysite_nginx.conf# The upstream component Nginx needs to connect Toupstream Django {server127.0.0.1:8000;# for a Web port socket}# Configuration of the serverserver {# The Port your site is served on listen80; # the domain name it would 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 # Django static and media directory # If there is no static or media directory, you need to first create Location/media {Alias/var/www/mysite/media;} location/static {alias/var/w ww/mysite/static; The # the request for all non-static files to the Django server processing, where Django server uses Uwsgi. Location/{Uwsgi_pass Django; include/var/www/mysite/uwsgi_params;}} #你可以从/etc/nginx/uwsgi_params copy a copy to/var/www/mysite/uwsgi_params. $ cp/etc/nginx/uwsgi_params/var/www/mysite/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
It should be added that in the/etc/nginx/nginx.conf file, the last line is configured, that is, include /etc/nginx/conf.d/*.conf
/etc/nginx/conf.d/mysite_nginx.conf is included in the/etc/nginx/ In the nginx.conf.
Restart the Nginx server to verify the access results
/etc/init.d/nginx restart
What do you find when you access port 80 through a browser? 502 Bad Gateway? Isn't it? Think about it, why is it? The reason is that when you access port 80, the requested resource is not static, nor media, this time Nginx will transfer the request to upstream Django,upstream gateway configuration 127.0.0.1:8000, and 127.0.0.1 : 8000 is to rely on Uwsgi start, so reported a 502 bad Gateway. You, get it?
Note: The CentOS 7 command to start the service issystemctl restart nginx.service
Start Uwsgi and verify the results again
Execute the following command to start Uwsgi.
uwsgi --socket :8000 --chdir=/var/www/mysite --module mysite.wsgi
Restart the Nginx service /etc/init.d/nginx restart
and try again via the browser to access port 80. Did it succeed?
Note: The CentOS 7 command to start the service issystemctl restart nginx.service
How do I make uwsgi run as a configuration file? Configuring Uwsgi to run with a. ini file
Create a Mysite_uwsgi.ini file that reads as follows:
[Uwsgi]socket=:8000ChDir =/var/www/mysite #wsgi-file = Mysite/wsgi.pymodule=mysite.wsgi:application processes = 10threads = 2# django<1.4, you must specify env and Moduleenv = Django_settings_module=mysite.settings# clear environment on Exitvacuum = Truesafe-pidfile =/tmp/ Project-master.pid # Create a Pidfileharakiri = # respawn Processes taking more than Secondslimit-as = Max # limit the project to + mbmax-requests = class= "Hljs-comment" ># respawn processes after serving daemonize = /var/log/uwsgi/mysite.log # Background The process & log
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
Execute commands to uwsgi --ini mysite_uwsgi.ini
run
How do I run in Emperor mode?
What is Emperor mode? , the official website said very clearly, as follows:
Uwsgi can run in ' Emperor ' mode. In this mode it keeps the eye on a directory of Uwsgi config files, and would spawn instances (' vassals ') for each one it fi Nds.
Whenever a config file is amended, the emperor would automatically restart the vassal.
Follow the steps below, which means you can 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 --gid nginx
How do I create a UWSGI service?
In Linux, a service is actually a shell script. In CenOS6, service scripts are typically in the/etc/init.d/directory.
First we create a Uwsgi file in the/etc/initd/directory with the following file contents:
#!/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 1 2 6# # # 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=trueuwsgi_vassals="/etc/uwsgi/vassals/" uwsgi_options="--uid nginx--gid nginx--logto/var/log/uwsgi/uwsgi.log" lockfile=/var/lock/subsys/uwsgiuwsgi_options="$UWSGI _options--autoload "if ["$UWSGI _emperor_mode "="true"];Then uwsgi_options="$UWSGI _options--emperor$UWSGI _vassals "FiCase"$ "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;; esacexit 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
We can then 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 should be noted that the ownership of the log folder should be attributed to the user Nginx specified in the configuration file
$ chown nginx.nginx /var/log/uwsgi -R
How to set up boot Uwsgi?
Add the command that launches Uwsgi to the "/etc/rc.local" file.
Multi-Site Deployment issues
#Simple HTTP serverserver { listen 80; root /usr/share/nginx/www; server_name host1.example.com;}#Django serverserver { listen 80; server_name host2.example.com; #...upstream config...}
Deploying a Django Project under Linux