How to Deploy a Django Rest API on Ubuntu

Posted by

How to Deploy a Django Rest API with Ubuntu

Deploy a Django Rest API with Ubuntu

Deploying a Django Rest API on Ubuntu server is a straightforward process that can be done in a few simple steps. Below are the steps to deploy a Django Rest API with Ubuntu:

Step 1: Install Python and Django

First, you need to make sure that Python and Django are installed on your Ubuntu server. You can install Python using the following command:

sudo apt-get install python3

Next, you can install Django using pip:

sudo apt-get install python3-pip
pip install Django

Step 2: Create a Django Project

Once Django is installed, you can create a new Django project by running the following command:

django-admin startproject myproject

Step 3: Create a Django App

Next, you can create a new Django app within your project by running the following command:

python manage.py startapp myapp

Step 4: Create a Django Rest API

Now, you can create a Django Rest API within your app by defining serializers, views, and urls as needed. Here is an example of a simple Django Rest API:


from rest_framework import serializers, viewsets
from .models import MyModel

class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = '__all__'

class MyModelViewSet(viewsets.ModelViewSet):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer

Step 5: Migrate and Runserver

After defining your API, you can migrate your database and run the Django development server using the following commands:

python manage.py makemigrations
python manage.py migrate
python manage.py runserver

Your Django Rest API should now be running on your Ubuntu server. You can access it by visiting http://localhost:8000 in your web browser.

Step 6: Configure Apache or Nginx

If you want to deploy your Django Rest API on a production server, you can configure Apache or Nginx to serve your API. Make sure to set up a WSGI file and configure your web server to point to your Django project.

Congratulations! You have successfully deployed a Django Rest API with Ubuntu. Happy coding!