Electronics Engineer Fixing Cables on Server

Point Domain and Host Django Project on Apache Web Server VPS Hosting

Hosting your Django project on an Apache web server requires careful configuration of both your domain settings and server environment. In this step-by-step guide, we’ll walk through the process of pointing your domain to your VPS hosting, setting up your Django project on the server, and configuring Apache to serve your application.

Step 1: Configure Domain Settings

  1. Login to Your Domain Provider Website:
    Access your domain provider’s website and log in to your account.
  2. Navigate to Manage DNS:
    Find the option to manage DNS settings for your domain.
  3. Add DNS Records:
    Add the following DNS records to point your domain to your VPS:
  • A Record:
    • Host/Name: @
    • Value: Your Remote Server IP
  • A Record:
    • Host/Name: www
    • Value: Your Remote Server IP
  • AAAA Record (for IPv6):
    • Host/Name: @
    • Value: Your Remote Server IPv6
  • AAAA Record (for IPv6):
    • Host/Name: www
    • Value: Your Remote Server IPv6

Step 2: Prepare Django Project on Local Machine

  1. Update Database Path:
  • Move your database file (db.sqlite3) into a new folder within your project directory, e.g., mbdb/db.sqlite3.
  • Modify settings.py to reflect the new database path:
    python DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'mbdb/db.sqlite3', } }
  1. Generate Requirements File:
  • Open a terminal, activate your virtual environment, and create a requirements.txt file:
    bash pip freeze > requirements.txt
  • Deactivate the virtual environment.
  1. Create Zip File:
  • Create a zip file of your project folder.

Step 3: Transfer Project to Remote Server

  1. Copy Zip File to Server:
  • Use SCP to copy the zip file from your local machine to the remote server:
bash scp -P Port_number Source_File_Path Destination_Path
  • Example:
bash scp -P 22 miniblog.zip raj@216.32.44.12:
  1. Access Remote Server via SSH:
  • SSH into your remote server:
bash ssh -p PORT USERNAME@HOSTIP

Step 4: Configure Apache and Deploy Django Project

  1. Install Required Software:
  • Verify and install necessary software and modules:
bash apache2 -v python --version apache2ctl -M pip --version
  1. Install Missing Software:
  • If any required software or modules are missing, install them using apt:
bash sudo apt install apache2 python libapache2-mod-wsgi-py3 python3-pip
  1. Verify Apache and Firewall:
  • Ensure Apache is active and running, and verify firewall settings:
bash sudo service apache2 status sudo ufw status verbose
  1. Unzip Project:
  • Unzip the project file on the server:
bash unzip miniblog.zip
  1. Move Project to Web Directory:
  • Move the project folder to the web server’s public directory:
bash sudo mv miniblog /var/www
  1. Configure Virtual Environment:
  • Navigate to the project directory and set up a virtual environment:
bash cd /var/www/miniblog virtualenv env source env/bin/activate pip install -r requirements.txt
  1. Create Virtual Host File:
  • Create a new virtual host configuration file for your domain:
bash sudo nano /etc/apache2/sites-available/your_domain.conf
  • Add the necessary configuration details for Apache.
  1. Enable Virtual Host and Restart Apache:
  • Enable the virtual host and restart Apache:
bash sudo a2ensite your_domain.conf sudo service apache2 restart

Step 5: Update Django Project Settings

  1. Modify settings.py:
  • Update the Django project settings:
    • Set ALLOWED_HOSTS to include your domain.
    • Set DEBUG to False.
    • Configure STATIC_URL, STATIC_ROOT, MEDIA_URL, and MEDIA_ROOT.
  1. Restart Apache:
  • Restart Apache after making the changes:
bash sudo service apache2 restart

Step 6: Troubleshooting and Permissions

  • Check error logs for any issues:
  cd /var/log/apache2
  cat error.log
  • Adjust permissions for database and media files as needed.

Conclusion:
By following these steps, you’ll successfully configure your domain to point to your VPS hosting, deploy your Django project on an Apache web server, and ensure seamless operation of your application.