first step.
sudo apt-get update
sudo apt-get upgrade
Update first. .
The mainstream deployment method of Django: nginx+uwsgi+django
The second step is to install nginx
sudo apt-get install nginx
Install nginx, If you need to install the latest nginx, you need to download the source code package from the official website and compile it manually.
The approximate file structure of nginx.
1. Configuration file: /etc/nginx
2. Program: /usr/sbin/nginx
3. Log: /var/log/nginx/access. log - error.log
The third step, install uwsgi
sudo apt-get install python3-dev
sudo apt-get install python3- pip
sudo pip3 install uwsgi (Before this step, you can change the pip source to improve the download speed. Create pip.conf under ~/.pip and write
[global]
trusted-host = pypi.douban.com
index-url = )
uwsgi is a web server that implements the WSGI protocol, uwsgi, http Waiting for agreement. The function of HttpUwsgiModule in Nginx is to exchange with uWSGI server.
The general process is: Client<==>nginx<==>uwsgi<==>Django. Static requests are handled by Nginx itself. Non-static requests are passed to Django through uwsgi, which is processed by Django to complete a WEB request.
Create a Django test project, django-admin startproject mysite, cd mysite, python manage.py startapp demo1.
The fourth step is to test uwsgi
Create a new test file in the mysite directory, nano test.py.
Write:
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')])return ["Hello World"]
Run:
uwsgi --http :8001 --plugin python --wsgi-file test.py
Access is normal.
The fifth step is to test that Django
python manage.py runserver 0.0.0.0:8002
has normal access.
Connect Django and uwsgi.
uwsgi --http:8001 --plugin python --module mysite.wsgi
Access is normal.
The sixth step is to configure uwsgi
uwsgi supports starting through a variety of configuration files. The ini configuration file method is used here.
New uwsgi: nano uwsgi.ini
# mysite_uwsgi.ini file[uwsgi] socket = 127.0.0.1:3400# Django-related settings # the django project directory (full path) chdir = /home/ubuntu/mysite # Django's wsgi filemodule = mysite.wsgi # process-related settings # master master = true# maximum number of worker processes processes = 2threads = 2max-requests = 6000# ... with appropriate permissions - may be neededchmod-socket = 664# clear environment on exit vacuum = true
An error occurred when accessing,invalid request block size: 21573 (max 4096)...skip
.
The reason is that the url address exceeds 4096 characters. The reason is that we use socket to start. Just change the socket in the configuration file to http, or modify the buffer-size.
(It is recommended not to make any changes, just change it to http during testing, and change it back to socket when connecting to nginx)
daemonize = /home/ubuntu/mysite/uwsgi.log
Replace this code when it is officially run Add it to the uwsgi.ini file, and the access log will be output to uwsgi.log in the background
At this time, Django can already access it.
Step 7, configure nginx
Modify nginx’s default configuration file/etc/nginx/sites-enabled/default
server { # the port your site will be served on listen 80; # the domain name it will serve forserver_name 127.0.0.1; # substitute your machine's IP address or FQDNcharset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias /home/ubuntu/mysite/media; # your Django project's media files - amend as required } location /static { alias /home/ubuntu/mysite/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { include uwsgi_params; # the uwsgi_params file you installed uwsgi_pass 127.0.0.1:8001;#此处跟uwsgi配置文件保持一致 } }
Remember to modify the uwsgi.ini configuration during testing.
Step 8, run
Restart nginx, run uwsgi.
Done
This is it for the time being, we will continue to add knowledge about nginx, django, and uwsgi in the future .
Most of the configurations come from Baidu search, so I won’t post each source one by one. Life is too short.
The above is the detailed content of Introduction to deployment examples of Ubuntu14.04. For more information, please follow other related articles on the PHP Chinese website!