Git Product home page Git Product logo

dj_gunicorn_nginx_demo's Issues

deploy tutorail

  1. install env
#!/bin/bash
sudo apt update
sudo apt -y install software-properties-common
yes | sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt -y install python3.8
sudo apt -y install python3-pip
sudo apt-get -y install python3-venv
python3 -m venv venv
sudo apt-get -y install nginx

remind CR LF type
image

  1. activate venv
source venv/bin/activate
  1. install py lib (you should install gunicorn when venv is activate, or the path is not in ENV var, which means cmd which gunicorn will get none.)
pip3 install django==3.0.2
pip3 install gunicorn==20.0.4
  1. git clone
git clone https://github.com/ARJhe/dj_gunicorn_nginx_demo.git
mv dj_gunicorn_nginx_demo demo
  1. Test whether project can run. (remind port is available or not)
cd ~/<yourprojectdir>
python3 manage.py runserver 0.0.0.0:8069
  1. Test gunicorn
cd ~/demo
gunicorn --bind 0.0.0.0:8069 demo.wsgi
  1. set static file location (for nginx)
deactivate

7-1. settings.py

cd ~/demo/demo

ALLOWED_HOSTS = [ 'domain.com', '<yourserverip>', 'localhost']
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

7-2. collect static file

cd ~/<yourprojectdir>
python3 manage.py collectstatic
  1. user add to www-data group (cmd id to check whether in group)
sudo usermod -a -G www-data <user_name>
  1. create gunicorn unix socket (gunicorn rely on unix socket to communicate with system process, so if bind in TCP socket, can not work with nginx )
sudo vim /etc/systemd/system/gunicorn.socket

[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
  1. create gunicorn service to run as deamon
sudo vim /etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=<user_name>
Group=www-data # make sure your user is in this group
WorkingDirectory=/home/ubuntu/demo
ExecStart=<Path of which gunicorn > --bind unix:/run/gunicorn.sock demo.wsgi:application

[Install]
WantedBy=multi-user.target
  1. activate gunicorn socket
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket

11-1. check if enable or not.

sudo systemctl status gunicorn.socket

11-2. check if run dir has a file named gunicorn.socket

file /run/gunicorn.sock
  1. Nginx setting

12-1.

sudo vim /etc/nginx/sites-available/demo

server {
    listen 80;
    server_name <domain_name> <public_IP>;
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        alias <your_static_root_path>; # static的根目錄位置
    }
location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

12-2. link available to enable

cd /etc/nginx/sites-enabled

sudo ln -s /etc/nginx/sites-available/demo
ls -l

12-3. activate

sudo service nginx start
sudo nginx -t
sudo systemctl restart nginx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.