From b18f43a48bfc6c8e1098910892f02bad417a5e9b Mon Sep 17 00:00:00 2001 From: Alexander Laevens Date: Sun, 3 Mar 2024 20:35:39 -0700 Subject: [PATCH] Tweaks for old server. Ready for re-deployment --- one_trip_api/.dockerignore | 3 +- one_trip_api/Dockerfile | 2 +- one_trip_api/Server deployment.txt | 11 ++++++ one_trip_api/docker-compose.yaml | 8 +++- one_trip_api/entrypoint.sh | 4 ++ one_trip_api/nginx.conf | 59 ++++++++++++++++++++++++++++++ one_trip_api/one_trip_api/urls.py | 2 - one_trip_api/requirements.txt | 10 ++++- 8 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 one_trip_api/Server deployment.txt create mode 100644 one_trip_api/nginx.conf diff --git a/one_trip_api/.dockerignore b/one_trip_api/.dockerignore index 7fcb023..e72c386 100644 --- a/one_trip_api/.dockerignore +++ b/one_trip_api/.dockerignore @@ -3,4 +3,5 @@ **/prod_data **/venv **/__pycache__ -**/db.sqlite3 \ No newline at end of file +**/db.sqlite3 +**/nginx.conf \ No newline at end of file diff --git a/one_trip_api/Dockerfile b/one_trip_api/Dockerfile index 459badd..8a1c196 100644 --- a/one_trip_api/Dockerfile +++ b/one_trip_api/Dockerfile @@ -22,7 +22,7 @@ RUN mkdir -p ${DATA_DIR} WORKDIR ${APP_DIR} RUN apt-get update -RUN apt-get install --yes --no-install-recommends nano curl +RUN apt-get install --yes --no-install-recommends wget # Build pip requirements ADD ./requirements.txt . diff --git a/one_trip_api/Server deployment.txt b/one_trip_api/Server deployment.txt new file mode 100644 index 0000000..d64cb2c --- /dev/null +++ b/one_trip_api/Server deployment.txt @@ -0,0 +1,11 @@ +1. sudo useradd --system --shell /bin/bash groceries +2. sudo usermod -aG www-data groceries +3. id -u groceries +4. id -g www-data +5. edit docker-compose.yaml to have correct UID / GID +6. docker compose build +7. sudo mkdir -p /var/www/groceries.alaevens.ca/data +8. sudo cp EXISTING_DB_FILE /var/www/groceries.alaevens.ca/db.sqlite3 +9. sudo chown -R groceries /var/www/groceries.alaevens.ca/ +10. sudo chgrp -R www-data /var/www/groceries.alaevens.ca/ +11. docker compose up -d diff --git a/one_trip_api/docker-compose.yaml b/one_trip_api/docker-compose.yaml index 0c8c949..cb493ad 100644 --- a/one_trip_api/docker-compose.yaml +++ b/one_trip_api/docker-compose.yaml @@ -1,12 +1,16 @@ services: groceries: + container_name: groceries-api build: context: . args: - - UID=999 + - UID=992 - GID=33 ports: - 8001:8080 + stdin_open: true + tty: true + restart: always volumes: - /var/www/groceries.alaevens.ca/data:/home/groceries/data:rw - type: bind @@ -14,7 +18,9 @@ services: target: /home/groceries/web/db.sqlite3 redis: + container_name: groceries-ws-cache image: "redis:alpine" + restart: always ports: - 6379:6379 diff --git a/one_trip_api/entrypoint.sh b/one_trip_api/entrypoint.sh index e0462d4..4327c4f 100644 --- a/one_trip_api/entrypoint.sh +++ b/one_trip_api/entrypoint.sh @@ -1,5 +1,9 @@ #! /bin/bash +printf "Make migrations:\n" python3 manage.py makemigrations +printf "\n\nMigrate:\n" python3 manage.py migrate +printf "\n\nCollect static:\n" python3 manage.py collectstatic --no-input +printf "\n\nStart ASGI server:\n" gunicorn one_trip_api.asgi:application -b 0.0.0.0:8080 --access-logfile - -k uvicorn.workers.UvicornWorker \ No newline at end of file diff --git a/one_trip_api/nginx.conf b/one_trip_api/nginx.conf new file mode 100644 index 0000000..58227c8 --- /dev/null +++ b/one_trip_api/nginx.conf @@ -0,0 +1,59 @@ +server { + server_name groceries.alaevens.ca; + + location / { + proxy_set_header Host $http_host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + proxy_redirect off; + proxy_buffering off; + proxy_pass http://container; + } + + location /downloads/ { + autoindex on; + alias /var/www/groceries.alaevens.ca/downloads/; + } + + location /static/ { + alias /var/www/groceries.alaevens.ca/data/static/; + } + + location /media/ { + alias /var/www/groceries.alaevens.ca/data/media/; + } + + location /app/ { + alias /var/www/groceries.alaevens.ca/web_app/; + index index.html; + } + + listen [::]:443 ssl; + listen 443 ssl; # managed by Certbot + ssl_certificate /etc/letsencrypt/live/alaevens.ca/fullchain.pem; # managed by Certbot + ssl_certificate_key /etc/letsencrypt/live/alaevens.ca/privkey.pem; # managed by Certbot + include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot + +} + +server { + if ($host = groceries.alaevens.ca) { + return 301 https://$host$request_uri; + } # managed by Certbot + + + server_name groceries.alaevens.ca; + + listen [::]:80; + listen 80; + return 404; # managed by Certbot + + +} + +upstream container { + server 127.0.0.1:8001; +} diff --git a/one_trip_api/one_trip_api/urls.py b/one_trip_api/one_trip_api/urls.py index aad2ae8..2eb24fd 100644 --- a/one_trip_api/one_trip_api/urls.py +++ b/one_trip_api/one_trip_api/urls.py @@ -28,5 +28,3 @@ urlpatterns = [ urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += staticfiles_urlpatterns() - -print("URLPATTERNS:", urlpatterns) diff --git a/one_trip_api/requirements.txt b/one_trip_api/requirements.txt index 5d2faa8..1a3c3ae 100644 --- a/one_trip_api/requirements.txt +++ b/one_trip_api/requirements.txt @@ -1,3 +1,4 @@ +anyio==4.3.0 asgiref==3.7.2 async-timeout==4.0.2 certifi==2022.9.24 @@ -13,6 +14,7 @@ djangorestframework==3.14.0 docopt==0.6.2 gunicorn==21.2.0 h11==0.14.0 +httptools==0.6.1 idna==3.6 Markdown==3.4.1 msgpack==1.0.4 @@ -20,12 +22,18 @@ packaging==21.3 Pillow==9.3.0 pipreqs==0.4.11 pyparsing==3.0.9 +python-dotenv==1.0.1 python-monkey-business==1.0.0 pytz==2022.6 +PyYAML==6.0.1 redis==4.3.5 requests==2.28.1 six==1.16.0 +sniffio==1.3.1 sqlparse==0.4.3 urllib3==1.26.13 uvicorn==0.27.1 -yarg==0.1.9 +uvloop==0.19.0 +watchfiles==0.21.0 +websockets==12.0 +yarg==0.1.9 \ No newline at end of file