Simple steps to Deploy Laravel 5/6/7/8 Website on Shared Hosting
Published: 2021-03-01 18:56:48

<p>Deploying on cPanel seems tricky because you cannot run `php artisan serve` command there and the index.php file resides in the public folder. If you deploy as is, you will have to visit your site as `domain.tld/public` and that's not workable for SEO and convenience.</p>

<p>Using `git` and `composer`, you can easily deploy your site and manage further updates.</p>

<p>Laravel is best hosted on a VPS or dedicated server but you may not want to pay more for hosting a small project and choose to settle for shared hosting.</p>

<h3>Step 1 - Visit https://yourdomain.com/cpanel</li><li>Login with your cPanel credentials.</h3>
<h3>Find the terminal icon or search for it and click it.</h3>

<blockquote class="wp-block-quote"><p>If the Terminal Icon does not exist, it means your shared account doesn't have "Shell Access" turned on. Ask your Hosting Provider to turn it on for you.</p><p>It could also mean that your cPanel version doesn't have the terminal option. Download and use an SSH client but you'll have to enable it on the server. Contact your provider on how to use SSH client.</p></blockquote>

<p>It will launch the terminal. This is where you will do most of the work.</p>

<ul><li>Check PHP version and ensure it is >= 7.1.3</li><li>Check that composer is installed on the server by running the composer command</li></ul>

<pre class="wp-block-code">
<code>
  composer -V
  // Composer version 1.7.2 2018-08-16 16:57:12
</code>
</pre>

<blockquote class="wp-block-quote"><p>Most cPanel comes with Git pre-installed, if it is not, you might need to ask your hosting provider to have it installed.</p></blockquote>

<h2>Step 2 - Set up the app</h2>

<ul><li>Clone the repo to a folder named "project", it should be in /home/username/project</li></ul>

<pre class="wp-block-code"><code>
  git clone git@github.com:username/repo.git project
</code></pre>

<ul><li>Go to the project folder and install the dependencies</li></ul>

<pre class="wp-block-code"><code>
 cd project
 composer install
</code></pre>

<ul><li>Create a new MySQL database in cPanel in DATABASES -> MySQL Databases section. take note of the database name, username and password.</li><li>copy the .env.example file to .env</li></ul>

<pre>
<code>
 cp .env.example .env
</code>
</pre>

<ul><li>edit the .env file and configure it with your setting</li></ul>

<pre><code> nano .env </code></pre>

<p>few key settings that is important are</p>

<pre class="wp-block-code"><code>
 DB_HOST=localhost
 DB_CONNECTION=mysql
 DB_PORT=3306
 DB_DATABASE=dbname
 DB_USERNAME=dbuser
 DB_PASSWORD=dbuserpass
 APP_ENV=production
 APP_DEBUG=false
</code></pre>

<ul><li>Generate the application key</li></ul>

<pre class="wp-block-code"><code> php artisan key:generate </code></pre>

<ul><li>Run the migrations</li></ul>

<pre class="wp-block-code"><code> php artisan migrate </code></pre>

<blockquote class="wp-block-quote"><p>If you received an error of "Specified key was too long; maxkey length is 1000 bytes", you need to check this article for the&nbsp;<a href="https://laravel-news.com/laravel-5-4-key-too-long-error">fix</a>. Apply the fix in the repo, then issue "git pull origin master" so that this copy will receive the fix . Then go to PhpMyAdmin, drop all the tables there and try running the migrate command again.</p></blockquote>

<ul><li>Set the permission of the storage folder so that the web server can write to it</li></ul>

<pre class="wp-block-code"><code> chmod -R 775 storage </code></pre>

<ul><li>Optimize things up!</li></ul>

<pre class="wp-block-code"><code> 
 php composer dump-autoload
 php artisan config:cache
 php artisan route:cache
</code></pre>

<h2>Step 2 - Make the app accessible to public</h2>

<p>right now, the app is not accessible outside because we placed it under /home/user/app directory. The only folder that is accessible to public is /home/user/public_html but we don't want to place all of our framework files into public_html folder. So what we gonna do is simply make a <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Symbolic_link" target="_blank">Symbolic link</a> to our /home/user/app/public folder.</p>

<p>to do this, backup first a copy of the public_html folder</p>

<pre class="wp-block-code"><code> 
 mv public_html public_html_old
</code></pre>

<p>create the symlink</p>

<pre class="wp-block-code"><code>
 ln -s /home/user/project/public /home/user/public_html
</code></pre>

<p>your folder structure should be similar to this</p>

<pre class="wp-block-code"><code>
 public_html -> /home/user/project/public public_html_old www -> public_html
</code></pre>

<h3>Bravo! Your site should be working now.</h3>

<ul><li>You can deploy further changes by `git pull`ing the latest version or you using Continuous Deployment integration.</li></ul>

<p>Inspired by <a rel="noreferrer noopener" href="https://www.darwinbiler.com/how-to-properly-deploy-laravel-in-shared-hosting-2019" target="_blank">Darwin Biler</a></p>