Difference between revisions of "Laravel Shared Hosting"

From HaFrWiki
Jump to: navigation, search
(Created page with "{{TOCright}} Laravel is not suited for Shared Hosting Servers. However it is possible according an article on Laravel Article <ref>https://laravelarticle.com/deploy-laravel-o...")
 
m (Update Database Details)
 
(12 intermediate revisions by the same user not shown)
Line 16: Line 16:
 
</pre>
 
</pre>
 
So you can not deploy or run artisan commands like {{FormFCTW|9|red|bold|php artisan serve}}.
 
So you can not deploy or run artisan commands like {{FormFCTW|9|red|bold|php artisan serve}}.
 +
 +
Important differences in the steps and actions on an ITIL based development environment.
 +
* Using an {{FormFCTW|8|blue|bold|DTAP}} ('''''D'''''evelop, '''''T'''''est, '''''A'''''cceptance, '''''P'''''roduction) 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.
 +
* '''''B'''''eyond '''''C'''''ompare ({{FormFCTW|8|blue|bold|BC}} or any other diff tool) is used to make the necessary changes.
 +
* '''''F'''''ile'''''z'''''illa ({{FormFCTW|8|blue|bold|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 ==
 
== Deployment Steps ==
Line 21: Line 29:
 
# [[#Export Database|Export database from the local environment]].
 
# [[#Export Database|Export database from the local environment]].
 
# [[#ZIP or FTP|Do ZIP your Laravel project]].
 
# [[#ZIP or FTP|Do ZIP your Laravel project]].
# Create a database in your cPanel.
+
# [[#Create Database|Create a database in your cPanel]].
# Import the local exported database into a shared hosting database.
+
# [[#Import Database|Import the local exported database into a shared hosting database]].
# Upload project ZIP file to public_html folder and extract.
+
# [[#Upload Files|Upload project ZIP file to public_html folder and extract]].
# Update database details into the config file.
+
# [[#Update Database Details|Update database details into the config file]].
# Some security setup.
+
# [[#Security setup|Some security setup]].
  
 
== Remove Public ==
 
== Remove Public ==
 +
The artisan {{FormFCTW|8|blue|bold|php artisan serve}} command is not possible on the Share Host.
 +
<br>Removing the {{FormFCTW|8|blue|bold|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 <abbr title="Beyond Compare">BC</abbr> to make the first clone of the <abbr title="Development-Test">DT</abbr> &rarr; <abbr title="Acceptance/Production">AP</abbr> environment <br>without the .git-folders.
 +
* On <abbr title="Acceptance/Production environment">AP-environment</abbr>:
 +
** Cut the {{FormFCTW|9|blue|bold|index.php}} and {{FormFCTW|9|blue|bold|.htaccess}} from the project {{FormFCTW|9|green|bold|public}} folder and paste them into the project {{FormFCTW|9|green|bold|root}} directory.
 +
** Open {{FormFCTW|9|blue|bold|index.php}} and make the following changes:
 +
*** Change the line containing {{FormFCTW|9|green|bold|autoload}} into {{FormFCTW|9|red|bold|require __DIR__.'/vendor/autoload.php';}}
 +
*** Change the line containing {{FormFCTW|9|green|bold|bootstrap}} into {{FormFCTW|9|red|bold|$app}} = {{FormFCTW|9|red|bold|require_once __DIR__.'/bootstrap/app.php';}}
 +
Please check if your app is using the public on other places!
  
 
== Export Database ==
 
== 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 {{FormFCTW|8|blue|bold|database.php}} file from {{FormFCTW|8|blue|bold|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.
 +
<pre>
 +
'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'),
 +
    ]) : [],
 +
],
 +
</pre>
 +
 +
== Security setup ==
 +
Protect your {{FormFCTW|9|blue|bold|.htaccess}}, env files from direct access, and disable directory browsing for our application.
 +
For that open the {{FormFCTW|9|blue|bold|.htaccess}} file from the public_html folder and write these codes at the end of the file and save.
 +
<pre>
 +
# Disables directory browsing
 +
Options -Indexes
  
== ZIP or SFTP Files == 
+
# Protects .env file
 +
<Files .env>
 +
order allow,deny
 +
Deny from all
 +
</Files>
  
 +
# Protects htaccess FILE
 +
<Files .htaccess>
 +
order allow,deny
 +
Deny from all
 +
</Files>
 +
</pre>
  
 
== See also ==
 
== See also ==
Line 42: Line 120:
  
 
[[Category:Index]]
 
[[Category:Index]]
 +
[[Category:PHP]]
 +
[[Category:Programming]]
 +
[[Category:Tools]]

Latest revision as of 15:22, 16 July 2022

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.