ubuntu + nginx + uwsgi deploy django project summary of ubuntu + nginx + uwsgi deploy django project

ubuntu + nginx + uwsgi deploy django project

Foreword

Django's deployment there can be many ways to use IIS or Apache under Windows, Nginx can be.

Using nginx + uwsgi manner is one of the more common way, all requests is nginx as the most front-end server, it will receive WEB unified management request. nginx all the static requests into their own hands. Then, nginx all requests pass through the non-static uwsgi to Django, it is handled by Django, WEB to complete a request. Uwsgi a similar effect on the bridge, play the role of a bridge.

The local development django project to deploy uwsgi server on Ubuntu, and configure nginx, finished running on nginx + uwsgi + django on the line. The following compilation of relevant procedures.

1, server configuration virtualenv

If you do not want to directly use python runtime environment, you need to install and configure virtual env on the server. If the direct use of python environment, you can skip this step.

  pip install virtualenv

  pip install virtualenvwrapper

We need to configure the environment variables to be able to use the commands:

Create a .bash_profile at ~, add the following:

  export WORKON_HOME=$HOME/.virtualenvs
  export PROJECT_HOME=$HOME/workspace
  source /usr/local/bin/virtualenvwrapper.sh

Then run the command: source ~ / .bash_profile

Configuration is complete. Use the following command:

  mkvirtualenv its_name # Create a virtual env
  workon its_name # to switch to a lower env (if you do not specify its_name) it is to list all the existing env
  deactive # exit the current env
  rmvirtualenv its_name # delete a virtual env

2, the installation dependencies

First, the development of local, need to export pip dependencies list.

pip freeze > plist.txt

Put this file to the server environment and batch installation dependent.

workon [虚拟环境名称] pip install -r plist.txt -i https://mirrors.tuna.tsinghua.edu.cn # 这里使用清华大学镜像源安装

Modify 3, django project

You need to modify settings.py file, close the debug mode.

DEBUG = False
ALLOW_HOSTS=['*','ipdress'] #加入访问服务器的ip

4, configuration uwsgi

Installation: pip install uwsgi

Django project required under the root directory (the same directory with manage.py), a new uwsgi.ini file, write configuration:  

Copy the code
Myweb_uwsgi.ini File # 
[uwsgi] 
socket =: 8080 # real service port, 
# Django project root directory (absolute path) 
chdir = / Home / username / myweb  # wsgi.py file location in the project  Module = myweb.wsgi  = Master to true running processes # 4 = number of processes Vacuum to true =使用nginx连接时,使用socket
Copy the code

Switch to the project under myweb directory, read myweb_uwsgi.ini file to start the project by uwsgi command.

启动uwsgi: uwsgi --ini xxx.ini
关闭uwsgi: uwsgi --stop xxx.pid
heavy Sirs uwsgi: uwsgi --reload xxx.pid

 Start state:

Copy the code
[uWSGI] getting INI configuration from myweb_uwsgi.ini
*** Starting uWSGI 2.0.12 (32bit) on [Sat Mar 12 13:05:06 2019] ***
compiled with version: 4.8.4 on 26 January 2016 06:14:41
os: Linux-3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:18:00 UTC 2015
nodename: ubuntu
machine: i686
clock source: unix
detected number of CPU cores: 2
current working directory: /home/fnngj/pydj/myweb
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/fnngj/pydj/myweb
your processes number limit is 15962
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8000 fd 3
Python version: 3.4.3 (default, Oct 14 2015, 20:37:06)  [GCC 4.8.4]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x8b52dc0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 319920 bytes (312 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x8b52dc0 pid: 7158 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 7158)
spawned uWSGI worker 1 (pid: 7160, cores: 1)
spawned uWSGI worker 2 (pid: 7161, cores: 1)
spawned uWSGI worker 3 (pid: 7162, cores: 1)
spawned uWSGI worker 4 (pid: 7163, cores: 1)
Copy the code

Note View startup information uwsgi, if there is wrong, we must check the parameters of the configuration file is set incorrectly.

5, configure nginx

