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).
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.
The banner confirms the test account;
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.
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 folder. This is important. When you first login, you will be landed in the htdocs folder. You need to move up a level (as below).
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.
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;
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.
Add a user. Make sure the DBO flag is checked. Remember the password and store it in Lastpass or similar.
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.
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