Search Google

Monday 27 April 2015

[Tutorial] Installing & configuring laravel on shared hosting (Hostgator)

 




Connect to Hostgator's terminal using Putty or some other SSH tool. Once logged in, type 'pwd'. You should see something like this:
-jailshell$ pwd
/home2/CpanelUsername
This is where we want to install Laravel. But in order to do so, we must install Composer. However, Composer requires PHP 5.3.2+ to run. To check the PHP version, type 'php -v' (remember, the terminal PHP can be different from the PHP configuration for your website). Hopefully you'll see someting like this:
-jailshell$ php -v
PHP 5.5.6 (cli) (built: Dec  4 2013 13:50:26)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
PHP 5.5.6! You probably won't. In fact, you'll probably get PHP 5.2, just shy of the requirements to install composer and Laravel. In order to remedy this, create a new file called '.bashrc'. Kind of like .htaccess, the dot makes the file hidden. In the .bashrc file, put in the code:
alias php='/opt/php55/bin/php'
alias composer='/home2/CpanelUsername/composer.phar'
export PATH="/opt/php55/bin:$PATH"
Also, create a file called '.bash_profile'. In it, put:
[[ -s ~/.bashrc ]] && source ~/.bashrc
We've created several aliases. The php alias allows us to simply type 'php' instead of the lengthy '/opt/php55/bin/php' in order to access PHP 5.5. The composer alias allows us to type 'composer' instead of 'composer.phar' before every command. Also, since, we've created a direct path to it, composer.phar can be accessed anywhere, even if the file is not in the Laravel directory. The export PATH line allows Laravel to correctly path to the right PHP during its installation. Aliases do not apply during the installation process. Lastly, the bash_profile file makes it so that the .bashrc file fires up every time you start the linux console.
Upload both of them in your CpanelUsername directory, so it looks something like this:
CpanelUsername/
    access-logs/
    etc/
    perl5/
    public_html/
    tmp/
    www/
    .bash_history
    .bashrc        <-- here
    .bash_profile  <--here
Your directory may have more files and folders, and I've purposefully ommited the hidden ones. Now, you must activate the bash files. In your console, type: 'source ~/.bashrc' and then check your php version by typing 'php -v'.
-jailshell$ source ~/.bashrc
-jailshell$ php -v
PHP 5.5.6 (cli) (built: Dec  4 2013 13:50:26)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
Awesome! We're good to go. Oh, while we're on the topic of PHP 5.5, we want to make an '.htaccess' file in our public_html folder. Create a file called '.htaccess' and write:
AddHandler application/x-httpd-php55 .php
This will enable PHP 5.5.6 on the website. Eventually, after we integrate Laravel's .htaccess, it should look something like this:
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

AddHandler application/x-httpd-php55 .php
Time to install Composer. In your terminal, type:
-jailshell$ curl -sS https://getcomposer.org/installer | php
The above process will download the necessary composer files in the current location. After that, install Laravel by running the following command:
-jailshell$ composer create-project laravel/laravel --prefer-dist
Now your directory should look something like this:
CpanelUsername/
    .composer/
    access-logs/
    etc/
    laravel/
    perl5/
    public_html/
    tmp/
    www/
    .bash_history
    .bashrc        
    .bash_profile 
    composer.phar
If you open the laravel folder, you'll see a folder structure like this:
CpanelUsername/
    .composer/
    access-logs/
    etc/
    laravel/
        app/
        bootstrap/
        public/
        ...
    perl5/
    public_html/
    tmp/
    www/
    .bash_history
    .bashrc        
    .bash_profile 
    composer.phar
You want to take the contents from the public folder and move them to the public_html folder. You can delete the public folder. It should look like this:
CpanelUsername/
    .composer/
    access-logs/
    etc/
    laravel/
    perl5/
    public_html/
        packages/
        .htaccess
        favicon.ico
        index.php
        robots.txt
    tmp/
    www/
    .bash_history
    .bashrc        
    .bash_profile 
    composer.phar