1, install nginx:

   sudo apt install nginx

2, the directory structure:

  Ubuntu Nginx configuration files in / etc / nginx directory, nginx.conf main configuration file,

      Contains configuration files fefault, located in sites-available / down / directory, / sites-enabled / link file has the same name. Nginx and Apache under Ubuntu directory structure, the configuration file is basically the same. )

3, nginx is configured:

  cd /etc/nginx/sites-available
  vim default

     May be as follows:  

Copy the code
# The following is nginx.conf portion provided on server: 
server { 
        # This is the port used to access 
        the listen 80 ; 
        server_name localhost ;  charset UTF-. 8 ; the root / var / WWW / HTML ; index index.html index.htm debian.html-index.nginx ; # /var/log/nginx/access.log log file access_log ; error_log /var/log/nginx/error.log ; client_max_body_size 75M ; # specify uwsgi service connection, which is a key part of the location / the include uwsgi_params { ; # uwsgi same content 127.0.0.1:8080 uwsgi_pass ; # 30 link timeout uwsgi_read_timeout ; } static folder setting # LOCATION / static / {autoindex ON ; Alias / Home / username / Django-Project / static /; } # Upload folder setting LOCATION / Media / {autoindex ON ; Alias / Home / username / Django-Project / Media / ; 
}
}
Copy the code

 Note: If you use the built-django admin backend, admin static directory can not be accessed, so the method can be configured / static / admin to the next source in django admin static directory, or the admin directory are copied to the static directory of your project .

4, start nginx service.

  Start nginx: service nginx start
  close nginx: service nginx stop
  restart nginx: service nginx restart
  View status: service nginx status

  浏览器访问http://localhost/ 可看到django项目已运行。

6, summed up

  1. Testing Django project, to ensure the normal operation;
  2. Installation uwsgi, the Django project files, configuration uwsgi.ini, start uwsgi;
  3. Install nginx, configuration nginx.conf, start nginx.
  4. Note that the user's permission settings, project folder, the folder permissions to upload files and database settings, network user is www-data.
Good text to the top follow me The paper collection

Foreword

Django's deployment there can be many ways to use IIS or Apache under Windows, Nginx can be.

Using nginx + uwsgi manner is one of the more common way, all requests is nginx as the most front-end server, it will receive WEB unified management request. nginx all the static requests into their own hands. Then, nginx all requests pass through the non-static uwsgi to Django, it is handled by Django, WEB to complete a request. Uwsgi a similar effect on the bridge, play the role of a bridge.

The local development django project to deploy uwsgi server on Ubuntu, and configure nginx, finished running on nginx + uwsgi + django on the line. The following compilation of relevant procedures.

1, server configuration virtualenv

If you do not want to directly use python runtime environment, you need to install and configure virtual env on the server. If the direct use of python environment, you can skip this step.

  pip install virtualenv

  pip install virtualenvwrapper

We need to configure the environment variables to be able to use the commands:

Create a .bash_profile at ~, add the following:

  export WORKON_HOME=$HOME/.virtualenvs
  export PROJECT_HOME=$HOME/workspace
  source /usr/local/bin/virtualenvwrapper.sh

Then run the command: source ~ / .bash_profile

Configuration is complete. Use the following command:

  mkvirtualenv its_name # Create a virtual env
  workon its_name # to switch to a lower env (if you do not specify its_name) it is to list all the existing env
  deactive # exit the current env
  rmvirtualenv its_name # delete a virtual env

2, the installation dependencies

First, the development of local, need to export pip dependencies list.

pip freeze > plist.txt

Put this file to the server environment and batch installation dependent.

workon [虚拟环境名称] pip install -r plist.txt -i https://mirrors.tuna.tsinghua.edu.cn # 这里使用清华大学镜像源安装

Modify 3, django project

You need to modify settings.py file, close the debug mode.

DEBUG = False
ALLOW_HOSTS=['*','ipdress'] #加入访问服务器的ip

4, configuration uwsgi

Installation: pip install uwsgi

