Django is a high-level Python WEB framework this encourages rapid development and clean, pragmatic design.
WSGI is a generic gateway interface defined for the Python language that assumes the middle tier between the Python web framework (Django, Flask, web.py, and so on) and the Web server (Nginx, Apache, LIGHTTPD, and so on).
- Django
Install Django into your virtualenv, create a new project, and into the cd
project:
#$ pip install Django #$ django-admin.py startproject mysite #$ cd MySite
- Uwsgi
#$ pip Install Uwsgi
- now test whether the the Web client <-> uwsgi <-> Python stack can go through
a. Create a test.py file and write the following
<code>
# test.py def application): Start_response ( ' OK ', [( ' text/html ' )] return [b "Hello World" # python3 #return ["Hello World"] # Python2
</code>
B. Use Uwsgi to directly tune up the python file test:
#$ uwsgi--http:8000--wsgi-file test.py
C. Access & nbsp;http://example.com:8000
- Test if the Django project can run and execute:
#$ python manage.py runserver 0.0.0.0:8000If you can access the Django Web page, execute:
#$ Uwsgi--http:8000--module Mysite.wsgi
If you open a webpage and see a Django page stating the Web client <-> uwsgi <-> Django Configuration succeeded
- Install Nginx:
#$ sudo apt-get install nginx#$ sudo/etc/init.d/nginx start # start Nginx
Visit localhost:80 If an IT work Description Nginx installation is successful
- Configure Nginx:
You'll need the uwsgi_params
file, which is available in the nginx
directory of the UWSGI distribution, or from Https://github. Com/nginx/nginx/blob/master/conf/uwsgi_params
Copy it into your project directory. In a moment we'll tell the Nginx to refer to it.
Now create a file called mysite_nginx.conf, and put the This in it:
# mysite_nginx.conf# The upstream component Nginx needs to connect Toupstream Django{# server Unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8001;# for a Web port socket (we'll use this first)}# Configuration of the Serverserver{# The Port your site is served on listen 8000;# The domain name it would serve for server_name. example.com;# Substitute your machine ' s IP address or FQDN charset Utf-8# max upload size client_max_body_size 75m# adjust to taste # Django media location/media { Alias/path/to/your/mysite/media# your Django project ' s media files-amend as required } location/static {alias/path/to/your/mysite/static# your Django project ' s static files-amend as required } # Finally, SE nd all Non-media requests to the Django server. Location/{uwsgi_pass django Include/path/to/your/mysite/uwsgi_params # the Uwsgi_params file you installed }
This conf file tells-serve up media and static files from the filesystem, as well as handle requests that require Django ' s intervention. For a large deployment it's considered good practice to let one server handle Static/media files, and another handle Djan Go applications, but for now, this would do just fine.
Soft-connect the configured Nginx configuration file to the Nginx configuration folder.
sudo ln-s ~/path/to/your/mysite/mysite_nginx.conf/etc/nginx/sites-enabled/
- Nginx Test
Restart Nginx:
Sudo/etc/init.d/nginx restart
To check that media files is being served correctly, add an image called media.png
/path/to/your/project/project/media directory
to the and then visit http://example.co M:8000/media/media.png-if This works, you'll be know at least, which Nginx is serving files correctly.
It is worth not just restarting Nginx, but actually stopping and then starting it again, which would inform if there is A problem, and where it is.
- associating Nginx with Uwsgi and python files
Let's get nginx to speak-the "Hello World" test.py
application.
Uwsgi--socket:8001--wsgi-file test.py
This was nearly the same as before, except this time one of the options is different:
socket :8001
: Use protocol Uwsgi, Port 8001
Nginx meanwhile have been configured to communicate with Uwsgi on that port, and with the "outside world" on port 8000. Visit:
Http://example.com:8000/
To check. And this are our stack:
The Web client <-> the Web server <-> the socket <-> uwsgi <-> Python
Meanwhile, can try to has a look at the USWGI output at http://example.com:8001-but quite probably, it won ' t work B Ecause your browser speaks HTTP, not Uwsgi, though-should see output from Uwsgi in your terminal.
- Replace the ports entry in the Nginx configuration file with the UNIX sockets
Edit mysite_nginx.conf
, changing it to match:
Server Unix:///path/to/your/mysite/mysite.sock# for a file sockets# server 127.0.0.1:8001; # for a Web port Soc Ket (we ' ll Use this first)
and restart Nginx.
Run Uwsgi again:
Uwsgi--socket mysite.sock--wsgi-file test.py
This time, the socket
option tells Uwsgi which file to use.
Try localhost:8000 in the browser.
If the access is unsuccessful, make the following attempt:Check your Nginx error log (/var/log/nginx/error.log). If you see something like:
Connect(13:permissiondenied)
Then probably your need to manage, the permissions on the socket so, Nginx is allowed to use it.
Try:
Uwsgi--socket mysite.sock--wsgi-file test.py--chmod-socket=# (very permissive)
Or
Uwsgi--socket mysite.sock--wsgi-file test.py--chmod-socket=# (more sensible)
You may also has to add your user to Nginx's group (which is probably www-data), or Vice-versa, so-nginx can read an D write to your socket properly.
It ' s worth keeping the output of the Nginx log running in a terminal window so you can easily refer to it while Troublesho Oting.
- Run the Django app with Uwsgi and Nginx
Let's run our Django application:
Uwsgi--socket mysite.sock--module mysite.wsgi--chmod-socket=664
Now Uwsgi and Nginx should is serving up isn't just a "Hello world" module, but your Django project.
- starting Uwsgi with an. ini file
We can put the same options that we used with UWSGI into a file, and then ask Uwsgi to run with that file. It makes it easier to manage configurations.
Create a file called `mysite_uwsgi.ini`
:
# Mysite_uwsgi.ini File[Uwsgi]# django-related Settings# The base directory (full path)ChDir=/path/to/your/project# Django ' s WSGI filemodule = project.wsgi# the Virtualenv (full path) home =/ Path/to/virtualenv# process-related settings# Mastermaster Span class= "o" >= true# maximum number of worker Processesprocesses = 10# the socket (use the full path to be Safesocket =/ Path/to/your/project/mysite.sock# ... with appropriate permissions-may be Needed# Chmod-socket = 664# Clear environment on Exitvacuum = true
To start up the server:
# The--ini option is used to specify a file
Again, if a Django welcome interface appears, all configuration items are configured successfully.
Original link: https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html