For the newly-introduced students, when the project code is completed, in the local localhost debugging no problem, you will encounter how to get the project on-line problems. The information on the internet is too complicated, so I hope this article can simply be a doubt for the students who are just getting started.
1. Domain Name
First, when we enter a URL http://www.example.com/, first through the DNS resolution to the corresponding IP address, so that the IP implementation access. So, the first step to getting someone to visit our project is to have two things, a domain name and a public IP.
The domain name is easy to obtain, simply register to buy one just fine. Then you need to resolve the domain name to your public IP. and the public network IP, generally in the purchase of cloud server can be obtained.
After this step, we implemented: request-->dns--> server IP, and our ultimate goal is: request-->dns--> server ip--> black box--Project WSGI application
2. Run the project using Gunicorn
Here, we use Django to build the project when your project is finished. We need to run a listening request, receive the request, and invoke the corresponding WSGI application's service program. For example, executing python manage.py runserver 0.0.0.0:8000,runserver is a WSGI-based service program that Django comes with for debugging.
On the cloud server we create a project
django-admin.py Startproject Blog
where wsgi.py
Import Osimport sys# When you use isolated project environments such as VIRTUALENV, you need to add the path of the project to the lookup path, path = OS.GETCWD () If Path not in Sys.path: Sys.path.append (path) os.environ.setdefault ("Django_settings_module", "blog.settings") from Django.core.wsgi Import Get_wsgi_applicationapplication = Get_wsgi_application ()
We execute the following command under the project
Gunicorn wsgi:application
Default binding 127.0.0.1:8000, the use of specific gunicorn can be Google for yourself
At this point, we can access our project content via the local 8000 port. Implemented: Request-->dns--> server ip--> black box-->127.0.0.1:8000--> Project Wsgi app
3.nginx receiving external requests, internal forwarding
Under the/etc/nginx/sites-available/folder, create a new file blog and add the following simple settings
server { listen; server_name your domain name to your public IP (optional); Access_log /var/log/nginx/blog.log; location/static { #静态文件如js, CSS storage directory root/project/blog; } Location/{ include proxy_params; # forward to local 8000 port Proxy_pass http://127.0.0.1:8000 after receiving the request externally ;} }
From the above we can understand that Nginx received the request, forwarded to Gunicorn is listening to the local 8000 port, Gunicorn based on the request to invoke the corresponding application function in the project returned results.
Since then we have basically implemented the request-->dns--> server Ip-->nginx (80 port)-->127.0.0.1:8000--> Project WSGI Application
And about Nginx and Gunicorn specific configuration there are many, may as well as more Google to extend the study
4. Summary
Gunicorn let the project run.
Nginx is responsible for receiving requests and forwarding requests to the ports in the running project listening requests
Deploy to the line, the main need domain name, public network IP, both can be resolved through the cloud server, so it is best to buy a cloud server practice, just do it