DEV Community

fractalbit
fractalbit

Posted on

Deploy a laravel 7 app to shared hosting

I know there are much better ways to deploy a laravel application. For example laravel forge and envoyer. 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 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:

    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.

    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
    
    $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 from Brad Traversy (excellent programming tutorials).

If you want to get started with laravel locally and you are using windows i highly recommend laragon. Have fun!

Top comments (5)

Collapse
 
cdsaenz profile image
Charly S.

"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." --> That's me! I work all the time with shared hosting environments and I'm sticking to Codeigniter 3 for that reason. Zero complication. But Laravel is great, indeed, way powerful. So I'll try your way but I still think it stinks; it should be easier. It sounds like they're trying to sell you the related services for deployment..

Collapse
 
aminemshady2080 profile image
aminem2080

hey great guide
but am having trouble creating symlink cant seam to point exactly of target folder.
i didnot use subfolder in public_hmtl cause it would required to redirect to it.

Collapse
 
akashdas profile image
Akash Das

Here is another resourceful article
nihardaily.com/21-deploy-guideline...

Collapse
 
danielsarrow profile image
opokudaniels
Collapse
 
onsoftadilsonjose profile image
Adilson Jose Miguel

great guide but am still getting error
Deprecated: Directive 'allow_url_include' is deprecated in Unknown on line 0

i already upgrade my server php to 7.4
can somebody Guide me 2 Thakx in dvance