Django Rest API for the Authentication. Nearby Mobile Application is using it.
Install Requirements
pip3 install -r requirements.txt
Some sort of problem during hosting it on Heroku
Problem Number - 1
! Error while running '$ python manage.py collectstatic --noinput'.
See traceback above for details.
You may need to update application code to resolve this error.
Or, you can disable collectstatic for this application:
$ heroku config:set DISABLE_COLLECTSTATIC=1
https://devcenter.heroku.com/articles/django-assets
! Push rejected, failed to compile Python app.
! Push failed
Solution
Django won’t automatically create the target directory (STATIC_ROOT) that collectstatic uses, if it isn’t available. You may need to create this directory in your codebase, so it will be available when collectstatic is run. Git does not support empty file directories, so you will have to create a file inside that directory as well. So this is a file to complete rule.
Add this Line into settings.py > MIDDLEWARE
'whitenoise.middleware.WhiteNoiseMiddleware',
Add this Line into settings.py (Buttom Line)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Heroku Doesn't have Static File. So we need to create static folder inside your project folder. make sure that folder have atleast one file
Problem Number - 2
Solution
We don't have http server to run this instance so we need gunicorn.
Add Procfile at root && write the below code
release: python manage.py makemigrations --no-input
release: python manage.py migrate --no-input
# wsgi from settings.py
web: gunicorn nearbyauth.wsgi
Step : 1
change allowed host in settings.py
ALLOWED_HOSTS = ['*']
Step : 2
# update machine
sudo apt-get update
sudo apt-get upgrade -y
# configuration for project
sudo apt-get install python3-pip
sudo apt-get install python3-venv
# create virtual enviroment and activate it
python3 -m venv env
source env/bin/activate
Step : 3
install all dependencies of your project
git clone <https://github.com/PushpanshuRanjanSingh/Nearby-Auth.git>
cd Nearby-Auth
python -m pip install -r requirements.txt
Step : 4
pip install gunicorn
sudo apt-get install supervisor
sudo apt-get install -y nginx
# edit inbound in aws : add http:80 source=> anywhere
Step : 5
test your project that it is working or not
# you can check your wsgi
$ cat settings.py | grep wsgi
# output : WSGI_APPLICATION = 'NearBYAuth.wsgi.application'
gunicorn --bind 0.0.0.0:8000 NearBYAuth.wsgi:application
# check your aws ip in browser along with port : http://111.11.11.111:8000
Step : 6
Configure Supervisor
$ cd /etc/supervisor/conf.d
$ sudo nano gunicorn.conf
# write in conf file according to you path & name
[program:gunicorn]
directory=/home/ubuntu/Nearby-Auth
command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/Nearby-Auth/app.sock NearBYAuth.wsgi:application
autostart=true
autorestart=true
stderr_logfile=/var/log/gunicorn/gunicorn.err.log
stdout_logfile=/var/log/gunicorn/gunicorn.out.log
[group:guni]
programs:gunicorn
$ mkdir /var/log/gunicorn
$ sudo supervisorctl reread
$ sudo supervisorctl update
# checkout gunicorn is running or not
$ sudo supervisorctl status
Step : 7
configure nginx configuration
cd /etc/nginx/sites-available
sudo touch django.conf
Write this code to django.conf
server{
listen 80;
server_name ec2-15-206-117-147.ap-south-1.compute.amazonaws.com 15.206.117.147;
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/Nearby-Auth/app.sock;
}
}
Check & Test
sudo nginx -t
#output
#nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
#nginx: configuration file /etc/nginx/nginx.conf test is successful
Now enable it
sudo ln django.conf /etc/nginx/sites-enabled/
Note : You may get error: could not build server_names_hash, you should increase server_names_hash_bucket_size: 64 so ..
sudo nano /etc/nginx/nginx.conf
#change this line
server_names_hash_bucket_size 512;
sudo service nginx restart
Requirements & process
Process Step :
- Associate Elastic IP
- Create Route 53
- Request Certificate Manger
- Create Load Balancer
-
Elastic IP
- Associate with Instance
-
Certificate Manager
- Request a public certificate
- Add domain Names
- DNS Validation
- Review & Request
- Add CNAME to Route 53
-
Load Balancer
- Classic Load Balancer
- Load Balancer Protocol Choose
- http
- https
- Select security group
- select those which one is associated with instance
- Configure Health Check
- ping path - '/'
- Connect EC2 instance -> Review
- Select Load Balancer
- Description: Migrate now
- Listener: Select HTTP : 80 > edit rules > THEN > edit > delete action & Select Redirect to
-
Route 53
- Route 53 create hosted zone
- [NS] default provide nameserver -> add 4 nameserver into your domin panel
- [A] add load balancer as alias [yes]
- [CNAME] add ssl certificate dns config
- [SOA] default provided
- Route 53 create hosted zone