AKZN Notes

Archives for My Lazy and Forgetful Mind

Deploy Laravel Project to Hosting or VPS

Last Modified on

Deploy to CPanel

Clone git to cpanel

Taken from https://dashboard.webhostingmagic.com/knowledgebase/242/How-To-Clone-A-Private-Github-Repo-To-A-cPanel-Server.html

Git private repository requires SSH access.

So you must ensure that your hosting package has SSH enabled (customers on Web Hosting Magic need not worry as every hosting package comes with SSH access) and perform additional steps in order to successfully clone a privately-hosted remote repository to your cPanel server.

You can use cPanel's Terminal interface (cPanel >> Home >> Advanced >> Terminal) to access the command line from within the cPanel interface.

You can also use your local system's Terminal or any tool that allows you to access the cPanel server via SSH.

1. Generate An SSH Key

If you have not already configured one, run the following command to generate an SSH key:

ssh-keygen -t rsa -b 4096 -C "username@example"

In this example, "username" represents the cPanel account username and "example" represents the domain name.

After you run this command, the system will prompt you to enter a passphrase.

Do not enter a passphrase.

Press Enter to continue.

2. Verify That You Generated The Ssh Key Correctly

To confirm that the key exists and is in the correct location, run the following command:

cat ~/.ssh/id_rsa.pub

3. Register Your SSH Key With The Private Repository Host

For information about how to register your SSH key with another private repository host (Bitbucket, GitLab, etc), consult that host's website or documentation.

Some repository hosts, such as Bitbucket, do not allow you to configure write access for your access keys.

To register an SSH key with GitHub, perform the following steps:

  1. Log in to your GitHub account.
  2. Navigate to your private repository.
  3. In the top right corner of the page, click Settings. A new page will appear.
  4. In the left side menu, click Deploy keys. A new page will appear.
  5. In the top right corner of the page, click Add deploy key. A new page will appear.
  6. Enter your SSH key data:
  7. In the Title text box, enter a display name for the key.
  8. In the Key text box, paste the entire SSH key. If you want to push code from your cPanel account to your GitHub account, select the "Allow write access" checkbox. Do note that if you do not select this checkbox, you can only deploy changes from your GitHub repository to the cPanel-hosted repository.
  9. Click Add key.

4. Test Out The SSH Key

To test your SSH key, run the following command.

ssh -T git@example.com

where example.com represents the private repository's host - e.g ssh -T git@github.com.

5. Clone The Private Repo

To clone the repository, run the following command on the cPanel account, where "git clone git@example.com:$name/private-repo.git" represents the private repository's clone URL:

git clone git@example.com:$name/private-repo.git

If you see Error: The WebSocket handshake failed at ... when you access cPanel's Terminal interface (cPanel >> Home >> Advanced >> Terminal), recheck your connect.

If you are using VPN, disconnect and use your normal internet connection.

To pull change from remote repo

git pull origin master

change "master" to "main" because github change master branch to main branch

Setup the app

https://www.darwinbiler.com/how-to-properly-deploy-laravel-in-shared-hosting-2019

  • on create symlink do
    ln -s /home/user/app/public/index.php /home/user/public_html
    ln -s /home/user/app/public/assets /home/user/public_html

If your app has email feature using gmail smtp

https://medium.com/graymatrix/using-gmail-smtp-server-to-send-email-in-laravel-91c0800f9662

  • but use port 587 and tls, ssl didnt work

Deploy to VPS


Option 1. Using Github

clone git https

  • git clone [https url repo]
  • login using token [github url/settings/tokens]

setting env

setting aliases

  • change file /etc/apache2/mods-enabled/alias.conf
  • add Alias /demo "/home/czetsuya/projects/eclipse/eclipse-php/demo-portfolio"

setting .htacces on pulic folder,

  • add RewriteBase /[alias name]

migrate database

set permission to writable folder [775]


Option 2. Using Git hooks (post-receive)

Clone repo

clone repo to vps with same tutorial as git-deploy-to-vps
for laravel, make sure to add work-tree outside apache html folder.

Make symlink

make symlink from public folder on work tree into apache html folder
example :

ln -s /var/www/app/public/ /var/www/html/app

Set Permission to storage and cache folder

sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache
chmod -R 775 storage
chmod -R 755 bootstrap/cache

Generate Composer

composer install

Generate new app key

php artisan key:generate

Make .htaccess

make .htaccess inside web root (ie:)/var/www/html/app to remove the need to typepublic from url

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

if you use sanctum api, add this below line

#api htaccess
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

if all above not working, add htacces to web root example /var/www/html/yoursite

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

if above still not working, try this htaccess on root folder, this one works too if used on laravel installed on subfolder

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php

</IfModule>

Leave a Reply

Your email address will not be published.