You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You may get an error git @ github.com: Permission denied (publickey) If you will try to clone it directly on Web Server Public Folder /var/www So we will clone github repo in User's Home Directory then Move it to Web server Public Directory
Clone Project from your github account
- Using HTTPS Path It doesnt require to setup SSH Key on Github
Syntax:- git clone https_repo_path
Example:- git clone https://github.com/geekyshow1/miniblog.git
- Using SSH Path It requires to setup SSH Key on Github
Syntax:- git clone ssh_repo_path
Example:- git clone [email protected]:geekyshow1/miniblog.git
Run ls command to verify that the project is present
ls
Move Project Folder to Web Server public directory
If Media Files (User Uploaded Files) throws error Permission Denied then Set below permissions
sudo chown -R www-data:www-data media
You may face problem if you work with FTP so to fix this add your user to webserver user group following below instruction:
Check Your User Group
sudo groups raj
Add your User to webserver group
sudo usermod -a -G www-data raj
Verify Your User is in Webserver Group
sudo groups raj
If needed Deactivate Virtual env
deactivate
If you make any changes in your project then you need to pull the new changes from github repo. It will update your website with latest changes.
git pull
After making any change in the project you have to either restart server or touch the wsgi file only then it will reflect on website
Restarting Server is not good idea as there might be multiple website running on same server but still it is solution to reflect changes however we can also use graceful
sudo service apache2 restart
// OR
sudo service apache2 graceful
// OR
cd /var/www/miniblog/miniblog
touch wsgi.py
Special Tip: If you face error "Name duplicates previous WSGI daemon definition" while installing SSL Certificate for your domain then comment below code then try to install SSL Certificate again and after successful installation un-comment it
cd /etc/apache2/sites-available/your_domain.conf
#WSGIDaemonProcess any_name python-home=/var/www/project_folder_name/myprojectenv python-path=/var/www/project_folder_name#WSGIProcessGroup any_name#WSGIScriptAlias / /var/www/project_folder_name/inner_project_folder_name/wsgi.py
A ssl config file will generate. Remember both ssl and non-ssl files can not have same WSGIDaemonProcess and WSGIProcessGroup name (mentioned above as any_name) so you may have to change the name manually in both the files.
How to Automate Django Deployment using Github Action
On Your Local Machine, Open Your Project using VS Code or any Editor
Create A Folder named .scripts inside your root project folder e.g. miniblog/.scripts
Inside .scripts folder Create A file with .sh extension e.g. miniblog/.scripts/deploy.sh
Write below script inside the created .sh file
#!/bin/bashset -e
echo"Deployment started ..."# Pull the latest version of the app
git pull origin master
echo"New changes copied to server !"# Activate Virtual Envsource mb/bin/activate
echo"Virtual env 'mb' Activated !"echo"Installing Dependencies..."
pip install -r requirements.txt --no-input
echo"Serving Static Files..."
python manage.py collectstatic --noinput
echo"Running Database migration"
python manage.py makemigrations
python manage.py migrate
# Deactivate Virtual Env
deactivate
echo"Virtual env 'mb' Deactivated !"# Reloading Application So New Changes could reflect on websitepushd miniblog
touch wsgi.py
popdecho"Deployment Finished!"
Go inside .scripts Folder then Set File Permission for .sh File
git update-index --add --chmod=+x deploy.sh
Create Directory Path named .github/workflows inside your root project folder e.g. miniblog/.github/workflows
Inside workflows folder Create A file with .yml extension e.g. miniblog/.github/workflows/deploy.yml
Write below script inside the created .yml file
name: Deploy
# Trigger the workflow on push and# pull request events on the master branch
on:
push:
branches: ["master"]
pull_request:
branches: ["master"]
# Authenticate to the the server via ssh# and run our deployment script
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.SSHKEY }}
script: "cd /var/www/miniblog && ./.scripts/deploy.sh"
Go to Your Github Repo Click on Settings
Click on Secrets and Variables from the Sidebar then choose Actions
On Secret Tab, Click on New Repository Secret
Add Four Secrets HOST, PORT, USERNAME and SSHKEY as below
Name: HOST
Secret: Your_Server_IP
Name: PORT
Secret: Your_Server_PORT
Name: USERNAME
Secret: Your_Server_User_Name
You can get Server User Name by loging into your server via ssh then run below command
whoami
Generate SSH Key for Github Action by Login into Remote Server then run below Command OR You can use old SSH Key But I am creating New one for Github Action