# Setup Ngnix with Uwsgi on Oracle Linux ## 關閉 SELinux ```bash= sudo vim /etc/sysconfig/selinux ``` SELINUX=enforcing -> disabled ## 增加 EPEL 套件來源 > keyword: yum epel (oracle linux) ```bash= sudo vim /etc/yum.repos.d/oracle-epel-ol8.repo ``` set `enable` to `1` ## Django 前後端關係 ```graphviz digraph "Django前後端關係"{ NGINX -> "backend\nuWSGI"; "backend\nuWSGI" -> "python APP"; NGINX -> "frontend\nreact built static files"; NGINX -> "django static files\n(for admin panel)"; } ``` uWSGI 會開一個 socket 給 NGINX 使用,這樣兩者就可以互相溝通 uWSGI 會運作我們所撰寫的 python 檔案 ## Enable Ports for Web Service enable port 80 & 443 for http(s) ```bash= 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](https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/uwsgi/) for uwsgi.ini add service file for uWSGI so that it can be started automatically on boot. ```bash= sudo vim /etc/systemd/system/uwsgi.service ``` ```ini [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 ``` > 基本上就是改 `ExecStartPre` 跟 `ExecStart` systemctl commands: ```bash= # 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 ```bash= journalctl -xe ``` ## Install & Configure NGINX Install nginx by yum. ```bash= 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: ```bash= python manage.py collectstatic ``` then configure NGINX for url `STATIC_URL` to provide static files. ```bash= sudo vim /etc/nginx/conf.d/wtf.conf ``` 詳細的看最下面 ## Setup React Frontend ### Install NodeJS 14 ```bash= curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash - sudo yum install -y nodejs ``` generate production built ```bash= 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 ```bash= https://certbot.eff.org/lets-encrypt/otherpip-nginx ``` ## full nginx config ```conf 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; } } ```