uWSGI Djnago project deployment of + Nginx

1, set up a virtual server environment

(1) into the Ubuntu system and creates a virtual environment python3

mkvirtualenv -p python3  环境名称

(2) Open a virtual environment

workon 环境名称

(3)
exit the virtual environment

deactivate

(4) delete Xun virtual environment

rmvirtualenv 虚拟环境名

(5) by ftp software to upload your project code and plist.txt files to the server.

(6) In the unit into the virtual environment, execute the command to export all packages that are currently needed.

pip freeze > plist.txt

(7) installation package on the virtual environment.

pip install -r plist.txt

2, WSGI and uWSGI

WSGI: spelling is Python Gateway Interface Web server, the Web server gateway interface Python, Python is an interface between the application and the Web server, or frame, it is widely accepted. No formal implementation since WSGI is more like a protocol, just follow these agreements, WSGI application (Application) can be run on any server (Server).

Standardization project will generate a wsgi.py file to determine the setting module, application objects.

  • application objects: Use an application to interact with the target server application in Python module.
  • settings module: for project configuration.

uWSGI: uWSGI implements all interfaces WSGI, is a fast, self-healing, developers and system administrators friendly server. uWSGI codes written entirely written in C, with high efficiency, and stable performance.

(1) Installation uWSGI.

pip install uwsgi

(2) Configuration uWSGI, create uwsgi.ini file in the project directory, configuration is as follows:

[uwsgi]
#使用nginx连接时使用
socket=192.168.206.128:8000
#直接做web服务器使用
#ttp=192.168.206.128:8000
#项目目录
chdir=/home/python/zhaochaoqun/work01/project
#项目中wsgi.py文件的目录,相对于项目目录
wsgi-file=project/wsgi.py
processes=4
threads=2
master=True
pidfile=uwsgi.pid
daemonize=uwsgi.log
virtualenv=/home/python/.virtualenvs/zhao

(3) start.

uwsgi --ini uwsgi.ini

(4) view.

ps ajx|grep uwsgi

(5) Stop

uwsgi --stop uwsgi.pid

Nginx Installation and Configuration

nginx details, see the official documentation

1) After downloading nginx on the table, unzipped.

tar zxvf nginx-1.6.3.tar.gz

2) enter nginx-1.6.3 directory, in turn execute the following command to install it.

./configure
sudo make
sudo make install

3) installed by default to / usr / local / nginx / directory, enter the directory.

cd /usr/local/nginx/

(Note) For nginx other expansion modules subsequent updates

4) start.

sudo sbin/nginx

5) see progress.

ps ajx|grep nginx

6) stop.

sudo sbin/nginx -s stop

Pointing uwsgi project

1) Open the conf / nginx.conf file.

sudo gedit conf/nginx.conf

2) Add a new entry in the server location routers, point uwsgi the ip and port.

location / {
            #将所有的参数转到uwsgi下
            include uwsgi_params;
            #uwsgi的ip与端口
            uwsgi_pass 127.0.0.1:8080;
        }

Renderings
Here Insert Picture Description

Static files

All static files are from the nginx, and do not request to uwsgi.
1) Open the conf / nginx.conf file.

sudo gedit conf/nginx.conf

2) lower on the server to add a new entry position for static files.

 location /static {
        alias /var/www/test6/static/;
    }

3) Create the following directory on the server.

sudo mkdir -vp /var/www/test6/static/

Modify directory permissions.

sudo chmod 777 /var/www/test6/static/

The final directory structure as shown below:
Here Insert Picture Description
4) to modify test6 / settings.py file.

STATIC_ROOT='/var/www/test6/static/'
STATIC_URL='/static/'

5) to collect all the static files to static_root specified directory.

python manage.py collectstatic

When prompted, enter that collect files.
Here Insert Picture Description
6) stop and start nginx service.

Deployment is complete.

Published 22 original articles · won praise 4 · Views 1944

Guess you like

Origin blog.csdn.net/qq_41337034/article/details/104038776