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
How to Point Domain and Host Laravel Project using Github on Apache Web Server VPS Hosting
Note - If you get Permission Denied Please use sudo
Login to Your Domain Provider Website
Navigate to Manage DNS
Add Following Records:
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
Important: If your project have user uploaded files and you have create symbolic link then Delete the storage folder which is inside your-project-folder/public/ Because It will not work on Live Server. You will have to create symbolic link again on live server.
Push your Poject to You Github Account as Private Repo
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
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
Set storage's File Permission to 644
sudo find storage -type f -exec chmod 644 {} \;
Set storage's Folder Permission to 755
sudo find storage -type d -exec chmod 755 {} \;
Done
Open Mysql
Create Database User (Optional)
Create Database
Open .env File
sudo nano .env
Make below changes, Important: Write DB User, DB User Password and DB Name in .env File, You may have to write DB User and DB Password enclosed with single quote
Create Symbolic Link at public/storage which points to the storage/app/public directory.
php artisan storage:link
Clearing cache - You may need to run with sudo
php artisan cache:clear
php artisan config:clear
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
If you ever need to work on your project you can switch ON/OFF Maintenance mode
php artisan down
php artisan up
How to Automate Laravel 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 ..."# Turn ON Maintenance Mode or return true# if already is in maintenance mode
(php artisan down) ||true# Pull the latest version of the app
git pull origin master
# Install composer dependencies
composer install --optimize-autoloader --no-dev --no-interaction
# Clearing Cache
php artisan cache:clear
php artisan config:clear
# Recreate cache
php artisan optimize
# Run database migrations
php artisan migrate --force
# Turn OFF Maintenance mode
php artisan up
echo"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
Syntax:- cd /var/www/project_folder_name
Example:- cd /var/www/miniblog
Pull the changes from github just once this time.
git pull
Your Deployment should become automate.
On Local Machine make some changes in Your Project then Commit and Push to Github Repo It will automatically deployed on Live Server
You can track your action from Github Actions Tab
If you get any File Permission error in the action then you have to change file permission accordingly e.g. Maintenance mode ON/OFF may need File Permission so you can set it to 775
Changing storage/framework File Permission (This is just for example)
cd /var/www/miniblog/storage
sudo chmod -R 775 framework