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


Leave a comment

Your email address will not be published. Required fields are marked *

5 thoughts on “Install Laravel 5.2 on shared hosting from Fasthosts

  • olidev

    Isn’t it better to avoid shared hosting? Since it has serious drawbacks regarding security and performance, a VPS, like DigitalOcean, is a better option. If a person doesn’t have sysadmin skill to start a DO droplet, then they can instead use managed platform, like Cloudways, to setup Laravel on DO server.

    • Mark Post author

      I run a number of sites on shared hosting, and there are NO security issues if implemented as described, and I could easily argue that this is more secure than a badly configured VPS. Performance is acceptable for most small sites where you have 5-10 concurrent users. For sites that get up to 200 visits per day this would be fine. Publishing the article does not argue against VPS or dedicated servers for larger sites.

  • Jack

    Rather than placing all your files into the root, could you create a folder such as ‘LaraApp’ and then point the symlink to LaraApp/public?
    Also, thanks for posting this- I hadn’t worked with Fasthosts before & hadn’t come across htdocs.

  • Mark Post author

    so if you move the laravel content ‘up’ a folder then you need to alter the two paths in the index.php file.

    if your file structure is like this;

    – LaraApp
    – LaraApp/public
    – htdocs (symbolic link)

    So now your symbolic link that is htdocs needs to point instead to LaraApp/public, and in the LaraApp/public/index.php the following lines need changing

    require __DIR__.’/../vendor/autoload.php’; change to require __DIR__.’/../LaraApp/vendor/autoload.php’;

    $app = require_once __DIR__.’/../bootstrap/app.php’; change to $app = require_once __DIR__.’/../LaraApp/bootstrap/app.php’;

    At least I think that’s correct! I cannot try it though.