Deploying a "Hello, World" Django app on Fedora 33
Requirement
1) Fedora server
2) NGINX web server
3) Django application framework
4) Gunicorn
A "Hello, World" application using Django
Django framework based upon Python. Current Python version is 3.7.
First check if python is installed. If not install it.
$ sudo dnf install python3
once Python is installed using pip3 django should be installed
$ pip3 install django~=3.1.2
Any version of django is good but it is personal preference.
next Gunicorn should be installed.
$ pip3 install gunicorn
We are almost ready for Django application development.
Soiling your hand
To start our development, we need to create a directory and put all our requirement there
$ mkdir myapp && cd myapp
I choose myapp but any thing is ok
$ django-admin startproject mapp
mapp is project name. Django will create some files for us and lessen our job. It will create
mapp directory and few files
.
└── mapp
├── manage.py
└── mapp
├── asgi.py
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
Next we create a small application to run on top of it.
$ cd mapp
$ django-admin startapp hello
.
├── hello
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── manage.py
└── mapp
├── asgi.py
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
now open the mapp/settings.py
$ vi mapp/settings.py
add following lines
INSTALLED_APPS = [
...
...
'hello',
]
next open views.py
$ vi hello/views.py
...
...
from django.http import HttpResponse
def homePageView(request):
return HttpResponse('Hello, World!')
create a file urls.py under hello and open it
$ vi hello/urls.py
add the following code to it.
from django.urls import path
from .views import homePageView
urlpatterns = [
path('', homePageView, name='home')
]
Update mapp/urls.py as bellow
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('hello.urls')),
]
Now test our app by running
$ python3 manage.py runserver
In browser, open http://127.0.0.1:8000
and enjoy your app
Ok you might have seen some warnings here. So please don't worry. Execute
$ python3 manage.py migrate
some messages will be displayed and now again execute
$ python3 manage.py runserver
This time there are no warnings, right? Ok good...
Deployment part
Open settings.py
$ vi mapp/settings.py
and change following
...
ALLOWED_HOSTS = [ '*' ]
...
...
STATIC_ROOT = '/var/www/static'
MEDIA_ROOT = '/var/www/media'
and make sure you created those directories
$ sudo mkdir /var/www/static
$ sudo mkdir /var/www/media
Next run
$ sudo python3 manage.py collectstatic
we are almost done.
Running guincorn
First copy mapp/wsgi.py as mapp/mapp.wsgi
$ cp mapp/wsgi.py mapp/mapp.wsgi
$ sudo gunicorn mapp.wsgi --workers=5 --bind=127.0.0.1:8888
now gunicorn is running. you can check with http://127.0.0.1:8888 and it will show you hello world page.
But we need nginx to serve for us on internet so we have to tell nginx to serve for us.
use following code as a .conf file in /etc/nginx/conf.d/ directory
we assume it as myapp.conf
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name 192.168.1.3; # substitute by your FQDN and machine's IP address
charset utf-8;
#Max upload size
client_max_body_size 75M; # adjust to taste
# Django static files. You should run 'django manage.py collectstatic'
location /static {
alias /var/www/static; # your Django project's media files
}
location /media {
alias /var/www/media;
}
# Finally, send all non-media requests to the Django server.
location / {
proxy_pass http://127.0.0.1:8888;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
restart nginx server by using
$ sudo systemctl restart nginx
now check from a remote machine using http://IP.address.of.server/
do you get it properly??? if not then check firewall settings on server. next check access on files etc...
goodluck.
No comments:
Post a Comment