nginx + uwsgi department Django

Constant dropping wears the stone
Drip through stone

This article is used to record some notes on Django deployment. The system environment described in this article is Ubuntu, the server used is nginx and uwsgi is used to connect to Django. This is also the mainstream deployment package of Django.

Deploying the connection principle

Environment Installation

nginx

Install nginx

1
sudo apt-get install nginx

Run and view status

1
2
/etc/init.d/nginx start
/etc/init.d/nginx status

Uwsgi

Install python-dev first, otherwise uwsgi installation may report an error

1
apt-get install python-dev

Install uwsgi

1
pip install uwsgi

Add environment variables after installation:
Open the file: sudo vim .bashrc and add the following:

1
export PATH=/home/nmask/.local/bin/:$PATH

Then run source .bashrc to make it work, you can run uwsgi directly from the command line.

Environmental Testing

Testing nginx

1
/etc/init.d/nginx start

Open http://localhost:80, you can see that nginx indicates that nginx is successfully installed.

Testing uwsgi

Create a test.py file in the root directory of the project and write:

1
2
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])

Run under the project root directory:

1
uwsgi --http :8001 --wsgi-file test.py

Visit http://localhost:8001, if you can see hello world, the uwsgi installation is successful.

Running django project with uwsgi
1
uwsgi --http :8001 --chdir /home/nmask/mydjango --wsgi-file mydjango/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:8080

Common options:

  • http : protocol type and port number
  • processes : number of processes opened
  • workers : The number of processes opened, which is equivalent to processes (the official website is spawn the specified number of workers / processes)
  • chdir : specify the run directory (chdir to specified directory before apps loading)
  • stats : enable the stats server on the specified address
  • threads : Run the thread. Because of the existence of GIL, I feel that this is really useless. (run each worker in prethreaded mode with the specified number of threads)
  • master : allows the main process to exist (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 one is to output the log to a local file.
  • pidfile : Specify the location of the pid file and record the pid number of the main process.
  • vacuum : Automatically clean up the environment when the server exits, delete the unix socket file and the pid file (try to remove all of the generated file/sockets)

File Configuration

myweb_uwsgi.ini

Create the project root directory: myweb_uwsgi.ini file, write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# myweb_uwsgi.ini file
[uwsgi]
# Django-related settings
socket = :8000
# the base directory (full path)
chdir = /home/nmask/mydjango
# Django s wsgi file
module = mydjango.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true

Run django with uwsgi: (same as the previous command line, so that it is easy to write a file)

1
uwsgi - here myweb_uwsgi.ini

Configuration file parameters:

  • socket: refers to the port that uwsgi runs.
  • Chdir: the directory to run
  • Module: running file

Configuring nginx

Open /etc/nginx/nginx.conf and add the following to http:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 8890;
server_name 127.0.0.1
charset UTF-8;
access_log /var/log/nginx/myweb_access.log;
error_log /var/log/nginx/myweb_error.log;
client_max_body_size 75M;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_read_timeout 2;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /home/fnngj/pydj/myweb/static/;
}
}

Description: The 8000 port here is the uwsgi port, and the nginx operation will open the 8890 port, that is, the 8890 port of nginx and the 8000 port of uwsgi communicate with each other.

Deployment Run

Run uwsgi:

1
nohup uwsgi - here myweb_uwsgi.ini &

Run nginx:

1
/etc/init.d/nginx start

Finally, visit http://localhost:8890 and you can see that the django project has been run on nginx.

Note: After updating the Django code, it is best to restart the uwsgi process to avoid unpredictable bugs!

  • If the server is mapped, the server_name in the nginx configuration file should write the IP or domain name of the external network.
  • Do not use uwsgi in the python virtual environment, there will be some problems, of course, can also be resolved, reference: https://stackoverflow.com/questions/14194859/importerror-no-module-named-django-core-wsgi-for- Uwsgi
  • If uwsgi is running normally, nginx is also running normally, but it is not connected. You can check if selinux is enabled in the system and you need to close it.

Reporting error

No module named django.core.wsgi

The following error was reported when starting uwsgi:

1
ImportError: No module named django.core.wsgi for uwsgi

When uwsgi is started in the python virtual environment, a virtual path is added to the configuration file.
Open django.init (create it yourself) write:

1
home=/path/to/venv/

run:

1
uwsgi - this is django.ini --protocol = http

uwsgi http is ambiguous

This is also because of the virtual environment, it is recommended to exit the python virtual environment, and then pip install uwsgi.

Reference article

http://www.cnblogs.com/fnng/p/5268633.html

本文标题:nginx + uwsgi department Django

文章作者:nmask

发布时间:2017年08月21日 - 20:08

最后更新:2019年07月11日 - 15:07

原始链接:https://thief.one/2017/08/21/1/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: