User Tools

Site Tools


python:django-prod-mode

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
python:django-prod-mode [2024/10/06 09:59] – created kirtisinghpython:django-prod-mode [2024/10/06 10:09] (current) kirtisingh
Line 5: Line 5:
 Once you have tested you app in development mode, we can then undertake following steps to run our Django app in production mode Once you have tested you app in development mode, we can then undertake following steps to run our Django app in production mode
  
-**Step 1 :** Log into N99panel. From under N99panel dashboard, click 'List Users'+**Step 1 :** Log into N99panel and click on 'Python'
  
-{{:gen:list-users.png?direct&400|}} +{{:python:screenshot_capture_-_2024-10-01_-_06-05-15.png?direct&400|}} 
-\\+
  
-**Step 2 :** You will see the list of all the users. Please click 'Manage SSHD/CronD' link against the appropriate user 
  
-{{:gen:click-manage-sshd.png?direct&400|}} +**Step 2 :** Now click on 'Python Apps (production mode)'
-\\+
  
-**Step 3 :** Now choose 'Full SSHD mode with Python'. With this you get the maximum command set which includes **python3** and **pip3** binaries along with git and other useful command set. +{{:python:pythin-prod-s2.png?direct&400|}}
- +
-{{:python:screenshot_capture_-_2024-10-01_-_22-06-14.png?direct&600|}} +
-\\ +
-When you click 'Submit', upon successful job execution, the SSHD daemon would restart, and its host signature would change from what it was earlier. This implies that when you try to connect via SSH, you will get a warning for the same, and that you need to appropriately handle at your end. +
- +
-**Step 4 :** SSH using to the VPS using the above user's credentials +
- +
-**Step 5 :** Change over to directory/folder //python_apps// . If it is not created, please create it manually. //python_apps// folder is also used when you have to later launch your app in production mode +
- +
-Assuming you are /home///user// folder, please run the below command to create python_apps folder, if it does not exist. +
- +
-<code> +
-mkdir python_apps +
-</code> +
- +
-Now change into python_apps folder +
- +
-<code> +
-cd python_apps +
-</code> +
- +
-**Step 6 :** From within this folder add/create your python project as you would normally do. Below we illustrate with specific examples for Flask, Django and Generic modes +
- +
-**Flask** +
- +
-<code> +
-mkdir flask-project +
-cd flask-project +
-python3 -m venv .venv +
-source .venv/bin/activate +
-</code> +
- +
-Running the last command with activate the virtual environment, proceeding further +
- +
-<code> +
-pip install flask +
-pip install gunicorn +
-</code> +
- +
-You you can upload/write your Flask app and fire it up using gunicorn, but here I will showcase an example using the hello world app - refer https://flask.palletsprojects.com/en/2.3.x/quickstart/  +
- +
-Below code is saved to hello.py +
- +
-<code> +
-from flask import Flask +
- +
-app = Flask(__name__) +
- +
-@app.route("/"+
-def hello_world(): +
-    return "<p>Hello, World!</p>" +
-</code> +
- +
-Now to fire our flash app, we use gunicorn  +
- +
-<code> +
-gunicorn -b 0.0.0.0:8000 -w 4 --forwarded-allow-ips='172.16.26.1' hello:app +
-</code> +
- +
-To run it in background, we append & to the above command +
- +
-<code> +
-gunicorn -b 0.0.0.0:8000 -w 4 --forwarded-allow-ips='172.16.26.1' hello:app & +
-</code> +
- +
-To exit virtual environment use the command +
-<code> +
-deactivate +
-</code> +
- +
-**Django** +
- +
-<code> +
-mkdir django-project +
-cd django-project +
-python3 -m venv .venv +
-source .venv/bin/activate +
-</code> +
- +
-Running the last command with activate the virtual environment, proceeding further +
- +
-<code> +
-pip install django +
-pip install gunicorn +
-</code> +
- +
-Here we can refer to https://docs.djangoproject.com/en/5.1/intro/tutorial01/ to run a hello world Django application +
- +
-<code> +
-django-admin startproject mysite +
-cd mysite +
-</code> +
- +
-When you change into //mysite// folder, it contains //manage.py// along with a //mysite// child folder +
- +
-W.r.t. Django, please refer to these important links \\ +
-* https://docs.djangoproject.com/en/1.10/ref/settings/#allowed-hosts - Please add your VHost name as mentioned in the linked article \\ +
-* https://docs.djangoproject.com/en/3.0/ref/settings/#secure-proxy-ssl-header - Tt concerns the prefixing of http or https to your URLs/Assets +
- +
-Now to fire our Django app, we use gunicorn  +
- +
-<code> +
-gunicorn -b 0.0.0.0:8000 -w 4 --forwarded-allow-ips='172.16.26.1' mysite.wsgi +
-</code> +
- +
-To run it in background, we append & to the above command +
- +
-<code> +
-gunicorn -b 0.0.0.0:8000 -w 4 --forwarded-allow-ips='172.16.26.1' mysite.wsgi & +
-</code> +
- +
- +
-To exit virtual environment use the command +
-<code> +
-deactivate +
-</code> +
- +
- +
-**Generic Python App** +
- +
-<code> +
-mkdir generic-project +
-cd geneic-project +
-python3 -m venv .venv +
-source .venv/bin/activate +
-</code> +
- +
-Running the last command with activate the virtual environment, proceeding further +
- +
-<code> +
-pip install gunicorn +
-</code> +
- +
-Here is a small hello world example of our generic app +
- +
-Below code is saved to hello.py +
- +
-<code> +
-def app(environ, start_response): +
-    data = b"Hello, World!\n" +
-    start_response("200 OK", [ +
-        ("Content-Type", "text/plain"), +
-        ("Content-Length", str(len(data))) +
-    ]) +
-    return iter([data]) +
-</code> +
- +
-Now to fire our flash app, we use gunicorn  +
- +
-<code> +
-gunicorn -b 0.0.0.0:8000 -w 4 --forwarded-allow-ips='172.16.26.1' hello:app +
-</code> +
- +
-To run it in background, we append & to the above command +
- +
-<code> +
-gunicorn -b 0.0.0.0:8000 -w 4 --forwarded-allow-ips='172.16.26.1' hello:app & +
-</code> +
- +
-To exit virtual environment use the command +
-<code> +
-deactivate +
-</code> +
-\\ +
-**With this our SSH Console aspect of our python project is covered. Now we will link our app to our VHost/Website URL** +
- +
- +
-**Step 6 :** Log into N99panel and click on 'Python' +
- +
-{{:python:screenshot_capture_-_2024-10-01_-_06-05-15.png?direct&400|}} +
  
  
-**Step :** Now click on 'Python Apps via SSH'+**Step :** Now further click on 'Add a Python App'
  
-{{:python:screenshot_capture_-_2024-10-01_-_06-06-40.png?direct&400|}}+{{:python:pythin-prod-s3.png?direct&400|}}
  
-**Step 8 :** Now further click on 'Map a Python Dev Port' 
  
-{{:python:screenshot_capture_-_2024-10-01_-_06-08-21.png?direct&400|}}+**Step 4 :** Then enter details of your Django App as explained below
  
-**Step 9 :** Fill the form with the requisite details+{{:python:django-app.png?direct&800|}}
  
-{{:python:screenshot_capture_-_2024-10-01_-_06-33-16.png?direct&600|}} 
  
-With these above mentioned steps, you can showcase your python apps to the world very easily.+With these above mentioned steps, you can run your Django app in production mode.
  
python/django-prod-mode.1728208791.txt.gz · Last modified: 2024/10/06 09:59 by kirtisingh

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki