Deploy a laravel 7 app to shared hosting
Tags: laravel, deploy, hosting
Published: 2020-07-12 12:08:16

I know there are much better ways to deploy a laravel application. For example laravel [forge](https://forge.laravel.com/) and [envoyer](https://envoyer.io/). But if you are like me, **just trying things out, you may not want to invest to these paid alternatives with monthly subscriptions**. This was something that bugged me for years. I hesitated to learn laravel because it seemed that it needed special and costly tools to work with it. **I am glad i finally decided to give laravel a try though, it's awesome!** The good news are that you can deploy a modern laravel 7 app to your shared hosting account **using just FTP**, it just requires some added effort on our part.

The example steps below has been tested with a laravel 7 app using [voyager](https://voyager.devdojo.com/) and installing it in a subfolder of my shared hosting account (which is using plesk). It should apply to any laravel 7 app though and should work with any hosting provider as long as it meets the laravel requirements (ex. supported php version). **Just be careful to change the paths to fit your application and folder-structure on your server**. I assume that you know how to connect with FTP (ex. with filezilla) to your account, create folders, upload files etc. I also assume you know how to export and import a database from phpmyadmin.

So let's see the steps required...

1.  Create a new folder ex. "deploy", outside your project directory (otherwise be sure to add it to the .gitignore file). All the files we edit should be saved there, so we will not break our local setup :)

2.  Edit the .env file with the correct settings for your server (APP_URL and the DB_settingName settings) and save it to the **deploy** folder.

3.  Upload all folders except public on a folder **above** "public_html" or "httpdocs" to another folder ex. "myapp-core"

    _This is absolutely necessary for security reasons. The .env file that holds database passwords and other important configuration data is a plain text file so it should be in a folder not accessible to the puclic._

4.  Upload the contents of the public folder to "public_html/myapp" (cpanel hosting) or "httpdocs/myapp" (plesk hosting).

5.  Change the paths of the index.php file to correctly point to "myapp-core". For example:

    ```php
    require __DIR__ . '/../../myapp-core/vendor/autoload.php';

    $app = require_once __DIR__ . '/../../myapp-core/bootstrap/app.php';
    ```

    and save the file to the **deploy** folder. Since we are deploying the app to the myapp subfolder inside the public folder, we need to go up two times (../../) to find the myapp-core directory. If you are deploying without a subfolder you only need "../" once.

6.  In the /App/Providers/AppServiceProvider.php file, modify the register() method.

    ```php
    public function register()
    {
        // Let laravel know the public path of your application
        $this->app->bind('path.public', function () {
            return base_path('../httpdocs');
            // Change httpdocs to public_html if you are using cpanel
        });
    }
    ```

    and save the file to the **deploy** folder.

7.  Upload the files you edited from the deploy folder **to their respective folders** on the server, overwriting the old files. for example _AppServiceProvider.php_ should go to _myapp-core/App/Providers/_ folder.

8.  Almost there. We need to create a symbolic link for the storage directory (which is outsite our public folder) to point to the public directory. To do this, create a new php file and paste the following code:

    ```php
    <?php

    $targetFolder = $_SERVER['DOCUMENT_ROOT'] . '/../myapp-core/storage/app/public';
    $linkFolder = $_SERVER['DOCUMENT_ROOT'] . '/myapp/storage';
    symlink($targetFolder, $linkFolder) or die("error creating symlink");
    echo 'Symlink process successfully completed';
    ```

    Change the paths to match yours, save it as create-symlink.php, upload it to the public/myapp folder and visit http://yourdomain/myapp/create-symlink.php to run it. If you see the success message, delete the file from the server and move to the final step.

9.  Transfer the database from your local phpmyadmin to your server. _If you encounter errors while importing, for example **"Specified key was too long; max key length is 767 bytes"**, try to export and import in mysql4 compatibility mode (click advanced and choose the appropriate option)._

Cross your fingers and visit your site, **your new laravel app should be up and running!** Give it a try and see if everything is working correctly. If you want something to add in the above process write in the comments.

If you want a video overview of the proccess check out [this guide](https://www.youtube.com/watch?v=6g8G3YQtQt4) from Brad Traversy (excellent programming tutorials).

If you want to get started with laravel locally and you are using windows i highly recommend [laragon](https://laragon.org/). Have fun!