Back to Blog

How to upload a Laravel project to shared hosting


Feb 2024

Image Description
Post Thumbnail

After Coding And Ensuring That Your Web Application Is Ready To Be Deployed To Shared Hosting, The Steps Enlisted Below Will Assist You Deploy Your Laravel Project To Cpanel With Ease.

  • The first step is to zip your laravel project folder.
  • Export your database by going to phpMyAdmin and navigating to the database associated with your project. This will create an SQL file which we will upload to the server phpMyAdmin database.
  • Login to cpanel using your username and passoword provided to you by your hosting provider.
  • The Laravel Project folder cannot be uploaded to the public_html folder for security reasons, it will  be uploaded to the root folder of the hosting plan.
  • The zipped laravel project is the unzipped, a folder with the project name will be created.
  • Copy the contents of the public folder inside your Laravel project folder to public_html
  • Inside the public_html edit the index.php file as shown below.
<?phpuse Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;define('LARAVEL_START', microtime(true)); if (file_exists(__DIR__.'/../storage/framework/maintenance.php')) {
    require __DIR__.'/../storage/framework/maintenance.php';
} require __DIR__.'/../vendor/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php';$kernel = $app->make(Kernel::class);$response = tap($kernel->handle(
    $request = Request::capture()
))->send();$kernel->terminate($request, $response);
  • add the project name here 
  • if (file_exists(__DIR__.'/../projectfoldername/storage/framework/maintenance.php')) {
        require __DIR__.'/./projectfoldername./storage/framework/maintenance.php';
    } require __DIR__.'/../projectfoldername/vendor/autoload.php'; $app = require_once __DIR__.'/../projectfoldername/bootstrap/app.php';

Go to phpMyAdmin on your cpanel and create a database for your project, create a super user to manage your database with all permissions.

Go to the project folder and find .env file and edit  the database name  to match the name of the database you have just created.  Also add the user name and passoword of the supersuser account you have created above. This will enable your application to connect to the database.

Open the database you have created and import the .sql file of the database you had exported from your local machine.

Your application can run without a problem.

To use the public_html as your storage public folder add the following piece of coded to the register function of your  AppServiceProvider

 $this->app->bind('path.public', function () {            return base_path('../public_html');          });

That is it you have successfully been able to run your application on shared hosting.

For any challenges and compliments, please share in the comment section below.

Comments (0)