Django deployment --uwsgi + Nginx (ultra-detailed)
Environment:
python3.6
CentOS 7
Django1.11
with Django wrote a small site, can only run in their own local a run! How this line? I heard that can be deployed on a cloud server so that others can visit!
Where to start? We went from Django started! The old rules, follow the steps:
1, the local run up Django
Here we do not speak Django project implementation process, suppose you've written a Django project, and the local 127.0.0.1:8000 be able to run up. Well, to give you a reference, the project is probably a long way:
- kindle directory of Django App I created
2, modify the project configuration
That is settings.py file in the project directory, the main emphasis in several places:
① close DEBUG mode:
Specifically
② modify ALLOWED_HOSTS:
Specifically
③ static configuration file storage path:
Specifically
After modifying the implementation of good configuration:
python manage.py collectstatic
Copy the static files used to project static file in the root directory of the project
3, and the installation uwsgi Nginx
This is nothing to say. . . In its own cloud server upload two good tools
installed after uwsgi good idea to verify the authentication method:
create a test.py file:
def application(env, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return [b'Hello world!'] #python3 # return ['Hello world!'] #python2
Start uwsgi server:
uwsgi --http :8000 --wsgi-file test.py
If you can start properly without an error it should be no problem, do not worry if revalidation in your terminal:
wget HTTP: // 127.0.0.1:8000 look at whether proper access to content
4, configuration items uwsgi
Creating uwsgi.ini configuration file in the folder where the project, which is here
As follows uwsgi.ini in:
[uwsgi] # Django - Related Settings # port here can be set according to their own socket = 127.0 . 0.1 : 8001 # at The Base Directory (Full path) # This is the root directory of your Django project, which is the directory that contains the App, FIG carefully control on the chdir = / Home / Python / book2kindle # S WSGI the Django File # project name is here (not App name) .wsgi Module1 = book2kindle.wsgi # Process - Related Settings # Master Master = to true # worker maximum Number of Processes Processes = 32 Appropriate with the Permissions ... # - May BE needed # chmod -socket = 664 # the Clear Environment ON Exit Vacuum = to true plugin python = to true # this is the project's virtual python environment, if useless virtual environment, you can not set the Home = / home / python / book2kindle_env
5, configuration items nginx
Find nginx configuration folder, centos7 of nginx configuration files in / etc / nginx, this path has a total nginx.conf profile, there are two folders ./conf.d,./default.d, we the nginx.conf copy to the next conf.d folder named with the following modifications to nginx.conf (or project name .conf) (corresponding to the configuration according to Chinese translations):
{HTTP upstream Django { # to note here and uwsgi.ini server profile consistent socket server 127.0 . 0.1 : 8002 ; # for A Web Port socket } server { the listen 80 ; server_name xxx.com; # here to fill their ip domain name or server (determine your access mode site in the future) charset UTF - 8 ; root / usr / report this content share / nginx / HTML; LOCATION / { root / Home / Python / book2kindle / the Kindle; # this is the path Django App of uwsgi_pass django; uwsgi_params the include; # The uwsgi_params File you Installed } LOCATION / static { path # Django project static files Alias / Home / Python / book2kindle / static; } } }
6, and nginx start uwsgi
Uwsgi.ini into the folder execute:
--This uwsgi uwsgi.ini
In Terminal execute:
service nginx restart
Then you can visit the website under the domain name (if you want to DNS server ip address) or server ip!
OK, get away! It's that simple!