You are on page 1of 2

Lesson-14 Instructions

(Django web server - Installation)


1)
##install apache2 and mod-wsgi
### Open a terminal to your cloud server and run the following command

sudo apt-get install apache2 apache2-utils ssl-cert libapache2-mod-wsgi-py3

2) Create virtual environment:

## Run the following commands to install virtual environmemt

pip3 install virtualenv


pip3 install virtualenvwrapper

### then edit the file ~/.bashrc and add the following:

export WORKON_HOME=~/.virtualenvs
export MY_PROJECT=~/my_proj
export VIRTUALENVWRAPPER_WORKON_CD=1
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/home/raman/.local/bin/virtualenv
source /home/raman/.local/bin/virtualenvwrapper.sh

### Note : in the above lines, replace <your home directory> with your actual home directory.
### in my case it was /home/raman
### Then, run the following command:

source ~/.bashrc

### Then create a virtual environment by running the following command.


### Here, django is the name of the virtual environment
mkvirtualenv django

### then activate the virtual environment by running the following command
workon django

### We are in virtual environment now

### install paho-mqtt in the django virtual environment by running the following command
pip install paho-mqtt

##install django by running the following command


pip install django

###Create a django project by running the following command


django-admin.py startproject my_proj

### edit the file settings.py

cd ~/my_proj/my_proj
vi settings.py

## add the following at the top (Or, make it as shown in settings.py-initial) in the attachment of
this lesson

import os

##And, modify the ALLOWED_HOSTS line as shown below:


## Note: iotcourse.xyz is my domain name. You may replace it with your domain name
## 35.223.145.82 is my server's ip address. You may replace it with your server's ip address

ALLOWED_HOSTS = ["127.0.0.1", "35.223.145.82", 'iotcourse.xyz', 'http://iotcourse.xyz',


'https://iotcourse.xyz', 'www.iotcourse.xyz', 'https://iotcourse.xyz', 'https://www.iotcourse.xyz',]

##And, add the following just after STATIC_URL at the bottom


STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

## Then save the settings.py


## This settings.py will look similar to settings.py-initial in the resources of Lesson-14,
### except for the ALLOWED_HOSTS. If you are copying settings.py-initial, make sure
### to change the ip address and domain name with yours.

### now, migrate by running the following commands

cd ~/my_proj
python manage.py makemigrations
python manage.py migrate

### Create superuser by running the following command

python manage.py createsuperuser

### collect static files by running the following command


python manage.py collectstatic

## run the following command and see if you see any errors
python manage.py runserver 0:8000
## If everything is correct, this will not show any errors.
## Hit ^C to get out of the above command

### And, close your terminal by running the following command


exit

You might also like