you will need a personal access token from github to clone or manage permissions of the repository.
Enter a token name and expiry date you need (30 Days recommended).
Note: if your repository exist in the organization, you need select your organization from Resource Owner Dropdown.
After login to Hosting Dashboard ( Sometimes CPanels ) you will get all details to setup SSH to connect to your server from local computer.
How to Setup SSH From Shared Hosting Locally.
Since it is a static site you can just create a folder in any existing project you want or you can set up a separate domain/subdomain for it.
Make a folder
Terminal
cd ~/www/<domain>
mkdir <your project name>
or Create a subdomain from main domain
then run following command,
Note: before running this command please take a backup of the the files in the folder, this command will delete all files and folders under this directory.
Terminal
cd ~/www/<your domain or folder>
rm -R *
You can copy your repository https link from github
e.g. it will be looking like this : https://github.com/Kashifwahaj/seat-booking.git
you need to add your github username and your generated token in URL while cloning (See below).
Terminal
git clone https://<username>:<fine-grained-token>@github.com/<name>/<repo-name> .
Donβt forget Dot(.) at the end of clone command.
if your hosting need a public_html folder, you can run the following
Terminal
ln -s public public_html
composer install
if composer install throws error then run the following
composer install --legacy-peer-deps
if this also not working then try running the following
rm package-lock.json
composer update
now if this also not working then it might be PHP version issue then in your cpanel or hosting panel change php version to as mention to your laravel project composer.json file, then try above commands again.
if changing php version also not working then run the following
/usr/local/bin/php81 /usr/local/bin/composer.phar update
create an .env file from .env.example
cp .env.example .env
now create a database and user in your cpanel/hosting and add user to database, your cpanel/hosting may guide you for this.
after than dbname,username and password paste in .env
Run migrations
/usr/local/bin/php81 artisan migrate
/usr/local/bin/php81 artisan optimize:clear
or if you do not have migrations then import your old database from PHPMyadmin.
Important: Now clear cache from cpanel/hosting panel also.
then try visit the website.
Now you project is deployed on server but it not CICD yet, it need to be updated automated whenever someone push code to github repository.
for that we have many ways, one of the ways is to setup webhook to manage automatic update (git pull) on the server.
Terminal
cd ~/www/<your domain or folder>
mkdir webhooks
touch pull.php
2.Open pull.php and setup a webhook endpoint in the pull.php
Terminal
cd ~/www/<your domain or folder>
cd webhooks
nano pull.php
Inside pull.php before paste you need to make a string key by yourself, we will use this key in following steps. e.g. βabc@123β
We are setting up a api end point in pull.php. endpoint will be : https://your-domain/folder/webhooks/pull.php we will pass our string key with the endpoint url : https://your-domain/folder/webhooks/pull.php?webhook_key=abc@123
paste below code in pull.php:
<?php
header('Content-Type: application/json');
header('Accept: application/json');
if($_SERVER['REQUEST_METHOD'] == "POST"){
$WEBHOOK_KEY = 'abc@123'; // this is string key you need to paste here
http_response_code(200);
$request = file_get_contents('php://input');
$data = $_GET;
if($data['webhook_key'] == $WEBHOOK_KEY){
$username = "<github-username>" ; // here write github username
$token = "<github-fine-grained-token>"; // here paster your generated token
$url = "https://".$username.":".$token."@github.com/username/repo-name.git";
echo shell_exec('git pull '.$url);
return;
}
http_response_code(403);
echo "UnAuthenticated";
return;
}
else {
http_response_code(405);
echo 'Method Not Allowed';
}
this is a simple api endpoint in php which will be hit by github whenever someone push on github.
endpoint : https://your-domain/folder/webhooks/pull.php?webhook_key=abc@123
whenever this endpoint get a hit it will take git pull and server cod will be updated.
Terminal
cd ~/www/<your domain or folder>
nano .gitignore
Paste or add below line inside .gitignore file.
.gitignore
/webhooks
Now commit and push this code to github so that it will not create conflict in future.
cd ~/www/<your domain or folder>
git add .
git commit -m "Webhooks Setup and Gitignore"
git push
Now we just need to let github know to call/hit this api endpoint whenever someone push on github repository.
https://your-domain/folder/webhooks/pull.php?webhook_key=abc@123
Now github will start a ping to your server just to check everything is fine.
Now you just need to push to your github repository and if everything is setup correctly your server code will automatically updated.
Note: Sometimes changes will not reflect , in this case clear cache in your server.