• NginX + PostgreSQL + uWSGI + Django


    1.Install PostgreSQL

    rpm -Uvh https://yum.postgresql.org/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
    yum install postgresql10-server postgresql10
    /usr/pgsql-10/bin/postgresql-10-setup initdb
    
    • 1
    • 2
    • 3

    edit the following two conf files, configure who can access the site and what address the site listens to.

    /var/lib/pgsql/10/data/postgresql.conf
    /var/lib/pgsql/10/data/pg_hba.conf

    CREATE DATABASE azendb;
    CREATE USER azen WITH ENCRYPTED PASSWORD ‘password’;
    GRANT ALL PRIVILEGES ON DATABASE azendb TO azen;

    $ python manage.py migrate  
    $ python manage.py makemigrations blogs  
    $ python manage.py migrate  
    
    • 1
    • 2
    • 3

    pg_dump azendb > azendb.bak
    psql azendb < azendb.bak

    crontab to back up data regularly:
    59 23 * * * /usr/bin/pg_dump “host=localhost hostaddr=127.0.0.1 port=5432 user=azen password=azen dbname=azendb” > azendb.bak

    2.Install Python3.6

    sudo yum install -y https://centos7.iuscommunity.org/ius-release.rpm

    sudo yum update

    sudo yum install -y python36u python36u-libs python36u-devel python36u-pip

    Remember that yum, firewall-cmd, urlgrabber use python 2.7. Modify python files’ first line when necessary.

    3.Django project folder

    Pull codes

    cd /var/www
    
    git clone git@github.com:xxx/xx.git (pull codes through ssh)
    
    • 1
    • 2
    • 3

    set up venv

    python -m venv venv4zen
    
    • 1

    activate the virtual environment

    source ./venv4azen/bin/activate
    
    pip3 install Django  
    
    pip3 install Django-Markdownx
    
    deactivate
    
    pip3 install psycopg2-binary
    
    pip3 install uwsgi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4.Configure

    make uwsgi.service file so that uWSGI can work as a deamon

    touch /etc/systemd/system/uwsgi.service

    [Unit]
    Description = uWSGI Emperor
    After = syslog.target
    
    [Service]
    ExecStart=/usr/local/bin/uwsgi --ini /etc/uwsgi/emperor.ini
    ExecStop = kill -INT `cat /run/uwsgi.pid`
    ExecReload = kill -TERM `cat /run/uwsgi.pid`
    Restart = always
    Type = notify
    NitifyAccess = main
    PIDFile = /run/uwsgi.pid
    
    [Install]
    Wantedby=multi-user.target
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    create uwsgi user.

    useradd uwsgi -s /bin/false
    
    • 1

    create file /etc/uwsgi/emperor.ini

    [uwsgi]
    emperor = /etc/uwsgi/vassals
    uid = uwsgi
    gid = nginx
    logto = /etc/uwsgi/log
    
    • 1
    • 2
    • 3
    • 4
    • 5

    create file azen.ini under /etc/uwsgi/vassals

    [uwsgi]
    socket = 127.0.0.1:3030
    chdir = /var/www/azen
    venv = /var/www/venv4azen
    wsgi-file = azen/wsgi.py
    uid = uwsgi
    gid = nginx
    
    #uwsgi --socket 127.0.0.1:3030  --chdir /var/www/azen --venv /var/www/azen/venv4azen --wsgi-file azen/wsgi.py --master --processes 4 --threads 2 --logto /etc/uwsgi/log
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    configure a file azen.conf under /etc/nginx/conf.d/

    server{
    	listen       80 default_server;
    	listen       [::]:80 default_server;
    	server_name  192.168.56.101;
    	error_log    /var/www/azen/log/error_log;
    	access_log   /var/www/azen/log/access_log;
    	charset      utf-8;
    	location /static/ {
    		alias /var/www/azen/static/;
    	}
    
    	location / {
    		include uwsgi_params;
    		uwsgi_pass 127.0.0.1:3030;
    	}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Secure Django Admin with SSL Client Certificate in Nginx

    https://gist.github.com/mtigas/952344

  • 相关阅读:
    前端(二十六)——常见的HTTP异常状态码以及正反向代理配置
    6-6 循环队列入队出队 分数 10
    数据可视化?这些平台能处
    Springboot集成Swagger2(亲测直接可用)
    深度理解CNN中的感受野(大杀器)
    SQL—— 优化
    在 Windows 10/11 上恢复已删除文件的 9 种简单方法
    无涯教程-JavaScript - DDB函数
    什么样的人不适合做管理者
    【牛客网-面试必刷TOP 101】01链表
  • 原文地址:https://blog.csdn.net/azenlijing/article/details/125542080