There are multiple ways to deploy your project on the server. some of them are using FTP account or uploading local project folder directly to the server and moving the files to public_html folder . Most of the methods are good when your application is completely developed or you are not changing your website features frequently. But if your need to update its feature frequently and there are lots of change time to time then its tedious to track your files and change then or upload them in the server time to time.
So here SSH comes to play a great role of git ssh setup and installing composer in the server so that we perform every action from the terminal of our share hosting c-panel.
we will see how we can deploy Laravel project on shared hosting by following 6 easy steps. And I personally believe that this is the best way to deploy so that one can maintain the application easily for continuous integration. Now lets see the steps below.
Step 1 : Go to your c-panel and open Terminal
use your login credentials to login in c-panel.
- Scroll down and in advance system you will find Terminal (Make sure you have SSH access from your provider)
Step 2 : Install Composer
Now lets install composer to our system for that lets configure our terminal shell environment by setting alias.
- Copy and pest the following Two lines one by one and don't forget to replace "UserName" with your c-panel user name.
echo 'alias composer="php -d allow_url_fopen=On /home/UserName/composer.phar"' >> ~/.bashrc source ~/.bashrc
*Also make sure your root directory matches i.e. /home/username/ if note replace the above accordingly like home2/username etc.
- Next is to download composer enter the following command
cd ~ curl -k -O https://getcomposer.org/installer
- Run the installer
php -d allow_url_fopen=On installer
after these commands you will find composer.phar and installer file in the filemanager>> home/UserName directory. To conform run
Step 3: Configure Git for SSH key
- Generate SSH key
ssh-keygen -t rsa -b 4096 -C "UserName"
replace UseName with c-pannel user name .
Hit enter 3 times or you can provide the parameters while generating ssh key as your prefrence.
Run this command
now copy the ssh-key (select the ssh-key in c-pannel and right click >> copy)
Go to your project repository (github, gitlab or bitbucket) and add access-key or deploy key.
gitlab -> https://gitlab.com/user_name/repo_name/-/settings/repository >>deploy keys >>expand>> past key >> save.
Bitbucket -> https://bitbucket.org/user_name/repo_name/admin/access-keys >>Add key >>Paste key >> save.
Github -> https://github.com/user_name/repo_name/settings/keys >> add deploy key >>paste key>>save.
Note: user_name == your git user name and repo_name = project repo name
Step 4: Setup your application
Create a new folder on filemanager >> home/c-panel-user-name say "app". or use terminal for that as well as follows.
git clone firstname.lastname@example.org:username/repo.git app
Now cd into your projects folder you just cloned
cd /path and run composer install
cd app/repo_name composer install
create .env file in the project directory
if you can't see the .env file click the setting icon in filemanager top-right corner and check the show hidden files option
Add a new application key.
php artisan key:gen
Step 5 : Setup your database
Navigate to Databases >> Mysql Databases
create new database.
create user and provide all the privilege to the user.
add these credentials to the .env file.
php artisan cache:clear
Migrate all databases
php artisan migrateand seed.
Step 6 : change file permissions and make accessible for public
Set the correct folder permissions
chmod -R 775 storage
- create a symlink of public file
ln -s /home/c-panel-username/app/repo_name/public /home/c-panel-username/public_html
if you get file exist error then use rm -r public_html
- Create a symlink for your storage
ln -s /home/c-panel-username/app/repo_name/storage/app/public /home/c-panel-username/public_html/storage
- Run storage link
php artisan storage:link
If every thing is done correctly as specified congratulations your project is now live. And from now to reflect your changes jut open your c-panel terminal navigate to you code directory and use git pull
cd app/repo_name // (cd /path_where_all_the_files_lies i.e. previous directory where u have cloned the project at start) git pull origin branch_naem
And also you can run all artisan commands and composer commands from c-panel terminal.
cd /path php artisan down // do whatever artisan commands needed // or git pull , reset commands // or composer commands dump-autoload , update , etc php artisan up
Hope your web-application is running properly now, hope this article helped you.
If you have any doubt let me know in the comments, Thanks.