From ffaaa405ce540cfdbb14c9bf467af9b36e692a81 Mon Sep 17 00:00:00 2001 From: James Krepelka Date: Tue, 14 Jun 2022 12:43:36 -0700 Subject: [PATCH 1/4] Enabling HTTPS for Menta BackEnd --- Menta/Menta/settings.py | 15 +++++++++++++-- Menta/config/gunicorn/dev.py | 20 ++++++++++++++++++++ Menta/config/gunicorn/prod.py | 19 +++++++++++++++++++ requirements.txt | 1 + 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 Menta/config/gunicorn/dev.py create mode 100644 Menta/config/gunicorn/prod.py diff --git a/Menta/Menta/settings.py b/Menta/Menta/settings.py index 73e0cbd..0fc6977 100644 --- a/Menta/Menta/settings.py +++ b/Menta/Menta/settings.py @@ -23,9 +23,11 @@ SECRET_KEY = 'django-insecure-06=-!hy)#3g&$-i!_)x%3ak^p(b(nyg@_mvwjx5qk4l1m_@8h)' # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = False -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = [ + "34.132.172.130" +] # Application definition @@ -38,6 +40,7 @@ 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', + 'corsheaders', #myApps 'Profiles', ] @@ -45,6 +48,7 @@ MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', + 'corsheaders.middleware.CorsMiddleware' 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', @@ -124,3 +128,10 @@ # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +CORS_ALLOW_ALL_ORIGINS = True + +SECURE_HSTS_SECONDS = 30 +SECURE_HSTS_PRELOAD = True +SECURE_HSTS_INCLUDE_SUBDOMAINS = True +SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") diff --git a/Menta/config/gunicorn/dev.py b/Menta/config/gunicorn/dev.py new file mode 100644 index 0000000..7cbfe60 --- /dev/null +++ b/Menta/config/gunicorn/dev.py @@ -0,0 +1,20 @@ +"""Gunicorn *development* config file""" + +# Django WSGI application path in pattern MODULE_NAME:VARIABLE_NAME +wsgi_app = "Menta.wsgi:application" +# The granularity of Error log outputs +loglevel = "debug" +# The number of worker processes for handling requests +workers = 2 +# The socket to bind +bind = "0.0.0.0:80" +# Restart workers when code changes (development only!) +reload = True +# Write access and error info to /var/log +accesslog = errorlog = "/var/log/gunicorn/dev.log" +# Redirect stdout/stderr to log file +capture_output = True +# PID file so you can easily fetch process ID +pidfile = "/var/run/gunicorn/dev.pid" +# Daemonize the Gunicorn process (detach & enter background) +daemon = True \ No newline at end of file diff --git a/Menta/config/gunicorn/prod.py b/Menta/config/gunicorn/prod.py new file mode 100644 index 0000000..dd380ff --- /dev/null +++ b/Menta/config/gunicorn/prod.py @@ -0,0 +1,19 @@ +"""Gunicorn *prod* config file""" + +import multiprocessing + +# Django WSGI application path in pattern MODULE_NAME:VARIABLE_NAME +wsgi_app = "Menta.wsgi:application" +# The number of worker processes for handling requests +workers = multiprocessing.cpu_count() * 2 + 1 +# The socket to bind +bind = "0.0.0.0:443" +# Write access and error info to /var/log +accesslog = "/var/log/gunicorn/access.log" +errorlog = "/var/log/gunicorn/error.log" +# Redirect stdout/stderr to log file +capture_output = True +# PID file so you can easily fetch process ID +pidfile = "/var/run/gunicorn/prod.pid" +# Daemonize the Gunicorn process (detach & enter background) +daemon = True \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 6f1c1f3..ba094ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ djangorestframework +django-cors-headers \ No newline at end of file From 1058d5d55877d25755ebf3e4422737f005ec5525 Mon Sep 17 00:00:00 2001 From: James Krepelka Date: Sun, 26 Jun 2022 19:10:02 -0700 Subject: [PATCH 2/4] Create folder from working setup on server --- .gitignore | 2 + README.md | 134 +++++++++++++++++++++++++++++++++++++++++- api/admin/__init__.py | 0 api/admin/admin.py | 2 + api/menta.py | 12 ++++ api/requirements.txt | 3 + api/user/__init__.py | 0 api/user/user.py | 2 + api/wsgi.py | 4 ++ 9 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 api/admin/__init__.py create mode 100644 api/admin/admin.py create mode 100644 api/menta.py create mode 100644 api/requirements.txt create mode 100644 api/user/__init__.py create mode 100644 api/user/user.py create mode 100644 api/wsgi.py diff --git a/.gitignore b/.gitignore index b72833f..5e808c4 100644 --- a/.gitignore +++ b/.gitignore @@ -106,6 +106,8 @@ celerybeat.pid .venv env/ venv/ +mentaenv/ +mentavenv/ ENV/ env.bak/ venv.bak/ diff --git a/README.md b/README.md index c17ffa0..3619b0f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,37 @@ # Menta A Mentor/ Mentee Matchmaking App +# Development + +## Running Locally + +### API + +Clone the repo + +Configure and setup the venv +```shell +cd Menta/api +python -m venv mentavenv +source mentavenv/bin/activate +# OR: source mentavenv/Scripts/activate +pip install -r reaquirements.txt +``` + +Start the server +```shell +python menta.py +``` + +Check it out on your browser. + +http://127.0.0.1:5000/ + +To get out of venv +```shell +deactivate +``` + # Deployment ## Frontend @@ -32,4 +63,105 @@ Ask @Jooms to do the following ## Backend -TODO(Jooms): Fix backend deployment and record instructions here. \ No newline at end of file +TODO(Jooms): Fix backend deployment and record instructions here. + +Start with ubuntu 22.04 LTS + +### One-Time +#### Prep Machine +```shell +sudo apt update +sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools +sudo apt install python3-venv +``` + +#### Prep Project +```shell +cd ~ +git clone [menta https url] +cd ~/Menta/api +python3 -m venv mentavenv +source mentavenv/bin/activate +pip install -r requirements.txt +deactivate +``` + +#### Create and Start Gunicorn Service +```shell +sudo vim /etc/systemd/system/menta.service +``` +Add the following contents: +```text +[Unit] +Description=Gunicorn instance to serve menta +After=network.target + +[Service] +User=jameskrepelka +Group=www-data +WorkingDirectory=/home/jameskrepelka/menta +Environment="PATH=/home/jameskrepelka/menta/mentavenv/bin" +ExecStart=/home/jameskrepelka/menta/mentavenv/bin/gunicorn --workers 3 --bind unix:menta.sock -m 007 wsgi:app + +[Install] +WantedBy=multi-user.target +``` +Then: +```shell +sudo systemctl start myproject +sudo systemctl enable myproject +``` +And confirm it works with +```shell +sudo systemctl status +``` + +#### Configure Nginx + +```shell +sudo vim /etc/nginx/sites-available/menta +``` +Add the following contents: +```text +server { + listen 80; + server_name api.menta.dev; + + location / { + include proxy_params; + proxy_pass http://unix:/home/jameskrepelka/menta/menta.sock; + } +} +``` +Then enable the site. +```shell +sudo ln -s /etc/nginx/sites-available/menta /etc/nginx/sites-enabled +``` +Ensure there aren't any other `default` sites enabled. +```shell +ls /etc/nginx/sites-enabled +``` +Ensure nginx is happy with the config. +```shell +sudo nginx -t +``` +Restart nginx. +```shell +sudo systemctl restart nginx +``` +Open the Firewall +```shell +sudo ufw allow 'Nginx Full' +``` +Share the home dir with nginx +```shell +sudo chmod 755 /home/jameskrepelka +``` + +### Every-Time + +```shell +cd ~/Menta/api +git pull +sudo systemctl restart menta +``` \ No newline at end of file diff --git a/api/admin/__init__.py b/api/admin/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/admin/admin.py b/api/admin/admin.py new file mode 100644 index 0000000..9c76053 --- /dev/null +++ b/api/admin/admin.py @@ -0,0 +1,2 @@ +def admin_hello(): + return "

