Setting up Laravel application on shared web hosting

This is a standard Laravel deployment issue that I experience regularly. I try to write down here a simple cheatsheet describing steps how to solve it.  We need only two things before we can dive into:

  1. Git repository with our Laravel project
  2. Shell access to our web hosting

1. Cloning project on our hosting

First, SSH into your server. Next we are going to clone the Laravel project from the git repository inside the target directory. For example if we want to create an application on a new subdomain using github repository it could look like this:

cd sub
git clone https://github.com/<author>/<repository>
cd <repository>

2. Setting up Laravel

Create configuration file .env inside project directory. You can copypaste it from localhost using vim command vim .env. Then, we have to download project dependencies by using composer update.

3. Setting up .htaccess

Last step is to create .htaccess file within our project directory, so our web application is served from root/public directory instead of root. Our .htaccess will look like this:

DirectorySlash Off
Options +FollowSymlinks -Indexes -MultiViews
AddDefaultCharset utf-8
DirectoryIndex /public/index.php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1 [L,QSA]
</IfModule>

It simply tells the server that all urls accessing our project will be rewritten with /public/ inside them. Whole query string is then appended after the URI (using QSA flag).

Voilà!

Leave a Reply

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