Django project required under the root directory (the same directory with manage.py), a new uwsgi.ini file, write configuration:  

Copy the code
Myweb_uwsgi.ini File # 
[uwsgi] 
socket =: 8080 # real service port, 
# Django project root directory (absolute path) 
chdir = / Home / username / myweb  # wsgi.py file location in the project  Module = myweb.wsgi  = Master to true running processes # 4 = number of processes Vacuum to true =使用nginx连接时,使用socket
Copy the code

Switch to the project under myweb directory, read myweb_uwsgi.ini file to start the project by uwsgi command.

启动uwsgi: uwsgi --ini xxx.ini
关闭uwsgi: uwsgi --stop xxx.pid
heavy Sirs uwsgi: uwsgi --reload xxx.pid

 Start state:

Copy the code
[uWSGI] getting INI configuration from myweb_uwsgi.ini
*** Starting uWSGI 2.0.12 (32bit) on [Sat Mar 12 13:05:06 2019] ***
compiled with version: 4.8.4 on 26 January 2016 06:14:41
os: Linux-3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:18:00 UTC 2015
nodename: ubuntu
machine: i686
clock source: unix
detected number of CPU cores: 2
current working directory: /home/fnngj/pydj/myweb
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/fnngj/pydj/myweb
your processes number limit is 15962
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8000 fd 3
Python version: 3.4.3 (default, Oct 14 2015, 20:37:06)  [GCC 4.8.4]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x8b52dc0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 319920 bytes (312 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x8b52dc0 pid: 7158 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 7158)
spawned uWSGI worker 1 (pid: 7160, cores: 1)
spawned uWSGI worker 2 (pid: 7161, cores: 1)
spawned uWSGI worker 3 (pid: 7162, cores: 1)
spawned uWSGI worker 4 (pid: 7163, cores: 1)
Copy the code

Note View startup information uwsgi, if there is wrong, we must check the parameters of the configuration file is set incorrectly.

5, configure nginx

1, install nginx:

   sudo apt install nginx

2, the directory structure:

  Ubuntu Nginx configuration files in / etc / nginx directory, nginx.conf main configuration file,

      Contains configuration files fefault, located in sites-available / down / directory, / sites-enabled / link file has the same name. Nginx and Apache under Ubuntu directory structure, the configuration file is basically the same. )

3, nginx is configured:

  cd /etc/nginx/sites-available
  vim default

     May be as follows:  

Copy the code
# The following is nginx.conf portion provided on server: 
server { 
        # This is the port used to access 
        the listen 80 ; 
        server_name localhost ;  charset UTF-. 8 ; the root / var / WWW / HTML ; index index.html index.htm debian.html-index.nginx ; # /var/log/nginx/access.log log file access_log ; error_log /var/log/nginx/error.log ; client_max_body_size 75M ; # specify uwsgi service connection, which is a key part of the location / the include uwsgi_params { ; # uwsgi same content 127.0.0.1:8080 uwsgi_pass ; # 30 link timeout uwsgi_read_timeout ; } static folder setting # LOCATION / static / {autoindex ON ; Alias / Home / username / Django-Project / static /; } # Upload folder setting LOCATION / Media / {autoindex ON ; Alias / Home / username / Django-Project / Media / ; 
}
}
Copy the code

 Note: If you use the built-django admin backend, admin static directory can not be accessed, so the method can be configured / static / admin to the next source in django admin static directory, or the admin directory are copied to the static directory of your project .

4, start nginx service.

  Start nginx: service nginx start
  close nginx: service nginx stop
  restart nginx: service nginx restart
  View status: service nginx status

  浏览器访问http://localhost/ 可看到django项目已运行。

6, summed up

  1. Testing Django project, to ensure the normal operation;
  2. Installation uwsgi, the Django project files, configuration uwsgi.ini, start uwsgi;
  3. Install nginx, configuration nginx.conf, start nginx.
  4. Note that the user's permission settings, project folder, the folder permissions to upload files and database settings, network user is www-data.

Guess you like

Origin www.cnblogs.com/sdlyxyf/p/11356866.html
Recommended