Tuesday, June 30, 2015

Laravel on Debian Wheezy

LAMP

First we need LAMP packages installed.
Laravel requires PHP >= 5.5.9 so we will install php56.

Add package repositories to apt sources.
Edit sources.list
$ vi /etc/apt/sources.list
and add these two lines:
deb http://packages.dotdeb.org wheezy-php56 all
deb-src http://packages.dotdeb.org wheezy-php56 all

Add dotdeb.gpg key so apt can authenticate packages from added sources.
$ cd
$ wget http://www.dotdeb.org/dotdeb.gpg
$ apt-key add dotdeb.gpg
$ apt-get update

Install Apache, MySQL and PHP
$ apt-get install mysql-server mysql-client
$ apt-get install apache2
$ apt-get install php5 libapache2-mod-php5 php5-mcrypt

Enable rewrite apache modul
$ a2enmod rewrite
$ /etc/init.d/apache2 restart

Confirm that php is working well with apache by creating info.php file:
$ vi /var/www/info.php
add this line:
<?php phpinfo(); ?>
Access inside browser: http://localhost/info.php

Laravel

# To install laravel we need composer:
$ sudo curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

* Use composer to install laravel:
$ composer global require "laravel/installer=~1.1"

* Add laravel bin to your PATH so you can use laravel executable from any location. Edit bash_profile file:
$ vi ~/.bash_profile
and add these lines:
PATH=$PATH:~/.composer/vendor/bin
export PATH

* Create new base for web application
$ cd ~/NetBeansProjects
$ laravel new LaravelDemo

# Or we could skip installing laravel and using it's executable to create base for web application (steps marked with asterisk *), and go ahead do all that with composer directly:
$ rm -rf ~/NetBeansProjects/LaravelDemo
$ cd ~/NetBeansProjects
$ composer create-project laravel/laravel LaravelDemo --prefer-dist

# Create Apache VirtualHost by:
$ vi /etc/apache2/sites-available/laraveldemo.org
and add these lines:
ServerAdmin webmaster@laraveldemo.org
ServerName  laraveldemo.org
ServerAlias *.laraveldemo.org

DocumentRoot /home/{user}/NetBeansProjects/LaravelDemo/public

        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all

ErrorLog ${APACHE_LOG_DIR}/laraveldemo_error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/laraveldemo_access.log combined

# Enable virtual host:
$ a2ensite laraveldemo.org

# Add virtual host to hosts file by editing:
$ vi /etc/hosts
add this line:
127.0.0.1 laraveldemo.org

# Restart apache
$ service apache2 restart

# Create simple /about route and appropriate controller, action and view
$ cd ~/NetBeansProjects/LaravelDemo
$ vi app/Http/routes.php 
add this line to routes.php:
Route::get('about', 'PagesController@about');

# Create PagesController by:
$ php artisan make:controller PagesController --plain
$ vi app/Http/Controllers/PagesController.php
 
add about() method to PagesController.php:
    public function about() {
    return view('about');
    }

# Create about view by editing about.blade.php:
$ vi resouces/views/about.blade.php
add this line:
    Hello World

# Make local configuration
$ copy .env.example to .env

# Generate app key
$ php artisan key:generate

# Set Directory Permissions
$ cd ~/NetBeansProjects/LaravelDemo
$ chgrp www-data -Rv storage/
$ chmod g+w -Rv storage/
$ cd bootstrap/
$ chgrp www-data -Rv cache
$ chmod g+w -Rv cache

Test it by pointing browser to:
http://laraveldemo.org/about

Create database
mysql> create database laraveldemo;
mysql> CREATE USER 'laraveldemouser'@'localhost' IDENTIFIED BY 'laraveldemopass';
mysql> GRANT ALL ON laraveldemo.* TO 'laraveldemouser'@'localhost';

# Add mysql authentication data to .env
$ vi .env
change these lines:
DB_HOST=localhost
DB_DATABASE=laraveldemo
DB_USERNAME=laraveldemouser
DB_PASSWORD=laraveldemopass

No comments: