Shared Hosting


Install Laravel 5.2 on shared hosting from Fasthosts

Fasthosts is a popular hosting provider in the UK although shared hosting should be avoided if possible for Laravel projects.  Unless you are happy to accept unpredictable response times, you will always be better off with dedicated hosting, and my recommendation is Digital Ocean and clicking through here will start your account with $10 credit (an affiliate link).

When working with Fasthosts, you will notice that most configuration changes are queued for execution. If you are struggling to connect to the database or to ftp, leave it 10 minutes and try again.

In this post, you will learn how to move local copy of your site to Fasthosts and then to replace the htdocs folder with a symlink that points to the Laravel public folder.

1. Create a linux hosting account

Here I am creating an account for the project ‘gxplan.com’.  Choose Linux hosting with mysql database.

Since my domain name is not yet fully registered, request a test url. Through this, you can reach your site even though the correct URL is not yet assigned.

Since DNS is not yet configured, click Enable under test domain

Since DNS is not yet configured, click Enable under test domain

The banner confirms the test account;

Note the test URL. You will use this later to access the site

Note the test URL. You will use this later to access the site

2. Setup FTP

The default web folder for public content at Fasthosts is called htdocs. This will cause us some problems but we can tackle that in a moment.  The first step is to upload your dev install onto the web server.

Go into the FTP settings and create a password for the hosting account.  Make sure this is a strong password and use a tool like Lastpass to keep a record of the password chosen.

Create FTP account and set the password.

Create FTP account and set the password.

 

Once you have the password set (give it a few minutes) then configure your FTP client application to use these credentials.  If, like me, you have not yet configured the domain name, you will need to use the server IP address rather than the Hostname.

Copy the entire project folder into the folder above the htdocs folderThis is important.  When you first login, you will be landed in the htdocs folder.  You need to move up a level (as below).

The default folder structure

The default folder structure

Copy all your files into the root folder.

Make sure the .htaccess file is uploaded to the public folder. My FTP client excludes this by default.

Note:

