Get noticed on the first page of Google

How to Host a Laravel Project on a Shared Hosting

How to Host a Laravel Project on a Shared Hosting

In this article, I’ll be showing you how to host your Laravel projects on a shared hosting using the usual CPanel we all know and love. If you have a local server like WAMP or XAMPP, this would work to test it out before uploading to your CPanel. Let’s get started

1. Create a Laravel Project

The first step in this tutorial is to create a Laravel project if you don’t already have one. Feel free to check out the official Laravel docs here for how to create one if you don’t already know how.

2. Refactor the Directory Structure

When hosting your projects on shared hosting servers, the servers always look for an index.html or index.php file at the root of your project folder to execute. When a Laravel project is created, the index.php file is usually located in the public folder. In order to make your application work on a shared hosting via CPanel, or WAMP/XAMPP, you need to restructure your directory.

Here’s what your public folder should look like after a new installation:

What you want to do is move these files to the root directory of your application.

NB: If you already created a “symlink” to the storage folder in your project, please leave it in the public folder as this would mess up Laravel’s process of loading resources. Also remember not to edit the .htaccess file after moving it to the root unless you thoroughly know what you’re doing and in which case you should definitely make a backup.

If you moved the files successfully, your directory structure should look like this:

3. Edit Files to Reflect our New Directory Structure

Although servers look for the index.php file in the root of the application, Laravel isn’t setup that way by default; because of this, you have to edit a couple of files so Laravel can know where to look for the files it needs to run properly. The first file we’ll be editing is theindex.php file. After editing it, it should look like this:

The purpose of each line is commented in there by the lovely Laravel team but just to clarify what we’ve done here — since we’ve moved the index.php file to the root, we need to change the path of these files so they can be found correctly. On lines 24 and 38, we simply remove the ../ in front of the directory names.

The next file we’ll be editing is the server.php file which is usually in the root of our Laravel application by default. After editing it, it should look like this:

With these changes, our application should work properly if we test it.

4. (Optional) Hack to Make Storage Work Like the Default

In Laravel projects, it is necessary to create a symlink to your storage folder from your public folder in order to allow Laravel create human-friendly URLs for your resources and assets like pictures, videos and css . For those who may not know what “symlinks” are, here is a really good tutorial for that.

Creating this symlink in Laravel is easy, just run this command from your terminal:

php artisan storage:link

When we create a symlink in our project the way we left it in step 3, we would notice that the links don’t quite work the way they do in the default Laravel application — URLs to a file stored in our storage folder would have to be located using <host name>/public/storage/<file> rather than the <host name>/storage/<file> that we’re used to. There’s a small hack to fix this, all we need to do is create a route in our routes/web.php file.

Route::get(‘/storage/{extra}’, function ($extra) {
     return redirect(“/public/storage/$extra”);
})->where(‘extra’, ‘.*’);

What this block of code would do is redirect all requests made to /storage/<file> to /public/storage/<file> without any hiccups.

5. We’re done here

At this point, we’re done. What’s left to do is upload this to our CPanel’s htdocspublic_htmlwww (whatever it’s called there) or in the directory of the subdomain, if we’re doing that stuff using FTP or zipping up the folder and uploading it directly.

If your project would need a database connection, feel free to set the database credentials in the .env file located at the root of your project like you would normally do and show your beautiful project to the world.

Conclusion

It’s not a daunting task to have your Laravel projects hosted on CPanel if you’d rather not use a Private Hosting. For help with cPanel and your website, you can reach us here. We would love to hear from you.

 

In this article, I’ll be showing you how to host your Laravel projects on a shared hosting using the usual CPanel we all know and love. If you have a local server like WAMP or XAMPP, this would work to test it out before uploading to your CPanel. Let’s get started

Related Posts