Laravel Shared Hosting

From HaFrWiki42
Jump to navigation Jump to search

Laravel is not suited for Shared Hosting Servers. However it is possible according an article on Laravel Article [1]

This article focuses on the shared hosting on One Hosting [2]

Introduction

Shared hosting is very popular (Budget hosting), but will not provide the user from ssh access. On One you have ssh access, but the possibility of running commands is limited.

Welcome on the One.com SSH service 

This service offers an environment for you to interact with your files in your
web space, either directly using your shell, or by using secure file transfer
protocols such as SFTP, SCP, RSYNC over SSH.
...  

So you can not deploy or run artisan commands like php artisan serve.

Important differences in the steps and actions on an ITIL based development environment.

  • Using an DTAP (Develop, Test, Acceptance, Production) environment changes the way to do the required actions.
    • In this article we do and the Acceptance (Local environment is equal to the Production env).
  • ZIP is not used, instead the D/T-environment is compared with the A/P-environment.
  • Beyond Compare (BC or any other diff tool) is used to make the necessary changes.
  • Filezilla (Fz) to make the changes on the Production environment using SFTP.
  • Please note that the required changes to make Laravel running are only made on A/P-environment.

Deployment Steps

  1. Remove the public from the URL.
  2. Export database from the local environment.
  3. Do ZIP your Laravel project.
  4. Create a database in your cPanel.
  5. Import the local exported database into a shared hosting database.
  6. Upload project ZIP file to public_html folder and extract.
  7. Update database details into the config file.
  8. Some security setup.

Remove Public

The artisan php artisan serve command is not possible on the Share Host.
Removing the public word from the URL enables access of your Laravel project as normal PHP-project. The actions are:

  • Create a deployment of the project
    • Use BC to make the first clone of the DTAP environment
      without the .git-folders.
  • On AP-environment:
    • Cut the index.php and .htaccess from the project public folder and paste them into the project root directory.
    • Open index.php and make the following changes:
      • Change the line containing autoload into require __DIR__.'/vendor/autoload.php';
      • Change the line containing bootstrap into $app = require_once __DIR__.'/bootstrap/app.php';

Please check if your app is using the public on other places!

Export Database

Exports the schema/tables of your local Database into a uploadable file.

ZIP or SFTP Files

There are 2 options:

  • Create a ZIP from the entire project files without the .git folder(s).

or

  • Use Beyond compare
  • Filezilla (or equivalent SFTP tool) and

Create Database

  • Create a new Database and import the Tables

Import Database

  • Use a mysql backup file from your local schema
  • Import the data using phpadmin of your provider.

Upload Files

  • Use filezilla to upload the entire website into your provider

Update Database Details

Open database.php file from config folder and update your database name, username & password into the database.php file and save. Do not put your database details or any sensitive credentials in the env file for shared hosting.

'mysql' => [
    'driver' => 'mysql',
    'url' => '',
    'host' => '127.0.0.1',
    'port' => '3306',
    'database' => 'mydatabase',
    'username' => 'mydbusername',
    'password' => 'here_password',
    'unix_socket' => '',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
    'options' => extension_loaded('pdo_mysql') ? array_filter([
        PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
    ]) : [],
],

Security setup

Protect your .htaccess, env files from direct access, and disable directory browsing for our application. For that open the .htaccess file from the public_html folder and write these codes at the end of the file and save.

# Disables directory browsing
Options -Indexes

# Protects .env file
<Files .env>
order allow,deny
Deny from all
</Files>

# Protects htaccess FILE
<Files .htaccess>
order allow,deny
Deny from all
</Files>

See also

top

Reference

top

  1. https://laravelarticle.com/deploy-laravel-on-shared-hosting LaravelArticle], Deploy Laravel on shared hosting.
  2. One.com, Shared Hosting Provider.