London, United Kingdom

(+44) 0785.600.3479 MariusCucuruz@gmail.com

How to Deploy Laravel on Ubuntu

 

Tasks:

  1. Ubuntu 14.04 (or greater)
  2. OpenSSL, OpenSSH, Git
  3. SendMail, cURL, Perl
  4. Apache 2
  5. PHP 7.0 (7.2)
  6. MySQL
  7. Webmin
  8. Composer
  9. Laravel Framework

 

Prerequisites Note:
It’s assumed the VM already has Ubuntu installed at this stage.
See online documentation on how to spin up a virtual machine and for installing Ubuntu 14 or 16.

 

# Install OpenSSH to access this box remotely:
sudo apt-get install openssh-server openssh-client

# Install OpenSSL:
sudo apt-get install openssl

# Install Git:
sudo apt-get install git
git config –global user.email “you@example.com”
git config –global user.name “Your Name”

# Install additional packages (SendMail, python, cURL, Perl):
sudo apt-get install sendmail python perl

# Install Apache v2:
sudo apt-get install apache2
sudo apache2ctl configtest
sudo apt-get install curl
sudo systemctl restart apache2

# Install PHP 7.0:
# a) update Ubuntu and the repository packages cache
apt-get update && apt-get upgrade

# b) prepare to add the PHP repository
apt-get install python-software-properties
apt-get install software-properties-common

# add the PHP repository from Ondřej (alternative repo: “ppa:ondrej/php”)
sudo add-apt-repository ppa:ondrej/apache2

# install PHP 7.0 (and modules)
sudo apt-get install php7.0
sudo apt-get install php-pear php7.0-dev php7.0-zip php7.0-mysql php7.0-curl php7.0-xml php7.0-gd php7.0-mbstring php7.0-mcrypt libapache2-mod-php7.0
# enable PHP7.0
sudo a2enmod php7.0

# or, to install PHP 7.2
sudo apt-get install php7.2
sudo apt-get install php-pear php7.2-dev php7.2-zip php7.2-mysql php7.2-curl php7.2-xml php7.2-gd php7.2-mbstring
sudo a2enmod php7.2

# optionally install Alt PHP Cache (APC)
pecl install apcu

# restart apache
systemctl restart apache2

# test PHP
php -v

# Install MySQL 5.6+ (make a note of the root password)

sudo apt-get install mysql-server
mysql_secure_installation
mysql -u root -p

> CREATE DATABASE `my_larevel_db`;
> CREATE USER IF NOT EXISTS ‘laravel_db_user‘ IDENTIFIED BY ‘laravel_db_pass‘;
> GRANT ALL PRIVILEGES ON my_larevel_db.* TO ‘laravel_db_user‘@’localhost’ IDENTIFIED BY ‘laravel_db_pass‘;
> FLUSH PRIVILEGES;

# test new user access:
mysql -u laravel_db_user -p

 

# Installing Webmin:

# add the Webmin repository by adding the repository to /etc/apt/sources.list file:
sudo nano /etc/apt/sources.list

# and add the following line at the end:
deb http://download.webmin.com/download/repository sarge contrib

# download the Webmin key and add it:
wget http://www.webmin.com/jcameron-key.asc
sudo apt-key add jcameron-key.asc

# update packages and install:
sudo apt-get update
sudo apt-get install webmin

 

# Install Composer globally

sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/

# create alias for “composer” command:
alias composer=’/usr/local/bin/composer.phar’

# or via link:
sudo ln -s /usr/local/bin/composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer.phar

 

 

# Laravel Framework v5.2(.45)

composer global require “laravel/installer”

# make sure permissions allow for current user to write in /var/www/:
composer create-project –prefer-dist laravel/laravel MyLaravelProject “5.2.*”

# test installation is successful: 
cd MyLaravelProject
php artisan

# if you’ve been granted access to the git repository clone it now:
git init
git remote add origin https://{{your-user-name}}@bitbucket.org/{{proj-url}}/{{LaravelGitRepository}}.git
# test origin:
git remote –v
git fetch –all
git pull origin master

# double check all dependencies are installed via composer:
composer diagnose
composer install
composer clear-cache
composer install –optimize-autoloader
composer dump-autoload –optimize
composer clear-cache

# make sure Laravel has all necessary directories:
mkdir node_modules/
mkdir public/uploads/
mkdir public/storage/
mkdir public/storage/logs/
mkdir storage/logs
mkdir storage/sessions/
mkdir bootstrap/cache/

# make sure Laravel has the necessary permissions:
chmod -R 775 public/
chmod -R 775 storage/
chmod -R 775 bootstrap/cache/

# clear caching in artisan:
php artisan clear-compiled
php artisan config:clear
php artisan cache:clear
php artisan optimize

 

# Create VHost for MyLaravelProject to be called from a web browser:

sudo vi /etc/apache2/conf/httpd.conf
# make sure your file contains this line:
>> IncludeOptional sites-enabled/*.conf

# create a configuration file in apache for your application:
sudo nano /etc/apache2/sites-available/mylaravelproject.localdev.conf

# …and insert the following:
<VirtualHost *:80>
     ServerAdmin me@mailserver.com
     DocumentRoot /var/www/MyLaravelProject/public

     ServerName mylaravelproject.localdev
     ServerAlias mylaravelproject.localdev

     HostNameLookups off
     <Directory “/var/www/MyLaravelProject/public”>
        Order allow,deny
        Allow from all
        DirectoryIndex index.php index.html
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
     </Directory>
</VirtualHost>

# create symbolic links to configuration files in sites-enabled:
sudo ln -s /etc/apache2/sites-available/mylaravelproject.localdev.conf /etc/apache2/sites-enabled/mylaravelproject.localdev.conf

# edit your local hosts file (C:\Windows\System32\drivers\etc\hosts) to resolve mylaravelproject.localdev name to IP (e.g. 192.168.174.666 – obviously *.666 is invalid):
192.168.174.666  MyLaravelProject.localdev

 

Test the URL http://MyLaravelProject.localdev/ in your browser. If errors occur read them carefully as you might have forgotten the contents for .env and .htaccess files – get those from production. Also, make sure to double check the MySQL credentials and connection for your user.