[PYTHON] Deploy Django (Ubuntu 14.04 LTS + Nginx + uWSGI + Supervisor)

Overview

I had a lot of trouble deploying Django with the combination of titles, so I'll make a note for the next time. I will leave the place I was addicted to.

The combination is as follows

Django

Make it to some extent and execute the following command to confirm that the application works from the browser. If this is not possible, it is useless to do the following.

$ python manage.py runserver 0.0.0.0:8000

uWSGI settings

uWSGI was installed with apt, but it didn't work. I was addicted to this alone for 2 days.

When I run uwsgi with sudo, I get the following error and it doesn't work.

$ sudo uwsgi ***

When I check it with a browser, the log shows no python application found.

--- no python application found, check your startup logs for errors ---
[pid: 10523|app: -1|req: -1/1] 121.101.66.160 () {40 vars in 785 bytes} [Sun Feb 19 14:12:06 2017] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)

If you google, you will find this information ... http://www.yodiaditya.com/no-python-application-found-check-your-startup-logs-for-errors/

Uninstall uWSGI (this procedure is not necessary)

sudo apt-get remove uwsgi*
sudo apt-get purge uwsgi*

uWSGI installation

Install uWSGI via pip. (I forgot to add sudo)

pip install uwsgi

Confirm before making an ini file (http) Access http: // ****: 8889 / admin with a browser.

$ uwsgi --http :8888 --file /home/<user>/<pass>/<to>/<projectname>/<projectname>/wsgi.py

I saw the site.

Nginx settings

It was set as follows. For the time being, on port 8878.

# sudo vi /etc/nginx/conf.d/uwsgi.conf

upstream django {
    server unix:///tmp/<projectname>.sock;
}

server {
    listen 8887;
    access_log /var/log/nginx/<projectname>_access.log;
    error_log /var/log/nginx/<projectname>_error.log;

    location /static {
        alias /home/<user>/<pass>/<to>/<projectname>/static/;
    }

    location / {
         include uwsgi_params;
         uwsgi_pass django;
    }
}

Check the operation from the browser after starting uWSGI

$ uwsgi --socket /tmp/<projectname>.sock --chmod-socket=666 --file /home/<user>/<pass>/<to>/<projectname>/<projectname>/wsgi.py

Check with a browser (http: // ****: 8877 / admin)

error removing unix socket, unlink(): Operation not permitted [core/socket.c line 198]

Is there a socket file left when uWSGI is inserted via apt? I got a permission error. Delete the socket file with rm.

sudo rm /tmp/<projectname>.sock

I started uwsgi again and checked it with a browser again to see it.

uWSGI automatic startup setting (Supervisor)

This is entered from apt.

sudo apt-get install supervisor

Configuration file (I don't know if it matches, but it's working for the time being ...)

#  sudo vi /etc/supervisor/conf.d/uwsgi.conf
[program:uwsgi]
directory = /home/<user>/<pass>/<to>/<projectname>
command = /home/<user>/.pyenv/shims/uwsgi --ini /etc/uwsgi/apps-enabled/<projectname>.ini
stopasgroup = true
numprocs = 1
autostart = true
autorestart = true
user=root
redirect_stderr = true
stdout_logfile = /var/log/supervisor/uwsgi.log

Status check

sudo supervisorctl status

Check from the browser

http://*****:8889/adminとかで開けばOK! Restart the server and check if you can access it again and it's OK!

Future tasks

--I want to be able to run multiple Django applications in a subdirectory (http: // domain / subdirectory /: 80). --Since unicorn is also running for Ruby, I want to be able to sort uwsgi and unicorn from Nginx by http: // domain / subdirectory /. (Can both be done on port 80) --Log rotation

Recommended Posts

Deploy Django (Ubuntu 14.04 LTS + Nginx + uWSGI + Supervisor)
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Python execution server construction (Python + uWSGI + Django + Nginx)
Django Heroku Deploy 1
Create Nginx + uWSGI + Python (Django) environment with docker
Django Heroku Deploy 2
Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (installation)
Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (implementation)
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Install Docker (Ubuntu 18.04 LTS)
Installing Singularity (Ubuntu 18.04 LTS)
MongoDB installation (Ubuntu 18 LTS)
Initial setting of environment using Docker-compose + Django + MySQL + Nginx + uwsgi
Deploy Django + React from scratch to GKE (1) Backend development --Nginx + Django