Laravel 5.4 Install

This is the installation of Laravel 5.4 on an Ubuntu Server 16.04 LTS. Laravel requires that you run Apache, MySQL and PHP. In this installation, I’m using the following versions: Apache 2.4.18, MySQL 5.7.17 and PHP 7.0.15. The installation of Laravel has changed over the years. I wished the installation was simpler. You can try Forge, but it’s not free. It also requires that you have an account at Linode, Digital Ocean or AWS. Forge costs $15 per month and Forge Plus is $30 per month. If you don’t want to pay, I’m afraid you are going to have to install Laravel yourself. Laravel installation may vary based on the distro you’re using. These instructions are for the Ubuntu Server 16.04 LTS.

Run Update

sudo apt-get update
sudo apt-get upgrade

Install Apache

sudo apt-get install apache2
sudo a2enmod rewrite
sudo service apache2 restart

Install MySQL

sudo apt-get install mysql-server
sudo mysql_secure_installation

Install PHP

sudo apt-get install php libapache2-mod-php php-mbstring php-zip php-xml php-mysql

Install Composer

sudo curl -sS https://getcomposer.org/installer | sudo php — —install-dir=/usr/local/bin —filename=composer
# you may have to move composer manually if it doesn't work
sudo mv composer.phar /usr/local/bin/composer
# check if composer is accessible globally
composer -V

Install Laravel Installer

composer global require "laravel/installer"
# Using alias instead of $PATH
echo 'alias laravel="~/.composer/vendor/bin/laravel"' >> ~/.bashrc
source ~/.bashrc
# add source ~/.bashrc to .profile to load automatically
echo 'source ~/.bashrc' >> ~/.profile
# restart apache
sudo service apache2 restart
# check if laravel is accessible globally
laravel

Install New Laravel Project

# set permissions
cd /var/www/
sudo chown -R ubuntu:ubuntu /var/www/html
sudo chmod -R 755 /var/www/html
# install new project
cd /var/www/html
laravel new project
# give apache permissions 
sudo chgrp -R www-data /var/www/html/project/storage
sudo chgrp -R www-data /var/www/html/project/bootstrap/cache

Configure Apache

cd /etc/apache2/sites-available
sudo cp 000-default.conf project.conf
sudo nano project.conf
#
<VirtualHost *:80>
  ServerName local.project.com
  ServerAdmin admin@project.com
  DocumentRoot /var/www/html/project/public
  <Directory "/var/www/html/project">
    AllowOverride All
  </Directory>
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
#
sudo a2ensite project.conf
sudo a2dissite 000-default.conf
sudo service apache2 restart

Finally, you can now access Laravel from the browser from the server IP address or domain.

Laravel Forge Edit .env

Laravel Forge was recently upgraded with several new features. You can now edit your .env file directly from within Forge. It’s easier and faster than adding each environmental variable. In addition, you can also create and manage Nginx load balancing servers to balance your application load. With Forge, you can now deploy a new server in just minutes from any of the three cloud hosting service providers: Linode, Digital Ocean and now Amazon Web Services.

Migrations in Laravel

Migrations in Laravel is like having version control of your database. It gives you the ability to create, modify and share a schema, as well as drop tables. Since it has version control, you can migrate and rollback your database changes with just a couple of CLI commands. Here’s a couple of examples.

To migrate:

$ php artisan migrate

To rollback:

$ php artisan migrate:rollback

In addition, you can also create a new migration table.

$ php artisan make:migration create_users_table --create="users"

It will create a file similar to this:

app/database/migrations/2015_01_01_213247_create_articles_table.php

which you can modify and add fields.

PSR-2 Coding Style Guide

Laravel recently started using the PSR-2 style guide. PSR-2 is a coding style agreed upon by many PHP framework developers. It’s an extension of the original PSR-1, which basically has 3 main conventions.

  1. Classes are upper Camel case.
  2. Methods are lower Camel case.
  3. Constants are written in caps like VERSION_NUMBER.

PSR-2 expands this by using these standards

  1. Indents are 4 spaces instead of tabs.
  2. Keep line lengths at 80 characters. Soft limit of 120 characters.
  3. Use one blank line after a namespace declaration. Use a blank line after a group of declarations.
  4. Opening braces for classes MUST go on the next line. Closing brace must be on its own line.
  5. Opening braces for methods must go on the next line, and a separate line for the closing brace.
  6. Closures must have a space after the function keyword.
  7. Never use the var keyword, this is used with  JavaScript and would cause some confusion.
  8. Control structures, like IF, WHICH, FOR, and FOREACH must have one space before the condition parenthesis.
  9. The case keywords should be indented from the switch keyword.
  10. Keywords, like true, false, and null show be in lower case.

See examples.

Laravel 5.1 with Forge

Laravel 5.1 was released in June 9, 2015. So, I updated my development environment with version 5.1 and updated it to GitHub. From there, I spun a server via Forge at Digital Ocean. Then, I deployed my development repository via GitHub.

However, I faced two issues:

  1. I was getting an error saying that there is not enough memory. The simple fix is to do a composer update in your dev environment and then upload your changes to GitHub. If you have automatic deploy turned on, then the changes will be updated on the server.
  2. The other error was a missing app key. You can fix this by simply running artisan key:generate on the server. You must have a .env file on the Laravel directory. I believe it’s under /home/forge/default. You can create one or copy the .env.example.

Hopefully, I save you some time with troubleshooting if you run into these issues.