Ubuntu 14.04+Django 1.7.1+Nginx+uwsgi deployment tutorial

  • 2020-04-02 14:22:11
  • OfStack

Specific environment:
Ubuntu 14.04 Python 2.7.6 Django 1.7.1 Virtualenv name:test Nginx uwsgi

Assume that the project folder is located in /data/ WWW /ts Settings saved in./conf


virtualenv name = test
domain name = example.com

Deployment of django+uwsgi was a pain in the ass.. Existing online tutorials seem to have compatibility problems with new versions. Finally run to the uwsgi official website to find the tutorial finally run through..
However, the tutorial on the website seems to be instructional in nature, which makes it detour when deploying

(link: http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html)

First, you need a uwsgi_params file in the project's conf folder. And then we need to point to it. The contents of the document are as follows:


uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

Create a file called ts_nginx.conf that looks like this


# ts_nginx.conf # the upstream component nginx needs to connect to
upstream 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 server
server {
    # the port your site will be served on
    listen 80;
    # the domain name it will 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 /data/www/ts/media; # your Django project's media files - amend as required
    }     location /static {
        alias /data/www/ts/static; # your Django project's static files - amend as required
    }     # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass django;
        include /data/www/ts/conf/uwsgi_params; # the uwsgi_params file you installed
    }
}

Connect the conf file to nginx's search directory.


sudo ln -s /data/www/ts/conf/ts_nginx.conf /etc/nginx/sites-enabled/

Prerequisites: set the static files in the Settings of your django project


STATIC_ROOT = os.path.join(BASE_DIR, "static/")
and then run python manage.py collectstatic

After:


service nginx restart

You should be able to see that
(link: http://example.cn:8000/media/1.gif)
(insert a static file first)

The next blabla step is all crap, jump here:


Configuring uWSGI to run with a .ini file

Ts_uwsgi.ini is in the project root


[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /data/www/ts
# Django's wsgi file
module = ts.wsgi
# the virtualenv (full path)
home = /root/.envs/test
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = 127.0.0.1:8001
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
# set an environment variable
env = DJANGO_SETTINGS_MODULE=conf.settings
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file

The conf folder is init.py, otherwise conf wouldn't be considered a module

Currently all ports except port 80 are accessible via address: port. I have tested 8000. I don't know why 81.80 doesn't work. To be continued tomorrow)


Related articles: