Laravel: Deploy Laravel Projects On Shared Hosting

Deploy Laravel 5 / 5.1 / 5.2 / 5.3 / 5.4 Projects on Shared Hosting Servers Using cPanel Options (Without Using SSH)

Posted on February 24, 2017 in Laravel Tutorials

Welcome guys! In this tutorial, I am gonna show you how to deploy your awesome Laravel 5.X project on a Shared Hosting Server.

(01) Access cPanel

Go to your cPanel’s login page using its access IP address provided by your hosting provider.

Now log in to your cPanel and it will look like this. In this tutorial, we are using File Manager, MySQL® Databases, and phpMyAdmin features in cPanel.

(02) Upload Project Files

Go to File Manager. Create a new Folder in your root directory. For this tutorial, I am going to name my new folder as “MyAwesomeProject”.

Now we need to get the entire collection of your Laravel project files and directories into this MyAwesomeProject folder (including vendor files). Go inside your project folder on your local computer. Open your terminal inside the Laravel project folder and run php artisan cache:clear to flush your application’s cache and then run php artisan config:clear to clear the cached version of your config file, if there is any.

Now zip the entire Laravel project, and upload the zip into MyAwesomeProject folder. When the upload finishes, unzip all project files into MyAwesomeProject folder by selecting destination as ~/MyAwesomeProject/. If your hosting provider lets you use the SSH (Secured Shell) , you can even directly clone your repo from Github and then run composer install/composer update command to get the latest project files. But in most cases, for Shared Hosting packages, SSH access is not provided. So we have to upload all the project files manually as a zipped file.

Now go into the public folder of your Laravel project (~/MyAwesomeProject/public/). Look whether you can see .htaccess file. If you can’t see this .htaccess file (since Linux hides files with filenames starting from .), click on Settings (on top right corner) and on the Preferences window, enable Show Hidden Files (dotfiles) and save your selection. You now must see the dotfiles. Next, copy all the files inside this public folder (~/MyAwesomeProject/public/) into the public_html folder (~/public_html). Anything we store inside public_html folder can be accessed using the browser via your public domain, so make sure you do not keep any private stuff inside this public_html folder.

(03) Configure Files Paths

Let’s open our public_html folder and you now must see that all the files inside our Laravel project’s public folder have properly copied into public_html folder. If everything looks good, open index.php file (~/public_html/index.php) in Code Editor. Find the following lines,

require __DIR__.’/../bootstrap/autoload.php’;
$app = require_once __DIR__.’/../bootstrap/app.php’; 

Modify them by adding your project folder’s name right before the bootstrap folder name (~/MyAwesomeProject/bootstrap/…) as follows, to reflect the changes in file structure.

require __DIR__.’/../MyAwesomeProject/bootstrap/autoload.php’;
$app = require_once __DIR__.’/../MyAwesomeProject/bootstrap/app.php’;

If your project has views (blade files) which does not require database access, you can now check those views on the browser. To complete the deployment, we need to connect our database to the project as well.

(04) Connect Databases

Assuming you are using Laravel with MySQL (which is the most common stack preferred by many), I will explain how to connect your MySQL database with the deployed project.You have to first create your database on your Shared hosting server. Open your cPanel and go to MySQL® Databases page.

Under Create New Database section, give your database a name and create database. Page will refresh and under Current Databases section, you will see your new database is also listed. Now go to MySQL Users section and create a database user. Your page will refresh and you will see your new user listed under the Current Users section. Now go to the Add User To Database Section and select your new user and new database, so that your user now can access and modify your database. Now open the .env file inside your project folder (~/MyAwesomeProject/.env) and update the following fields with new database information;


(05) Create Database Tables

When we locally run our Laravel project, we can simply migrate our tables using php artisan migrate. But on Shared Hosting, most of the times you don’t get SSH access and will have to create databases manually. To do this, come to the cPanel and go to phpMyAdmin. You will be redirected to your familiar phpMyAdmin interface. If you see your new database, then you are good to go. Now our plan is to, get a copy of our local database as a SQL file and import it to our database on Server. So that we can get a database with tables and data which is exactly similar to the database on our local machine.To get a copy of your local database, go to your local database on phpMyAdmin (http://localhost/phpmyadmin) > Export > Select SQL as format > Go. Here now we have a copy of our local database in SQL format. Before importing this SQL file to our online database, make sure you locally open the SQL file and add the line

USE myawesomeprojectdb; 

at the very beginning of the SQL code. Otherwise, when exporting the SQL, phpMyAdmin will not know on which database, it should run those SQL commands.Now go to phpMyAdmin on your online server > Import > Choose File > Browse and select your locally-made SQL file > Go. If you follow the steps correctly, your online database now must be equal to your local database. With that, the deployment of your awesome Laravel project is complete. Now you can visit your awesome web application and check whether all the URLs and links are working.

Finally you could successfully deploy your first Laravel project on an online server!! Congratulations, and welcome to the community of Laravel Web Artisans!

comments powered by Disqus