install python
Download the source package in python:
wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz # unzip tar -zxf Python-3.6.5.tgz cd Python-3.6.5 #compile ./configure --prefix=/appliocation/python3 --enable-optimizations make && make install # Configure environment variables vim ~/.bash_prefile # Add python's bin path to this line, as follows export PATH=$HOME/bin:/usr/local/python3/bin:/usr/local/bin:$PATH source ~/.bash_profile
install django
pip3 isntall django==1.11.8 #Create a django project django- admin startproject hello_django cd hello_django #Create application python3 manager.py startapp app01 #Create uwsgi configuration file vim hello_django.ini #Enter these contents # myweb_uwsgi.ini file [uwsgi] # Django-related settings socket = :8080 # The port of the real service # Django project root directory (absolute path) chdir = /root/ hello_django # The location of the wsgi.py file in the project module = hello_django.wsgi # process-related settings # master master = true #Number of processes running processes = 4 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true # ##Install uwsgi pip install uwsgi
start uwsgi
uwsgi hello_django/ hello_django.ini It can only be accessed after modification in seeing.py, the default is only localhost, as follows ALLOWED_HOSTS = ['192.168.0.75', '127.0.0.1', 'localhost']
Configure Nginx
Find nginx.conf and write the following
server { #here is the port listen 80 used for access ; server_name localhost; charset UTF-8; client_max_body_size 75M; location / { include uwsgi_params; #Same as uwsgi content uwsgi_pass 127.0.0.1:8080 ; #Link timeout uwsgi_read_timeout 30 ; } }
In this way, restart your nginx, access port 80, and you can see the effect.
Still have questions?
You may have discovered that the static resources on your web page cannot be accessed! ! For example, the admin page will be particularly rudimentary, because when nginx+uwsgi+Django, Django's processing of static resources, nginx cannot proxy (maybe). In short, this kind of thing should not be done by Django, because nginx is more capable of processing static resources. For static resources, let nginx handle it.
Generally speaking, you will have two kinds of static resources /media/
at the beginning of the link and /static/
the beginning. Static is used to process original pictures, videos, js, css files of some websites. Django supports this kind of link by itself. So how to close /static/
the file that Django processes at the beginning, it is very simple, change setting.py
the DEBUG
value in False
, at this time, Django will not process /static/
the file.
So /media/
what? Generally speaking, we will save the pictures uploaded by users and use them when they are displayed on the webpage /media/
.setting.py
MEDIA_URL = ' /media/ ' #Access prefix link MEDIA_ROOT = os.path.join(BASE_DIR, ' ../media ' ) #The specific location where the file is stored
url.py
add in
from django.conf import settings from django.conf.urls.static import static if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This means that when DEBUG=True, the file will be parsed /media/
, and the location where the file is stored is the second parameter.
In this way, when deploying as a production environment, you only need to change DEBUG to False, and Django will not deal static
with media
it.
Collect static files
Django has a tool that collects all the static files used in the application for easy parsing by nginx. Specifically: Set the static files collected in this way into the above directory
. How to run this tool?setting.py
STATIC_ROOT = os.path.join(BASE_DIR, '../collectedstatic')
python3 manager.py collectstatic
Configure nginx to parse static files
Similarly, nginx.conf
First, add user root
a statement at the top of the file to let the root user run nginx, otherwise access to static files may prompt no permission
. Second, location /
add the following content before the configuration file mentioned above
location /static/ { autoindex is; alias /root/SchoolBuyWeb/collectedstatic/; } location /media/ { autoindex is; alias /root/SchoolBuyWeb/media/; }
After paying attention alias
, you can correspond to the directory you set yourself!
Restart nginx, now it's ok~~