In this article, you will learn how to host a Laravel app on a cPanel, in case you have a local server like WAMP or XAMPP, this would work to test it out before uploading it to cPanel.
The article will cover 2 steps:
- How to deploy your app to shared hosting.
- How to migrate your databases without running migration commands.
Installing LARAVEL on cPanel
the first step to host Laravel app on cPanel is to install the framework under your user, however, if you are on a Shared Hosting plan (restricted or no SSH access) you won't be able to use most of its functionalities. How to properly deploy your LARAVEL Project on a Shared Plan will be covered below in the next section.
- Login to your Cpanel.
- Under the SOFTWARE section, choose the Softaculous Apps Installer.
- Type Laravel in the search bar and on the next window click Install Now.
- On the next page fill in the requested information and choose Quick Install.
- Choose Protocol: choose SSL or non-SSL protocol (http/https).
- Choose Domain: the domain to install the software.
- In Directory: name of the directory to which you wish the application be installed.
5. Click Install.
6. If the Laravel application is installed properly, you will see the message below:
Laravel is installed successfully
How to deploy your app to shared hosting
assume you've finished building your app. I also assume you are using Laravel 5.0 – although this article would be relevant for Laravel 5.1
- Compress the entire project folder on your local machine. You'll get a zip file – laravelProject.zip
- Open your shared hosting cPanel.
- Click on File Manager.
- Click on Upload.
- Upload the laravelProjec.zip to the root directory – not the public_html.
- Extract the laravelProjec.zip
- Open the laravelProject folder and MOVE the CONTENTS of the public folder to your cpanel’s public_html folder. You can as well delete the empty public folder now.
- Navigate to the public_html folder and locate the index.php file. Right-click on it and select Code Edit from the menu.
- This will open up another tab showing the cPanel code editor.
- change the following lines (22 and 36) from, According to your projects folder name.
‘require __DIR__.’/../bootstrap/autoload.php’; … $app = require_once __DIR__.’/../bootstrap/app.php’;’ to ‘require__DIR__.’/../laravelProject/bootstrap/autoload.php’; ... $app = require_once __DIR__.’/laravelProject/bootstrap/app.php’;’
11. Go to (http://yourdomain.com) should throw database errors (if you have models running on your app). Not to worry. The next phase is migrating your databases to your shared hosting.
Migrating your tables
One good thing about Laravel Framework is that it enables you to quickly setup defined databases with a single command php artisan migrate. Since we are using shared hostingwe can not do this without the use of SSH. This can be done easily if you have up to 10 tables. Its straightforward so let's start:
Create a database on your web host
- Most cPanel comes with PHPMyAdmin and Mysql Database Wizard.
- Use the Mysql Database wizard to create a Database and User then assign the user to the database allowing all privileges.
- Note down the username and password you'll need that soon.
- Use the cpanel's PHPMyAdmin to create your tables. To do this efficiently, open up PHPMyAdmin on your local machine.
- For each table structure create the exact structure on your cPanel’s PHPMyAdmin.
Importing and exporting databases from a local machine
- login to PHPMyAdmin from your computer locate the database your project uses, from the toolbar find the export tool.
- After exporting move over to your shared hosting and do the same steps on create database section.
- If successfully uploaded you'll see the Success Message
- On your cpanel File Manager, navigate to laravelProject.
- Go to config/database.php
- Right-click and select Code Edit.
- Locate line 55 – Your MySQL configuration Section.
- Change the username and password to your MySQL username and password from your cPanel.