At this point, this starts to look a lot like Dries Vint's excellent guide. We are doing the first option. Anyway, open the 'laravel/bootstrap/paths.php' file and correct the bootstrap path like this:
28
29  'public' => __DIR__.'/../../public_html',
30
Do the same in 'public_html/index.php' and change the following lines:
20
21  require __DIR__.'/../laravel/bootstrap/autoload.php';
22

//

34
35  $app = require_once __DIR__.'/../laravel/bootstrap/start.php';
36
And that is it! Fire up your website and enjoy the lovely 'hello' screen :).

 

 

 

 

 

 

 

 

 

 

 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Uploading your base files

Laravel has been designed so that you can keep your app files out of the public_html folder which adds extra security for your app. So what I suggest is that you create a folder in your root home directory, name it something like ‘app_base’, and put everything in there, assuming you have downloaded Laravel as a zip file from github. We will work on that folder for the next steps.
Your next option is to use git command via ssh. Hostgator has git installed for you.
login via ssh using the command bellow:
ssh -p 2222 username@server-ip-address
Once logged in, and you are in your root home folder, type git just to make sure you have it installed.
Next, clone the Laravel repository in the app_base folder using this code:
git clone https://github.com/laravel/laravel.git app_base
git will start downloading the framework for you.
Once finished, we are ready to install composer’s phar file. getComposer.org has a command line that you can use to download composer.phar into your app_base folder.
curl -sS https://getcomposer.org/installer | php
Obviously this won’t work, composer will tell you that it needs php 5.3.x to work.

PHP 5.3 is not used by default

Hostgator has php 5.3.7 installed on every shared hosting account. but to use it you need to do 2 small changes. The first is to add some code to your .htaccess file, and the second change is to use another command in your terminal to reference php 5.3.

Create an .htaccess file in your app_base folder

Yes you are right, there is no .htaccess in your app_base folder, but we will need to create one and add some code to it.
once created (And don’t forget the dot in front of htaccess), put this code in:
# Use PHP 5.3
AddType application/x-httpd-php53 .php

Using php 5.3

Now your app will be using php 5.3 instead of 5.2. but this is not everything, now when trying to use php in your terminal (or putty or whatever you are using) via SSH, you need to use/opt/php53/bin/php and it will work. Now let’s get back to install composer.
here is a little trick if you want to use php instead of/opt/php53/bin/php, add an alias:
alias php="/opt/php53/bin/php"

Installing Composer

Using ssh, cd into your app_base folder, and install composer using this code:
curl -sS https://getcomposer.org/installer | php
or
curl -sS https://getcomposer.org/installer | /opt/php53/bin/php
Composer will be installed without a problem if you followed all the steps from the beginning.
do a little ls just to verify that the composer.phar file is there.

Install the framework dependencies using composer

This is the fun part, all you have to do is shoot a composer install on your terminal, and every single package used by laravel will be downloaded and installed for you.
/opt/php53/bin/php composer.phar install
Or
php composer.phar install
If you notice that the last artisan command fails, don’t worry, you can run it yourself after composer is done installing.
The command is simply php artisan optimize
Now you have everything installed. Nothing will be accessible yet from the web. because we still need to move the public folder’s content to the public_html folder, so that you can point to that folder from the web.

Where to put things

Create a folder inside your public_html folder, name it say DEMO. And move the content of your laravel’s public folder into the demo folder.
If you point to the demo folder from the web, it won’t work. this is because you still need to update your paths.php file to include the correct paths to different parts of the application.

Update your public/index.php && paths.php files to include the correct paths

In order for the application to know where your App is, open the index.php file inside DEMO folder you created and change all the paths you find there. You need to provide valid paths to your bootstrap files.
change this:
require __DIR__.'/../bootstrap/autoload.php';
to
require __DIR__.'/../app_base/bootstrap/autoload.php';
And so on.
Now open your bootstrap folder inside your laravel app, and change the public path inside paths.php
Change this
'public' => __DIR__.'/../public'
to
'public' => __DIR__.'/../../public_html/demo'
If you got here, everything should work now, point your browser to your demo folder and see if your laravel app boots up with a default view saying YOU HAVE ARRIVED!
I hope this was helpful for you. let me know if you get stuck.


............................................

No comments:

Post a Comment