How to Host a Laravel Project on a Shared Hosting?

0
546
How to Host a Laravel Project on a Shared Hosting

Shared hosting is a great way to host your web application and also save money. It has become very popular lately, and if you are looking for a way to host your Laravel projects, you should definitely consider shared hosting.

In this guide, you will be provided with clear and detailed instructions on hosting a Laravel project on a shared hosting through cPanel.

Step-by-step guide: How to host a Laravel project on shared hosting?

It is a straightforward process, and you will easily be able to do it if you just follow the steps detailed below. However, you can also test it on a local server, such as XAMPP or WAMP prior to uploading it on the cPanel.

Step 1: Make a Laravel project

Firstly, what you need to do before we get into the hosting process is to develop a Laravel project if you haven’t already. If you are unsure about how to create a Laravel project, you can check out the official Laravel documents that will provide you with a tutorial on how to do it.

Step 2: Recode the directory structure

The servers on a shared hosting look for an index.php or index.html file at the root to execute and due to this reason, you have to recode the directory structure to allow it to work. After a new installation, your public folder of the Laravel application should look something like this:

Recode the directory structure

Now what you need to do is move the files present to the root directory of your Laravel application.

Note: If you have created a ‘symlink’ in your Laravel project’s storage folder, leave it there. Also, you are not required to touch the .htaccess file under any circumstance and just leave it in the root folder. If you still want to move it because you know what you are doing, then make sure you create a backup.

After you have moved the files successfully, your directory structure will appear something like this:

Directory structure

Step 3: Edit files according to the new directory structure

Now that you have refactored the directory structure, you will need to edit a couple of files. This is so because although shared hosting servers look for the index.php or index.html file at the root of the directory for the proper execution of the application, Laravel is not set up like that by default.

So, in order to make the Laravel project run properly on cPanel, you need to edit some files, first of which is the index.php file.

Laravel project run properly on cPanel

You can see that the purpose of each line is commented on by the team at Laravel but still to make things clearer for you, here is what we have done and you need to do this the exact way.

Since you have moved the index.php file to the root, now you need to change the path of these files so that Laravel has their correct location. Now make sure that you remove the ‘../’ in front of the directory names in lines 24 and 38.

For those of you who are using newer versions of Laravel, you are required to visit the server.php file that is located generally in the root directory by default. It would be best if you made similar changes to its path like it is explained above.

Laravel 1

Now you will find that your application is running properly if you test it.

Step 4: Make storage work like the default (Optional)

You can create human-friendly URLs for your resources and things like videos, pictures, and CSS. To do this, you must create a ‘symlink’ from your public folder to your storage folder. If you do not know what a symlink is, then you can go through this tutorial.

To create this symlink, type the following command and run it:

php artisan storage:link

After creating a symlink in your project the way you left it in the 3rd step, you will notice that the links are not working in the same manner as in the default Laravel application. URLs to a file stored in the storage folder are supposed to have a path location like this:

<host name>/public/storage/<file> instead of like: <hostname>/storage/<file>

You can fix this simply by creating a route in the routes/web.php file, and for that, you need to run this code:

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

Now your all requests made to /storage/<anything> will be redirected to /public/storage/<anything>

Step 5: Upload the project to cPanel

Now the last step is to upload the project to the shared hosting using cPanel. But before that, you need to ZIP your Laravel-based project. Select all project files except the git, node_modules folders in your Laravel project, and then ZIP them all.

After you have done this, you need to upload the entire project ZIP file to the public_html folder in your shared hosting cPanel and then extract the ZIP file. Do not put this in any subfolder in public_html and remember that it should be only located in the public_html folder.

Now open the database.php file in the config folder. Then you can update the database name, username, and password in the database.php file and save it. Ensure that you do not enter details of your database or any other sensitive data in the .env file for shared hosting.

'mysql' => [
    'driver' => 'mysql',
    'url' => '',
    'host' => '127.0.0.1',
    'port' => '3306',
    'database' => 'mydatabase',
    'username' => 'mydbusername',
    'password' => 'here_password',
    'unix_socket' => '',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
    'options' => extension_loaded('pdo_mysql') ? array_filter([
        PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
    ]) : [],
],

Thus, you have now successfully deployed your Laravel project in a shared hosting cPanel. You are free to use your web application or website by hitting the domain name.

Conclusion

This guide must have, hopefully, prove to you that it is, after all, not that hard to host a Laravel project on a shared hosting through cPanel. Running your web application on shared hosting is better than private hosting since it saves you money, time, and resources.

People are also reading: 

LEAVE A REPLY

Please enter your comment!
Please enter your name here