Saturday, 26 November 2022

Django rest framework Project tutorials part final deploy

environment 

python -m venv projectenv

activate environment

pip install -r requirements.txt

.env files


MySQL

remotely connect 

mysql configuration changes

Services 

/etc/systemd/system/service_name.services

gunicorn  project_name.wsgi


reverse proxy

nginx 

configuration

localhost:8000

1271.0.0.1:8000

http->80

https ->443


8000->80

9000->80/443

443-> ssl certificate 

rverse_proxy:127.0.0.1:8000

listen:80

certificate  /etc/mm,vgvc...

Installing mysql

1.sudo apt update

2.sudo apt install mysql-server

3.sudo systemctl start mysql.service

4.sudo systemctl statusmysql.service

5.CREATE USER 'my_db_user'@'%' IDENTIFIED BY '7882#BB8B$FC!D327C30$8DD0E7E1750B*';

6.CREATE DATABASE my_db_nameCHARACTER SET utf8 COLLATE utf8_general_ci;

7.FLUSH PRIVILEGES;

8.sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

change to 

bind-address            = 0.0.0.0

change password if needed
ALTER USER 'learning_user'@'%' IDENTIFIED BY '73BE53ED2#BB8B$FC!D327C30$8DD0E7E1750B';

install nginx

configuration accordingly
/etc/nginx/sites-available

sudo apt update
sudo apt install nginx


server{

         server_name 109.69.10.29 mydomain.com;
        listen 80;

        location /static {
        root /home/apps/project_name;
        }
        location /media {
        alias /home/apps/project_name/media/;
        }
        access_log /home/apps/project_name/logs/nginx-access.log;
        error_log /home/apps/project_name/logs/nginx-error.log;


        location / {
                 proxy_set_header Host $http_host;
            proxy_pass http://localhost:9000;
                proxy_redirect off;

       }

         location /apis {
                 proxy_set_header Host $http_host;
            proxy_pass http://localhost:9000;
                proxy_redirect off;

       }


}


add this line to nginx.conf inside http{}

if file uploading get this error

413 Request Entity Too Large


nginx/1.22.0 (Ubuntu)
client_max_body_size 500M;
ln -s /etc/nginx/sites-available/project_name.conf /etc/nginx/sites-enabled/
sudo nginx -s relaod
sudo service nginx restart
sudo service nginx status

creating service
/etc/systemd/system/project_name.service

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

[Service]
Type=notify
# the specific user that our service will run as
# another option for an even more restricted service is
#DynamicUser=yes
User=root
Group=root
# see http://0pointer.net/blog/dynamic-users-with-systemd.html
RuntimeDirectory=gunicorn
WorkingDirectory=/home/apps/project_name
ExecStart=/home/apps/project_name/projectenv/bin/gunicorn project_name.wsgi --bind 0.0.0.0:8000 --timeout 120
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=mixed
TimeoutStopSec=5
PrivateTmp=true

[Install]
WantedBy=multi-user.target

php
 configuration
nano /etc/php/8.1/fpm/pool.d/www.conf

;listen = /run/php/php8.1-fpm.sock
listen = 127.0.0.1:9000
;security.limit_extensions = .php .php3 .php4 .php5 .php7
security.limit_extensions = .php .html

-----------------

php nginx

nginx.conf

---------------

     fastcgi_buffers 8 16k;

    fastcgi_buffer_size 32k;

    fastcgi_connect_timeout 90;

    fastcgi_send_timeout 90;

    fastcgi_read_timeout 90;

-----------------

domain.conf

location / {

        root /home/apps/front;

        try_files $uri $uri/ /index.php;

        #index index.php index.html;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_pass  127.0.0.1:9000;

        include fastcgi_params;

        fastcgi_index index.php;

        #fastcgi_param SCRIPT_FILENAME `$document_root/service/public$fastcgi_script_name`;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;


}


  # deny access to Apache .htaccess on Nginx with PHP,

  # if Apache and Nginx document roots concur

  location ~ /\.ht {

    deny all;

  }