How to configure Apache 2 to host Django websites ?

Author: neptune | 25th-May-2024
#Django

Configuring Apache 2 to host Django websites involves several steps. Let's walk through them:

Accessing the Server:

First, you need to access your server. You can do this using methods like Putty with a .ppk key, RSA key, or by connecting via SSH with the server's public IP address.

Installing Necessary Packages:

Once logged into your server, install the required packages:


    sudo apt install apache2

    sudo apt install python

    sudo apt install libapache2-mod-wsgi-py3

    sudo apt install python3-pip

    sudo apt install git

    sudo pip install virtualenv



Checking Apache Status and Firewall:

Verify that Apache is active and running:


    sudo service apache2 status


Ensure your firewall allows traffic on port 80 (usually for HTTP):


    sudo ufw status verbose

    sudo ufw enable

    sudo ufw allow 80


Setting Up the Database:

Choose a database system (e.g., PostgreSQL, MySQL, SQLite) and install it on your server.

For PostgreSQL, install and configure it:


    sudo apt install postgresql postgresql-contrib

    sudo systemctl status postgresql

    sudo -u postgres psql

    ALTER USER postgres WITH PASSWORD 'new_password';

    CREATE DATABASE myproject;

    CREATE USER myprojectuser WITH PASSWORD 'password';

    GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;


Cloning the Project and Creating Virtual Environment:

Clone your Django project from GitHub or another repository.

Create a virtual environment and activate it:

    git clone your_project_url

    cd Project_Directory

    virtualenv env_name

    source env_name/bin/activate

    pip install django gunicorn psycopg2-binary python-decouple


Installing Dependencies and Configuring Environment Variables:

Install required Python packages using pip.

Create a .env file with your configuration:

    touch .env


Add your configuration to the .env file:

    DEBUG=False

    SECRET_KEY=your_django_project_secret_key


Creating the Apache Virtual Host Configuration:

Create a configuration file for your Django website:

    cd /etc/apache2/sites-available

    vi django-prj1.conf


Add the virtual host configuration. All virtual hosts share a common IP address.

Enabling the Virtual Host and Restarting Apache:

Enable the virtual host:

    sudo a2ensite django-prj1.conf


Reload Apache:

    sudo systemctl reload apache2


By following these steps, you'll have configured Apache to host your Django application effectively, ensuring seamless operation and easy troubleshooting . 🚀




Related Blogs
Deploy Django project on AWS with Apache2 and mod_wsgi module.
Author: neptune | 18th-May-2024
#Python #Django
In this blog I use the AWS Ubuntu 18.22 instance as Hosting platform and used Apache2 server with mod_wsgi for configurations. We create a django sample project then configure server...

Opportunities - React Django Developer
Author: neptune | 14th-Apr-2023
#React.js #Django
React Django stack is popular for building web applications. Opportunities for React Django developers in Full Stack, Web, and Software development roles...

Boosting django app performance with Custom Context Processors
Author: neptune | 17th-May-2023
#Django
Learn how to create and use custom context processors in Django to add dynamic variables to your templates and enhance user experience...

View More