May 25, 2017

How to Run Laravel Application on a Shared Hosting

Few of my friends asked me how to upload and run a Laravel application on a shared, non-VPS, hosting. So I thought of writing the answer to their question in this article. If you happen to be asking the same question as my friends, welcome and follow along..

Note: this tutorial has been tested on a shared hosting with CPANEL. Feel free to ask me on the comment section if you have other than CPANEL shared hosting services and this tutorial doesn't work.

Ok, here we go..

Step 1. Login to your CPANEL


CPANEL Login Form

Make sure you entered the correct credentials. Most hosting companies will disable your account or block your IP address upon several consecutive login failure. The hosting company whom I subscribed to will block my IP address after my third failure login attempts.

If this actually happens, maybe because you forgot your password, just contact your hosting company. They will gladly help to solve your problem.

Step 2: Open the File Manager

After succesfully logged in to your CPANEL, you will find a lot of features displayed on your CPANEL dashboard. If you are new to CPANEL, don't be overwhelmed, just click on the File Manager button/icon.


Next, click on the "Add Folder" button. This button is located somewhere on the upper section of the file manager page.


When a "New Folder" menu pops out, type the word "core"inside the New Folder Name input box. Make sure that the second input box which says "New Folder will be created in" is empty. This will create the core folder on the root folder of your UNIX directory (you can confirm this by seeing that the core and public_html are located at the same folder)


Once the core directory has been created, we are ready to upload from local project to shared hosting via FTP.

Step 3: Upload Files

We are going to do the upload in two parts. The first part is to upload all of files and folder, except the public directory, into the core directory we have created in the previous step. Use an FTP client and connect to your hosting service's FTP server. I like to use FileZilla, since it's free and seazy to use.

Copy all but the public local direcotry to the remote core directory

Once we are done uploading to the core directory, we will continue to the second part. Open the public directory on your local machine, and open the public_html directory on your remote server. Now upload all files and folders inside the public local directory to the public_html remote directory.

Upload all files inside public to public_html

Remember, do not upload any files or folder outside the local public directory to public_html, because the public_html directory is accessible through the internet. If you accidentally upload, for example the .env file, then your database, SMTP, or any other crucial credentials might be exposed. Immediately change your credentials if this happens.

Also, according to the Laravel installation guide, you have to allow the storage and the bootsrap/cache directory to be writable and executable.

Step 4: Changing Permissions for storage and bootstrap/cache

To change permission for the the storage directory, do this:
  1. Open the core directory.
  2. Right-click on the storage directory
  3. Click on the File Permission that shows up on the pop-up menu. 
  4. Type in the lucky seven number (777) on the "Numeric Value" input box. 
  5. Make sure that the "Recurse into subdirectories" checkbox is checked.
  6. Select the "Apply to all files and directories" radio button
  7. Click the OK button,
Right-clik storage, and click File Permissions

Give all Read-Write-Execute permission recursively

To change permission for the the bootstrap/cache directory, do this:
  1. Open the core directory, then get inside the bootstrap directory.
  2. Right-click on the cache directory
  3. Click on the File Attributes that shows up on the pop-up menu. 
  4. Type in the lucky seven number (777) on the "Numeric Value" input box. 
  5. Make sure that the "Recurse into subdirectories" checkbox is checked.
  6. Select the "Apply to all files and directories" radio button
  7. Click the OK button,
Right-click bootstrap/cache, and select File Attributes

Give all Read-Write-Execute permission recursively

Great, all file permissions has been given. Now it's time to tweak the index.php file.

Step 5: Edit the index.php File

Since we separate the application into two directories, core and public_html, we need to tell Laravel that some directories are no longer in their default location. The way we tell this fact to Laravel is to open the index.php file located inside the public_html directory.

From the CPANEL file manager, click the public_html directory on the left directory tree panel. Then on the file lists on the right, click on the index.php file.


Next click the Edit button somewhere on the upper menu.


Next, find this line:
require __DIR__.'/../bootstrap/autoload.php';
and change it to become like this
require __DIR__.'/../core/bootstrap/autoload.php';
Similarly, do the same for this line:
$app = require_once __DIR__.'/../bootstrap/app.php';
and change it to become like this
$app = require_once __DIR__.'/../core/bootstrap/app.php';

Final Step: Open Your App in a Browser

The last step is to simply open your favorite web browser, and type in your application's URL. Go then, give it a try..!