Try   HackMD

Setup Ngnix with Uwsgi on Oracle Linux

關閉 SELinux

sudo vim /etc/sysconfig/selinux

SELINUX=enforcing -> disabled

增加 EPEL 套件來源

keyword: yum epel (oracle linux)

sudo vim /etc/yum.repos.d/oracle-epel-ol8.repo

set enable to 1

Django 前後端關係







Django前後端關係



NGINX

NGINX



backend\nuWSGI

backend
uWSGI



NGINX->backend\nuWSGI





frontend\nreact built static files

frontend
react built static files



NGINX->frontend\nreact built static files





django static files\n(for admin panel)

django static files
(for admin panel)



NGINX->django static files\n(for admin panel)





python APP

python APP



backend\nuWSGI->python APP





uWSGI 會開一個 socket 給 NGINX 使用,這樣兩者就可以互相溝通
uWSGI 會運作我們所撰寫的 python 檔案

Enable Ports for Web Service

enable port 80 & 443 for http(s)

sudo firewall-cmd --zone=public --permanent --add-port=80/tcp sudo firewall-cmd --zone=public --permanent --add-port=443/tcp sudo firewall-cmd --reload

Some configure on oracle cloud's panel is required.

// todo 土豆XDD
// iptable

Configure uWSGI

install uwsgi by pip and then add uwsgi.ini.

ref for uwsgi.ini

add service file for uWSGI so that it can be started automatically on boot.

sudo vim /etc/systemd/system/uwsgi.service
[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/usr/bin/bash -c 'mkdir -p /run/uwsgi; chown opc:nginx /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi -i /home/opc/Frontend_and_Backend/backend/uwsgi.ini
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

基本上就是改 ExecStartPreExecStart

systemctl commands:

# start a service systemctl start <S> # enable a service systemctl enable <S> [--now] # now for enable and start # same for stop and disable. # reload systemd conf, required after service files being edited. sudo systemctl daemon-reload

check systemd log

journalctl -xe

Install & Configure NGINX

Install nginx by yum.

sudo vim /etc/nginx/conf.d/wtf.conf sudo systemctl restart nginx sudo systemctl restart uwsgi

https://www.digitalocean.com/community/tutorials/how-to-set-up-uwsgi-and-nginx-to-serve-python-apps-on-ubuntu-14-04

Generate django static files

first of all, edit settings.py, modify STATIC_URL and STATIC_ROOT.
static file will be generated under STATIC_ROOT.
then, run the following command to generate static files:

python manage.py collectstatic

then configure NGINX for url STATIC_URL to provide static files.

sudo vim /etc/nginx/conf.d/wtf.conf

詳細的看最下面

Setup React Frontend

Install NodeJS 14

curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash - sudo yum install -y nodejs

generate production built

cd <your frontend> npm i # npm install npm run build # generate production optimized built cp build /var/www/..... chown nginx:nginx ... -R

https://certbot.eff.org/lets-encrypt/otherpip-nginx

https://certbot.eff.org/lets-encrypt/otherpip-nginx

full nginx config

server {
        listen 80;
        server_name tmp-project.ray-fish.me;

        index index.html index.htm index.nginx-debian.html;
        # location = favicon.ico { access_log off; log_not_found off; }
        location /api/ {
                include uwsgi_params;
                uwsgi_pass unix:/run/uwsgi/tmp_backend.sock;
        }

        location /admin {
                include uwsgi_params;
                uwsgi_pass unix:/run/uwsgi/tmp_backend.sock;
        }
        location /static_django {
                alias /var/www/static_django;
                try_files $uri $uri/ =404;
        }
        location / {
                root /var/www/tmp_frontend;
                try_files $uri $uri/ =404;
        }
}