A Step By Step Guide To Deploy Laravel On Shared Hosting (that Actually Works)
Given Ncube
Deploying your laravel website on shared hosting is not something that is well documented and some articles out there don't work, trust me I tried them all and I wish I had a guide like this then so I decided to write one.
What you'll need
-
Your laravel app of course
-
git
-
a Github/Gitlab account (which I assume you have)
-
a shared hosting account
-
ssh (optional)
Summary
To deploy Laravel in your shared hosting you simply need to clone your git repo in your hosting account, set up database, setup .htacess for document root and you are up and running.
Preparation
Initialize a git repository if you haven't already
git init && git add . && git commit -m "Initial commit"
Next, go to Github and create an empty repository if you haven't already, and then push your code to that rep
git remote add origin your-github-url && git push origin master
Next, build your assets for production
npm run production
Commit those changes and push them
git add . && git commit -m "Ready for production"
git push origin master
Deploying
Depending on your hosting provider you might need to contact your hosting provider for ssh access. For security reasons some hosting providers only allow shell access via a browser interface. I'm hosting with [ Tremhost](https://www.tremhost.com/clientarea/aff.php?aff=78&key=Web Hosting and Emails), they provide shell access via browser.
Login to your Cpanel account and go to Advanced > Terminal.
Once the terminal is open head over to the public_html
directory.
cd public_html
Make sure your hosting provider has git installed otherwise you might have to contact them. Before you can clone the repo you might want to save your git credentials to avoid entering them every time you want to pull or push since we won't be using ssh.
git config credential.helper store --local
Make sure to use the --local
flag so that they are only specific to that git repo.
Now clone your repo
git clone your-https-github-repo-url
For security reasons your hosting provider has disabled some functions which may be required by laravel to run particularly proc_open
.
Let's enable it. On your hosting provider terminal head over to the root directory open the php.ini
file with nano or vim I prefer nano.
nano php.ini
replace whatever is in there with
disable_functions = system,exec,passthru,shell_exec,proc_get_status,proc_terminate,proc_close,virtual,popen,show_source,curl_multi_exec
Close nano and save the file.
Now open your cpanel in a new tab and create a database. And then cd
to public_html/your_repo_name
and set your database credentials in the .env
file.
cp .env.example .env
After setting all your environment variables run
composer install
to install your dependencies. Then run your migrations by
php artisan migrate
The last thing is to set laravel's public
directory as the document root of your domain, to do this open the .htaccess
in the public_html
directory.
nano .htaccess
At the bottom of the file add the following lines
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com$
RewriteRule (.*) /your-repo/public/$1 [L]
This will route all your traffic from public_html
to public_html/your-laravel-app-folder/public
.
Save the file and open a new tab and visit yourdomain.com and your app should be live.
Happy coding!