Setting up Django Python Web Environment on Oracle Cloud

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

Setting up a Django Python Environment on Oracle cloud can be little tricky as most documentations assume that the code is run on localhost, so thought let me put this on a blog article so it can help developers to easily run these environments on OCI

I would start with an assumption that an Ubuntu 18 Instance is already running on OCI , you need to ensure that port 8080 or 5000 is opened in Ingress Rule under Security List

No alt text provided for this image

the next thing of of many things 🙂 is to SSH into your Ubuntu environment and remove REJECT ALL firewall rule

 ubuntu@cblogs1:~$ sudo iptables --list --line-numbers Chain INPUT (policy ACCEPT) num  target     prot opt source               destination 1    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED 2    ACCEPT     icmp --  anywhere             anywhere 3    ACCEPT     all  --  anywhere             anywhere 4    ACCEPT     udp  --  anywhere             anywhere             udp spt:ntp 5    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh 6    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) num  target     prot opt source               destination 1    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) num  target     prot opt source               destination 1    BareMetalInstanceServices  all  --  anywhere             link-local/16 Chain BareMetalInstanceServices (1 references) num  target     prot opt source               destination 1    ACCEPT     tcp  --  anywhere             169.254.0.2           owner UID match root tcp dpt:iscsi-target /* See the Oracle-Provided Images  section in the Oracle Bare Metal documentation for security  impact of modifying or removing this rule */ 2    ACCEPT     tcp  --  anywhere             169.254.2.0/24        owner UID match root tcp dpt:iscsi-target /* See the Oracle-Provided Images  section in the Oracle Bare Metal documentation for security impact of  modifying or removing this rule */ .... removing this rule */ reject-with icmp-port-unreachable ubuntu@cblogs1:~$ sudo iptables -D INPUT 6 ubuntu@cblogs1:~$ sudo netfilter-persistent save run-parts: executing /usr/share/netfilter-persistent/plugins.d/15-ip4tables save run-parts: executing /usr/share/netfilter-persistent/plugins.d/25-ip6tables save ubuntu@cblogs1:~$ sudo netfilter-persistent reload run-parts: executing /usr/share/netfilter-persistent/plugins.d/15-ip4tables start run-parts: executing /usr/share/netfilter-persistent/plugins.d/25-ip6tables start

Install Django Python Environment

(venv) ubuntu@aiml:~/mypython$ python -m django --version
/home/ubuntu/venv/bin/python: No module named django
(venv) ubuntu@aiml:~/mypython$ pip install Django
Collecting Django
Downloading https://files.pythonhosted.org/packages/39/
b0/2138c31bf13e17afc32277239da53e9dfcce27bac8cb68cf1c0123f1fdf5
/Django-2.2.3-py3-none-any.whl (7.5MB)
|████████████████████████████████| 7.5MB 24.5MB/s
Collecting sqlparse (from Django)
Downloading https://files.pythonhosted.org/packages/ef/53/
900f7d2a54557c6a37886585a91336520e5539e3ae2423ff1102daf4f3a7/
sqlparse-0.3.0-py2.py3-none-any.whl
Collecting pytz (from Django)
Downloading https://files.pythonhosted.org/packages/3d/73/
fe30c2daaaa0713420d0382b16fbb761409f532c56bdcc514bf7b6262bb6/
pytz-2019.1-py2.py3-none-any.whl (510kB)
|████████████████████████████████| 512kB 45.6MB/s
Installing collected packages: sqlparse, pytz, Django
Successfully installed Django-2.2.3 pytz-2019.1 sqlparse-0.3.0
(venv) ubuntu@aiml:~/mypython$ import django
Command 'import' not found, but can be installed with:
sudo apt install imagemagick-6.q16
sudo apt install graphicsmagick-imagemagick-compat
sudo apt install imagemagick-6.q16hdri
(venv) ubuntu@aiml:~/mypython$ python -m django --version
2.2.3

Check for Django Installation

(venv) ubuntu@aiml:~/mypython$ import django
Command 'import' not found, but can be installed with:
sudo apt install imagemagick-6.q16
sudo apt install graphicsmagick-imagemagick-compat
sudo apt install imagemagick-6.q16hdri
(venv) ubuntu@aiml:~/mypython$ python -m django --version
2.2.3
(venv) ubuntu@aiml:~/mypython$ django-admin startproject mysite
(venv) ubuntu@aiml:~/mypython$ ls
pycache main.py mysite web.py
(venv) ubuntu@aiml:~/mypython$ cd mysite/
(venv) ubuntu@aiml:~/mypython/mysite$ ls
manage.py mysite
ubuntu@aiml:~/mypython/mysite$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial… OK
Applying auth.0001_initial… OK
Applying admin.0001_initial… OK
Applying admin.0002_logentry_remove_auto_add… OK
Applying admin.0003_logentry_add_action_flag_choices… OK
Applying contenttypes.0002_remove_content_type_name… OK
Applying auth.0002_alter_permission_name_max_length… OK
Applying auth.0003_alter_user_email_max_length… OK
Applying auth.0004_alter_user_username_opts… OK
Applying auth.0005_alter_user_last_login_null… OK
Applying auth.0006_require_contenttypes_0002… OK
Applying auth.0007_alter_validators_add_error_messages… OK
Applying auth.0008_alter_user_username_max_length… OK
Applying auth.0009_alter_user_last_name_max_length… OK
Applying auth.0010_alter_group_name_max_length… OK
Applying auth.0011_update_proxy_permissions… OK
Applying sessions.0001_initial… OK

You would then need to edit settings.py where ALLOWED_HOSTS = [‘*’], so that access from Public Cloud is Enabled.

(venv) ubuntu@aiml:~/mypython/mysite$ cd mysite/
(venv) ubuntu@aiml:~/mypython/mysite/mysite$ cat settings.py
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 2.2.3.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os
Build paths inside the project like this: os.path.join(BASE_DIR, …)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))
Quick-start development settings - unsuitable for production
See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '34u$l^ks4yl_#nj9uo&=&19y=gcv'
SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
ignore whatever below this …

the last thing of many things 🙂 is to run the Python Script at Server

(venv) ubuntu@aiml:~/mypython/mysite$ 
python manage.py runserver 0.0.0.0:8080
Watching for file changes with StatReloader
Performing system checks…
System check identified no issues (0 silenced).
July 21, 2019 - 16:34:10
Django version 2.2.3, using settings 'mysite.settings'
Starting development server at http://0.0.0.0:8080/
Quit the server with CONTROL-C.
[21/Jul/2019 16:34:16] "GET / HTTP/1.1" 200 16348
[21/Jul/2019 16:34:16] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[21/Jul/2019 16:34:17] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
Not Found: /favicon.ico
[21/Jul/2019 16:34:17] "GET /favicon.ico HTTP/1.1" 404 1978
[21/Jul/2019 16:34:17] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[21/Jul/2019 16:34:18] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
Not Found: /tmUnblock.cgi
[21/Jul/2019 16:34:51] "POST /tmUnblock.cgi HTTP/1.1" 404 1974

This should allow us to access to our Python default Web App directly from Cloud VM Public Ip Address, now in your Browser open http:// < VM Public IP Address > : 8080 , you should be able to see Django Python Web application default page.

No alt text provided for this image

Please like the Blog if you really liked this 🙂

Regards, Madhusudhan Rao ( Cloud Architect @ Oracle )

reference :

https://www.linkedin.com/pulse/setting-up-django-python-web-environment-oracle-cloud-madhusudhan-rao/

https://docs.djangoproject.com/en/2.2/topics/install/#installing-official-release

https://www.djangoproject.com/