Python execution server construction (Python + uWSGI + Django + Nginx)

Overview

Try to build an environment like Python + uWSGI + Django + Nginx. I'm feeling better now, and I'm working with reference to other Qiita articles in the first place, so do I need to bother to write an article? There is a question, but well, I will leave it as a memorandum.

environment

environment URL
AWS https://〜/hello/

Horizontal expansion

If you want to increase the number of sites (apps), perform the following work.

--Create projects and apps in Django --Create nginx.conf, ini and service files for your growing site --Start the service


procedure

1) Create a directory

mkdir -p /WORK/etc;
mkdir -p /WORK/var/www;
mkdir -p /WORK/var/log;
mkdir -p /WORK/var/tmp;
mkdir /etc/nginx/sites-available;
mkdir /etc/nginx/sites-enabled;

2) Create a configuration file

Nginx settings

/etc/nginx/nginx.conf


user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    #-- WORK
    include /etc/nginx/sites-enabled/*.conf;
}

/WORK/etc/nginx_myweb.conf


upstream django {
    server 127.0.0.1:8001;
}
server {
    #-- SSL
    listen       443 ssl http2 default_server;
    listen       [::]:443 ssl http2 default_server;
    server_name  WORK.work;
    ssl_certificate     "/etc/letsencrypt/live/WORK.work/fullchain.pem";
    ssl_certificate_key "/etc/letsencrypt/live/WORK.work/privkey.pem";
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    charset utf-8;
    client_max_body_size 75M;

    #-- myweb
    location /static {
        alias /WORK/var/www/myweb/static;
    }
    location / {
        uwsgi_pass  django;
        include     /WORK/var/www/myweb/uwsgi_params;
    }
}

UWSGI settings

/WORK/etc/myweb.ini


[uwsgi]
master = true
socket = :8001
module = myweb.wsgi
pythonpath = /WORK/var/www/myweb
logto = /WORK/var/log/uwsgi.log
pidfile = /WORK/var/tmp/uwsgi.pid

Service settings

/WORK/etc/myweb.service


[Unit]
Description = myweb
After = syslog.target
[Service]
ExecStart = /usr/bin/uwsgi --ini /WORK/etc/myweb.ini
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target

3) Project and app generation

Generate projects and apps in Django

Project generation


cd /WORK/var/www;
django-admin startproject myweb;

App generation


cd /WORK/var/www/myweb;
python manage.py startapp hello;

4) App settings

/WORK/var/www/myweb/myweb/settings.py


INSTALLED_APPS = [
<Omitted>
    'hello',← Add
]
<End of sentence>
STATIC_ROOT = os.path.join(BASE_DIR, "static/")← Add

/WORK/var/www/myweb/hello/views.py


# Create your views here.
↑ Add below from here ↓ ↓ ↓
from django.http import HttpResponse
<Blank line>
def main(request):
    return HttpResponse("Hello!")

/WORK/var/www/myweb/myweb/urls.py


import hello.views ← Add
from hello import views ← Add
<Blank line>
urlpatterns = [
<Omitted>
    url(r'^hello/', hello.views.main),← Add
]

5) Migration

python manage.py migrate;
python manage.py collectstatic;

6) Service start (deployment)

Nginx


ln -s /WORK/etc/nginx_myweb.conf /etc/nginx/sites-enabled/.;
systemctl status nginx;
systemctl start nginx;
systemctl stop nginx;
systemctl start nginx;
systemctl status nginx;

uWSGI


ln -s /WORK/etc/myweb.service /etc/systemd/system/myweb.service;
systemctl status myweb;
systemctl start myweb;
systemctl stop myweb;
systemctl start myweb;
systemctl status myweb;

7) When the application is changed

--Implement 5 migrations (python manage.py migrate;) ――Implement 6 uWSGI glue start (systemctl restart myweb;)

Recommended Posts

Python execution server construction (Python + uWSGI + Django + Nginx)
Create Nginx + uWSGI + Python (Django) environment with docker
Python server settings (nginx + Gunicorn)
Django development environment construction with Docker-compose + Nginx + uWSGI + MariaDB (macOS edition)
Create Python + uWSGI + Nginx environment with Docker
Deploy Django (Ubuntu 14.04 LTS + Nginx + uWSGI + Supervisor)
Python development server construction procedure (AWS + Anaconda)
Run python wsgi server on NGINX Unit
Django environment construction
Python Django Tutorial (2)
Environment construction (python)
Hello World with nginx + uwsgi + python on EC2
Python Django Tutorial (8)
(Case) Django (DRF) + uWSGI + Nginx + MySQL docker-compose.yml sample
Python Django Tutorial (6)
Python --Environment construction
Python environment construction
[Django3] Environment construction and various settings summary [Python3]
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
python environment construction
Python Django Tutorial (3)
Python Django Tutorial (4)
Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (installation)
Web application made with Python3.4 + Django (Part.1 Environment construction)
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (implementation)
Install django on python + anaconda and start the server
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Django Middleware Execution Order
python> udp> echo server
Python Django tutorial summary
Django python web framework
Django project environment construction
Function execution time (Python)
python windows environment construction
Non-blocking with Python + uWSGI
homebrew python environment construction
Django Python shift table
Python development environment construction
Web server construction commentary
Try Debian + Python 3.4 + django1.7 ...
[Personal notes] Python, Django
Output python execution time
Ubuntu (18.04.3) Web server construction
python2.7 development environment construction
WebSocket with Python + uWSGI
DNS server in Python ....
Mac environment construction Python
python & jupyterlab container construction
Python Django CSS reflected
Local server with python
Python environment construction @ Win7
Python Basic Course (3 Python Execution)
Initial setting of environment using Docker-compose + Django + MySQL + Nginx + uwsgi
Until Python [Django] de Web service is released [Environment construction]
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04