Hello Admin!

" diff --git a/api/menta.py b/api/menta.py new file mode 100644 index 0000000..7f85409 --- /dev/null +++ b/api/menta.py @@ -0,0 +1,12 @@ +from flask import Flask +from user import user + +def hello(): + return "

Hello There!

" + +if __name__ == "__main__": + app = Flask(__name__) + app.route("/")(hello) + app.route("/user_hello")(user.user_hello) + + app.run(host='0.0.0.0') \ No newline at end of file diff --git a/api/requirements.txt b/api/requirements.txt new file mode 100644 index 0000000..900a979 --- /dev/null +++ b/api/requirements.txt @@ -0,0 +1,3 @@ +wheel +gunicorn +flask \ No newline at end of file diff --git a/api/user/__init__.py b/api/user/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/user/user.py b/api/user/user.py new file mode 100644 index 0000000..abb211c --- /dev/null +++ b/api/user/user.py @@ -0,0 +1,2 @@ +def user_hello(): + return "

Hello User!

" diff --git a/api/wsgi.py b/api/wsgi.py new file mode 100644 index 0000000..4d7c807 --- /dev/null +++ b/api/wsgi.py @@ -0,0 +1,4 @@ +from api import app + +if __name__ == "__main__": + app.run() \ No newline at end of file From 90e28fcba9ae28be0289d0f782e4164af842bf19 Mon Sep 17 00:00:00 2001 From: James Krepelka Date: Sun, 26 Jun 2022 19:11:26 -0700 Subject: [PATCH 3/4] Readme Tweaks --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3619b0f..370b0f7 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,7 @@ deactivate ## Frontend -IP: 34.122.108.133 -URL: https://www.menta.dev +**URL:** https://www.menta.dev To Deploy: @@ -63,11 +62,10 @@ Ask @Jooms to do the following ## Backend -TODO(Jooms): Fix backend deployment and record instructions here. +### One-Time -Start with ubuntu 22.04 LTS +Start with Ubuntu 22.04 LTS -### One-Time #### Prep Machine ```shell sudo apt update From b43d0cce7ec9f566b1cab038526ad1dd14b3831a Mon Sep 17 00:00:00 2001 From: James Krepelka Date: Sun, 26 Jun 2022 19:13:57 -0700 Subject: [PATCH 4/4] More Clean up --- Menta/config/gunicorn/dev.py | 20 -------------------- Menta/config/gunicorn/prod.py | 19 ------------------- api/menta.py | 2 +- api/wsgi.py | 2 +- 4 files changed, 2 insertions(+), 41 deletions(-) delete mode 100644 Menta/config/gunicorn/dev.py delete mode 100644 Menta/config/gunicorn/prod.py diff --git a/Menta/config/gunicorn/dev.py b/Menta/config/gunicorn/dev.py deleted file mode 100644 index 7cbfe60..0000000 --- a/Menta/config/gunicorn/dev.py +++ /dev/null @@ -1,20 +0,0 @@ -"""Gunicorn *development* config file""" - -# Django WSGI application path in pattern MODULE_NAME:VARIABLE_NAME -wsgi_app = "Menta.wsgi:application" -# The granularity of Error log outputs -loglevel = "debug" -# The number of worker processes for handling requests -workers = 2 -# The socket to bind -bind = "0.0.0.0:80" -# Restart workers when code changes (development only!) -reload = True -# Write access and error info to /var/log -accesslog = errorlog = "/var/log/gunicorn/dev.log" -# Redirect stdout/stderr to log file -capture_output = True -# PID file so you can easily fetch process ID -pidfile = "/var/run/gunicorn/dev.pid" -# Daemonize the Gunicorn process (detach & enter background) -daemon = True \ No newline at end of file diff --git a/Menta/config/gunicorn/prod.py b/Menta/config/gunicorn/prod.py deleted file mode 100644 index dd380ff..0000000 --- a/Menta/config/gunicorn/prod.py +++ /dev/null @@ -1,19 +0,0 @@ -"""Gunicorn *prod* config file""" - -import multiprocessing - -# Django WSGI application path in pattern MODULE_NAME:VARIABLE_NAME -wsgi_app = "Menta.wsgi:application" -# The number of worker processes for handling requests -workers = multiprocessing.cpu_count() * 2 + 1 -# The socket to bind -bind = "0.0.0.0:443" -# Write access and error info to /var/log -accesslog = "/var/log/gunicorn/access.log" -errorlog = "/var/log/gunicorn/error.log" -# Redirect stdout/stderr to log file -capture_output = True -# PID file so you can easily fetch process ID -pidfile = "/var/run/gunicorn/prod.pid" -# Daemonize the Gunicorn process (detach & enter background) -daemon = True \ No newline at end of file diff --git a/api/menta.py b/api/menta.py index 7f85409..1937465 100644 --- a/api/menta.py +++ b/api/menta.py @@ -9,4 +9,4 @@ def hello(): app.route("/")(hello) app.route("/user_hello")(user.user_hello) - app.run(host='0.0.0.0') \ No newline at end of file + app.run(host='0.0.0.0') diff --git a/api/wsgi.py b/api/wsgi.py index 4d7c807..bc52e9f 100644 --- a/api/wsgi.py +++ b/api/wsgi.py @@ -1,4 +1,4 @@ from api import app if __name__ == "__main__": - app.run() \ No newline at end of file + app.run()