Tuesday, November 5, 2024

Guide for Deploying a Django Web Application on Ubuntu Server

In this detailed tutorial, we’ll guide you through the process of deploying a Django web application on Ubuntu Server. By the end of this guide, you’ll be familiar with using popular web servers such as Nginx or Apache, setting up virtual environments, and managing dependencies.

Setting up Django Web Application on Ubuntu Server

Prerequisites for Deploying Django

Before we get started, make sure you have the following:

  1. A Django web application ready for deployment.
  2. An Ubuntu server instance (you can set this up through a cloud provider like AWS, Google Cloud, or DigitalOcean).
  3. Basic familiarity with the Linux command line.

Step 1: Install Python, pip, and virtualenv

Ubuntu Server usually comes with Python pre-installed. Check the installed version of Python with the following command:

python3 --version

If Python isn’t installed, use this command to install it:

sudo apt update
sudo apt install python3

Next, install pip which is a package manager for Python. Pip will allow us to install other Python packages.

sudo apt install python3-pip

Once pip is installed, we’ll install virtualenv. This tool lets us create isolated Python environments, which is useful when managing Python packages.

sudo pip3 install virtualenv

Step 2: Create a Virtual Environment

In your project’s directory, create a new virtual environment using the virtualenv command. Replace venv with your preferred environment name.

virtualenv venv

Activate the environment:

source venv/bin/activate

With your virtual environment active, you can install Django and other required dependencies.

Step 3: Clone Your Django Project and Install Dependencies

Use Git to clone your Django project into your Ubuntu Server:

git clone https://github.com/yourusername/yourproject.git

Go to the project directory:

cd yourproject

Install your project’s dependencies. These are usually listed in a file named requirements.txt:

pip install -r requirements.txt

Step 4: Install and Configure the Web Server (Nginx/Apache)

In this tutorial, we’ll show the setup process for both Nginx and Apache. Choose the one that suits your needs.

Nginx

Install Nginx with the following command:

sudo apt install nginx

Next, configure Nginx to reverse proxy to your Django application. Edit the default Nginx configuration file (/etc/nginx/sites-available/default), adding the following block:

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        include proxy_params;
        proxy_pass http://localhost:8000;
    }
}

Then, restart Nginx:

sudo systemctl restart nginx

Apache

Install Apache with the following command:

sudo apt install apache2 libapache2-mod-wsgi-py3

Next, configure Apache to serve your Django application. Create a new configuration file in /etc/apache2/sites-available/, named yourproject.conf, and add the following configuration:

<VirtualHost *:80>
    ServerName yourdomain.com

    WSGIDaemonProcess yourproject python-path=/path/to/yourproject:/path/to/venv/lib/python3.x/site-packages
    WSGIProcessGroup yourproject
    WSGIScriptAlias / /path/to/yourproject/yourproject/wsgi.py

    <Directory /path/to/yourproject/yourproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
</VirtualHost>

Then, enable the new site and restart Apache:

sudo a2ensite yourproject
sudo systemctl restart apache2

Step 5: Run Your Django Application

Before you run your Django app, ensure that all static files are collected in the static folder by running:

python manage.py collectstatic

Next, start your Django application:

python manage.py runserver 0.0.0.0:8000

At this point, your Django web application should be accessible via your server’s IP address or the domain you specified in your Nginx/Apache configuration.

Remember that this is a basic deployment tutorial. In a production environment, consider using a more robust WSGI server like Gunicorn or uWSGI, setting up a database server, and configuring HTTPS for secure connections.

Conclusion on Configuring Django on Ubuntu

Deploying a Django application on an Ubuntu Server is a multi-step process that involves creating a virtual environment, installing dependencies, and configuring a web server. With this guide, you should have a working deployment of your Django application. Happy coding!

Related Articles

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles