Note : A long time ago there was the idea of learning Django, and recently there was a chance to make a try. Since there are a lot of Django tutorials, I'm not going to go into the details here, just documenting the entire development process and some of my own thoughts in learning Django.
System:centos Linux release 7.2.1511 (Core)
django:1.10
python:2.7.5
Two very good tutorials are recommended:
The DjangoBook (Chinese version): I started with this tutorial to learn, it is very interesting that this tutorial has a lot of comments, almost every paragraph, from 10 onwards until now. Although the book is old and a lot of content is outdated, there are explanations, errata and the latest practices in these comments that make the whole learning process very interesting. I have left a lot of comments myself, haha.
liberated Jiang GE : This is another blog park blogger Vamei (Chinese reading, digging coal) written about the Django introduction of the series of articles, basic concepts, simple practice all have.
Not the main point.
- Create a dedicated user, such as Www-data: During the later deployment of Django, I encountered the most permissions issues since I used Linux. The big reason is that because of the need for communication between different processes, or mutual access, this requires that these users have a file read and write permissions. For example, when all two processes have access to the same socket file, the user of both processes has read and write access to the socket file. As a result, all of these processes are assigned to a dedicated user to operate, avoiding many permissions issues.
- URLs are one of the core elements of Web development: At the client, each URL is a page; from the developer's point of view, all development revolves around how to properly route these URLs to the correct HTML file.
- StackOverflow is a great website, most of the problems can be found in the above solution, if not find the answer, ask it, you can get a reply soon.
Overview
The user accesses a Django-developed page from the browser, the whole process is this:
The Web client <-> the Web server <-> the socket <-> WSGI <-> Django
The user accesses a URL from the browser, which is sent from the user to the Web Server,web server to communicate with WSGI through a socket (or a port), which is then sent by Wsgi to Django. Both Web server and WSGI have many options, and the common combinations are Apache + Mod_wsgi and Nginx + Uwsgi. I use the Nginx + Uwsgi. Here are a few nouns to begin with.
Web server:
Although each Web server program has a lot of different, but there are some common features: each Web server program needs to accept HTTP requests from the network, and then provide an HTTP reply to the requestor. An HTTP reply typically contains an HTML file that can sometimes contain plain text files, images, or other types of files.
In general, these files are stored in the local file system of the Web server, and the URL and local file name have a class structure, the server will simply control the URL to the local file system. When the Web server software is properly installed and set up, the server administrator assigns a local path name to the root directory from where the server software places the files.
Web server is the first door to the user's request, and some requests are handled by the Web server itself, such as access to static files, and other requests are given to WSGI processing, such as access to dynamic pages.
socket: Recently in the study of "Computer network" This course, socket related content in the network model belongs to the transport layer, located between the network layer and the application layer. Primarily used to implement interprocess communication. Here is the main implementation of Nginx and UWSGI two different processes between the communication.
WSGI:
The Web server gateway Interface (Python Interface, abbreviated as WSGI) is a simple and common interface between a Web server and a Web application or framework defined for the Python language. Similar interfaces have appeared in many other languages since Wsgi was developed.
Wsgi can be seen as a protocol, and it is said that there are many web frameworks in Python because Wsgi calls are very handy. UWSGI is the implementation of the WSGI agreement. In the actual use of the process, Uwsgi replaced the Python manage.py runserver role, of course, there are other functions.
The oldest Web server only supports static HTML. As sites become more complex, there are dynamic technologies. But the server does not directly run php,asp such files, therefore need a third party, with a third party to make a contract, I send the request parameters to you, and then I receive your processing results to the client. This Convention is common Gateway Interface, or CGI. This protocol can be implemented with Vb,c,php,python.
Simply put, CGI is a bridge between Web apps and HTTP server.
In addition to CGI, there are also Wsgi (Web Service Gateway Interface). The location of the WSGI layer is lower than CGI, and unlike CGI, WSGI is highly scalable and can run in multi-threaded or multi-process environments, because WSGI is only a standard and does not define how to implement it. In fact, WSGI is not CGI because it is between the Web application and the Web server, and the Web server can be CGI.
All started in Settings file
If we have created a Hello app and how to create it, please refer to the two tutorials above. So, what happened when we visited http://192.168.1.100:8000/hello/? All starts with the settings file. When you run Python manage.py runserver, the script will look for files named setting.py in manage.py same directory. This file contains all the configuration information about this Django project, capitalized: template_dirs, database_name, etc. the most important setting is root_urlconf, It will be used as urlconf to tell Django that the Python modules in this site will be available. Open the file settings.py you will see the following:
' Mysite.urls '
The corresponding file is mysite/urls.py when accessing url/hello/, Django loads URLCONF according to the root_urlconf settings. Then match the urlpatterns in the urlconf in order, until a match is found. When the matching urlpatterns is found, the associated view function is called and the HttpRequest object is used as the first argument, and a view function must return a HttpResponse. Once done, Django will complete the remainder of the converted Python object to a suitable web Response with HTTP headers and body (for example, Web content).The following procedure is implemented from HTTP Requese to HTTP response: access request, setting.py, urls.py, views.py, hello ()
# urls.py from Import URL, include from Import Admin from Import = [ url (r'^hello/$', hello),]
# viwes.py from Import HttpResponse def Hello (Request): return HttpResponse ("Hello world! ")
Communication between Uwsgi and Django
The communication between Uwsgi and Django is implemented through the wsgi.py file, which is generated when Django project is created. The main point of the setup here is to correctly indicate the location of the settings.py file. Because everything in Django starts with this file. You can add project's home directory/home/www-data/djcode/mysite/to the path of the current module scan before you can import additional files from that path.
@ wsgi.py
"" " wsgi config for HelloWorld Project. It exposes the WSGI callable as a module-level variable named ' Application '. For more information on the this file, seehttps://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/ "" " import OS, sys # Calculate the path based on the location of the WSGI script. Base_dir = Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__ # if the wsgi.py file is located in/home/www-data/djcode/mysite/mysite/, then base_dir to/home/www-data/djcode/mysite/
# Temple
# ADD the path to 3rd party Django application and to Django itself.
from Import
os.environ['django_settings_module'mysite.settings ' # equivalent to importing mysite/settings.py files from Base_dir
= get_wsgi_application ()
Configuration of Nginx
The Nginx configuration took a long time because it was not familiar at first. The key point is that there is no common port number (port) between different servers, so if you want to use the same port number, such as Port 80, all routes are configured in one server.
Nginx configuration file, can refer to here nginx.conf
Other
Process Management : There are many tools for process management, and the emperor mode in Uwsgi has similar functionality to the process management tools, which do not seem to be shared. I choose to be supervisor.
Supervisor (http://supervisord.org) is a python-written process management tool that can be easily used to start, restart, and close processes (not just the Python process). In addition to controlling a single process, you can start and close multiple processes at the same time, such as unfortunate server problems that cause all applications to be killed, and you can start all applications with supervisor at the same time instead of one by one.
PHP Files:PHP operation requires installation of PHP-FPM
Reference:
Https://zh.wikipedia.org/wiki/%E7%B6%B2%E9%A0%81%E4%BC%BA%E6%9C%8D%E5%99%A8
https://zh.wikipedia.org/wiki/Berkeley%E5%A5%97%E6%8E%A5%E5%AD%97
Https://zh.wikipedia.org/wiki/Web%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%BD%91%E5%85%B3%E6%8E%A5%E5%8F%A3
http://djangobook.py3k.cn/2.0/
Http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
Https://www.quora.com/What-are-the-differences-between-nginx-and-gunicorn # Here's why you should use Nginx and Gunicorn (function with Uwsgi)
http://data-eater.com/python-worker/
http://www.shellwjl.com/2016/03/07/ali_flask_wsgi_Nginx/
Build Web apps and their deployments with Django