How to Point Domain and Host Django Project using GitHub on Apache Web Server VPS Hosting
Table Of Content
- Step 1: Configure Domain Settings
- Step 2: Prepare Django Project on Local Machine
- Step 3: Access Remote Server via SSH
- Step 4: Make Connection between Remote Server and GitHub via SSH Key
- Step 5: Clone and Deploy Django Project on Remote Server
- Step 6: Configure Apache and Django Settings
- Step 7: Automate Django Deployment using GitHub Actions
- Step 8: Finalize Deployment and Maintenance
How to Point Domain and Host Django Project using GitHub on Apache Web Server VPS Hosting
Step 1: Configure Domain Settings
-
Login to Your Domain Provider Website:
- Log in to your domain provider’s website dashboard.
-
Navigate to Manage DNS:
- Find and navigate to the DNS management section.
-
Add DNS Records:
-
Add the following DNS records to point your domain to your VPS:
Type Host/Name Value A @ Your Remote Server IP A www Your Remote Server IP AAAA @ Your Remote Server IPv6 AAAA www Your Remote Server IPv6
-
Step 2: Prepare Django Project on Local Machine
-
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:DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'mbdb/db.sqlite3', } }
-
-
Generate Requirements File:
-
Open a terminal, activate your virtual environment, and create a
requirements.txt
file:pip freeze > requirements.txt
-
Deactivate the virtual environment.
-
-
Push Your Project to GitHub:
- Push your Django project to your GitHub account as a private repository for version control.
Step 3: Access Remote Server via SSH
-
Verify Required Software:
-
SSH into your remote server and verify that all necessary software is installed:
apache2 -v python --version apache2ctl -M pip --version git --version
-
-
Install Missing Software:
-
If any required software or modules are missing, install them using apt:
sudo apt install apache2 python libapache2-mod-wsgi-py3 python3-pip git
-
-
Install Virtual Environment:
-
Install and set up a virtual environment:
sudo pip install virtualenv
-
-
Verify Apache2 and Firewall:
-
Ensure Apache is active and running, and verify firewall settings:
sudo service apache2 status sudo ufw status verbose
-
Step 4: Make Connection between Remote Server and GitHub via SSH Key
-
Generate SSH Keys:
-
Generate SSH keys on your remote server:
ssh-keygen -t ed25519 -C "your_email@example.com"
-
If you encounter permission denied, change ownership of the
.ssh
directory:sudo chown -R user_name .ssh
-
-
Copy Public SSH Key to GitHub:
- Copy the public SSH key and add it to your GitHub account’s deploy keys.
-
Verify Connection:
-
Verify the SSH connection between your server and GitHub:
ssh -T git@github.com
-
Step 5: Clone and Deploy Django Project on Remote Server
-
Clone Project from GitHub:
-
Clone your Django project from GitHub to your server’s home directory:
git clone https_repo_path
or
git clone ssh_repo_path
-
-
Move Project Folder to Web Directory:
-
Move the project folder to the web server’s public directory:
sudo mv project_folder_name /var/www
-
-
Navigate to Project Directory:
-
Go to your project directory:
cd /var/www/project_folder_name
-
-
Set Up Virtual Environment and Install Dependencies:
-
Create and activate a virtual environment, then install project dependencies:
virtualenv env_name source env_name/bin/activate pip install -r requirements.txt
-
Step 6: Configure Apache and Django Settings
-
Create Virtual Host File:
-
Create a virtual host configuration file for your domain:
sudo nano /etc/apache2/sites-available/your_domain.conf
-
Add necessary configuration details for Apache.
-
-
Check Configuration and Enable Virtual Host:
-
Verify the configuration and enable the virtual host:
sudo apache2ctl configtest cd /etc/apache2/sites-available/ sudo a2ensite your_domain.conf
-
-
Restart Apache:
-
Restart Apache to apply changes:
sudo service apache2 restart
-
-
Update Django Settings:
- Modify Django project settings to reflect your domain and server configuration.
Step 7: Automate Django Deployment using GitHub Actions
-
Create Deployment Script:
- Create a deployment script (
deploy.sh
) inside the project’s.scripts
folder.
- Create a deployment script (
-
Set File Permissions:
-
Set execute permission for the deployment script:
git update-index --add --chmod=+x .scripts/deploy.sh
-
-
Configure GitHub Actions:
-
Create a GitHub Actions workflow file (
deploy.yml
) inside the.github/workflows
directory. -
Define the workflow to trigger deployment on push or pull request events.
-
-
Add Secrets to GitHub:
- Add repository secrets for the server host, port, username, and SSH key.
-
Generate SSH Key for GitHub Actions:
- Generate SSH key on the remote server and add it as a repository secret on GitHub.
Step 8: Finalize Deployment and Maintenance
-
Pull Changes from GitHub:
- SSH into your server and pull changes from GitHub to deploy updates.
-
Restart Server or Touch WSGI File:
-
Restart Apache or touch the WSGI file to apply changes:
sudo service apache2 restart
-
-
Monitor Deployment:
- Track deployment progress and errors using Apache error logs and GitHub Actions.
-
Maintenance and Troubleshooting:
- Regularly monitor logs for errors and perform necessary maintenance tasks.
- Troubleshoot any issues related to file permissions, database access, or server configurations.
By following these steps, you can successfully deploy your Django project to a VPS hosting server using GitHub for version control and automation. With automated deployment via GitHub Actions, you can streamline your development workflow and ensure smooth updates to your live website.