I had to remove the Multiviews option in htaccess as this was not supported by Fasthosts (here I have commented out the line with #)

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        # Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

3. SSH

Although it is possible to ssh into the server, the version of PHP available to you in SSH shell is 5.2 (at the time of writing). This means that it will not be possible to run any artisan commands from the SSH shell.

You will use SSH though, so create a password and if necessary (for Windows) install PuTTY or other ssh client. Mac users can run ssh directly from Terminal.

Create a password for your SSH user

Create a password for your SSH user

Once SSH is setup, connect to the server.

The task now is to create a symbolic link called htdocs, pointing to the public folder.  A symbolic link redirects one file or folder to another file or folder.  By this, when the web server tries to look in htdocs for the index.php file, it will be directed instead to the public folder.

  • Change to the htdocs folder and delete the cgi-bin folder.
  • Move up a level and delete the htdocs folder.
  • Create a symlink from htdocs to public
$ cd htdocs
$ rmdir cgi-bin
$ cd ..
$ rmdir htdocs
$ ln -s public htdocs

htdocs now points to the public folder, meaning you can FTP synchronise your development machine with the shared hosting whenever you need to push updates.

Remember to exclude the hidden .env file from the FTP folder from future synchronisation since it will contain your development settings and not your production settings.

4. Setup MySQL database

You will almost certainly use a database.  If so, request a new database from the control panel;

Setting up a mysql database

Setting up a mysql database

After requesting the database, it will take a few minutes for it to be installed.  Wait for this to be completed before adding a database user.

Make a note of the database IP address. It is not 'localhost'

Make a note of the database IP address. It is not ‘localhost’

Add a user. Make sure the DBO flag is checked. Remember the password and store it in Lastpass or similar.

Add a user. Make sure dbo is selected

Add a user. Make sure dbo is selected

Once the database is setup, you can log into phpMyAdmin using the username and password just created.  Having previously backed up your development database, you can now restore it to your new host.

Importing a database backup into your new database

Importing a database backup into your new database

 

Completed upload

Completed upload

The upload is completed, and my basic install of migrations, password_resets and users is visible in the gxplan database.

5. Edit the .env file on the Fasthosts webserver

The final step is to configure the .env file on the FTP server.

Compared to the development server copy;

  • delete the APP_ENV line (it will default to production)
  • delete the APP_DEBUG line (it will default to false)
  • Configure the database credentials according to the settings from the new server

6. Done

If you have managed to follow this far, then you should now have a working copy of your application, hosted at Fasthosts

Thats it! My test site is published and working

Thats it! My test site is published and working


Deploy Laravel 5 on shared hosting from Heart Internet

For trial sites, and quick to deploy, low traffic tools, its perfectly possible to host your site at Heart Internet using subdomains. Although these instructions are specific to Heart, they will work for other hosts, with and without subdomains.

Wait.....
Before you do anything – check that your host is providing PHP V5.4 or better (Laravel 5.0) or PHP 5.5.9 or better (Laravel 5.1 / 5.2)

1. Request subdomain setup

Heart run their subdomains on the same server. A folder is created in the public_html folder for the subdomain. For instance, I’m creating a service that will respond to dj3.mydomain.com  on the mydomain.com server, there will be a folder called public_html/dj3

After requesting the subdomain, wait an hour for the DNS to all be in place.

2. Upload your site

Your laravel code base should be located in a folder that is not accessible from the web.

Create a new folder in your root folder based on the name of your subdomain.  This is incase you want to install another application, you can put each backend in its own space.  Here i have used the name dj3core

dj3 directory

FTP everything except your public folder into the back-end folder that you created (dj3core in my example)

FTP the contents of your public folder into the subdomain folder (dj3 in my example)

Make sure that you copy the hidden file .htaccess also into your subdomain folder.  Do not put it in the root or the public_html folder

3. Fix the paths in the index.php file

You need different paths in the index.php file to what you have probably been testing with, so before uploading or inplace on the hosted server, edit the index.php (the one in the subdomain, eg /public_html/dj3/index.php) file as follows;

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels nice to relax.
|
*/

// require __DIR__.'/../bootstrap/autoload.php';
require __DIR__.'/../../dj3core/bootstrap/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

// $app = require_once __DIR__.'/../bootstrap/app.php';
$app = require_once __DIR__.'/../../dj3core/bootstrap/app.php';

I have retained the original lines, and added the modified lines below.

Compared to the distribution, the application can be found up two directories (../) and in the dj3core folder.

Thats it! Your site should now be working in the subdomain dj3.mydomain.com

4. Problems?

If you are still having problems, check that the storage folder is writable.

At the time of writing, I have not tested email, but I don’t expect there to be a problem.

If you are using the HTML and URL helpers make sure the url is set correctly in the config/app.php file.

 


Help, my host does not support CRON jobs

If you are working on a shoestring and using a shared host for your latest Laravel 5 project, you may want to setup some scheduled jobs. Laravel 5 has a great scheduler built in, but it needs a kick every minute for it to determine if it is time to run the job.

Through a third party service such as cron-job.org it is possible to provide this kick to the Laravel 5 Scheduler.

1. Create an account at cron-job.org

Accounts are free and permit you to schedule a task as frequently as once per minute

2. Create a route in your application to kick the laravel scheduler

    //trigger the scheduler
    Route::get('/hshhdyw7820037lammxh29' , function(){
        Artisan::call('schedule:run');
        return 'OK';
    });

Here I have used a random string for the path so that it is not accidentally ‘found’. If it would be an issue if your task is triggered twice, you might want to protect it further such as checking for the request coming cron-job.org’s IP address.

3. Add this route to the cron-job.org schedule

cron-tab

 

Other thoughts

By triggering the scheduler this way rather than just running the job directly means that you can then use the power of the Artisan scheduler.  Check out Eric Barnes intro to using the scheduler.

One thing not covered by Eric or the documentation is the ability to run a task every few minutes (5 minutes is catered for).

This example runs the ReplayServiceProvider every two minutes.

    $schedule->call('App\Providers\ReplayServiceProvider@feedData')->cron('*/2 * * * *');