Laravel Dev Environment in Ubuntu 18.04

It’s not often I have to set up a dev enviroment but when I do, I tend to blog about it so I can refer to it again if I need to. This time I will be creating a dev environment on Ubuntu Desktop 18.04. I will be installing the following.

  • PHP 7.2.x
  • MySQL Community Server 5.7.x
  • Composer
  • Node
  • Laravel Installer
  • PHPStorm

I will not use Apache or Nginx. I will be using PHP’s built-in web server for testing and debugging.

Installing PHP 7.2.x

I like using packages made by Ondřej Surý. You will need to add his PPA. This will give you the ability to use different versions of PHP.

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.2 php7.2-cli php7.2-mysql php7.2-zip php7.2-mbstring php7.2-xml

# check version of php
php -v

php_version.png

Installing MySQL

Installing MySQL is straight forward. You install MySQL server then you run the secure installation process. Just answer the questions and that will do it.

sudo apt-get install mysql-server

#run secure installation
sudo mysql_secure_installation

Just follow the prompts and and answer the questions. Being it’s a dev environment I don’t enable the password validation plugin. Enter your password you will use as root. I do remove any test related stuff and remote connection. Reload privileges.

The odd thing is root won’t have access unless you run it with sudo.

sudo_mysql.png

As you can see from the screenshot above, you will get an access denied unless you use sudo.

If you don’t want to use terminal, then I would suggest install Workbench.

#install mysql workbench
sudo apt-get install mysql-workbench

You might run into the issue as explained above. One thing you can do is create a new user but essentially it will have the same priveleges as root. I guess you can increase security by using a username that is not commonly used to define such an account. But it’s still decreasing security.

CREATE USER 'admin'@'localhost' IDENTIFIED BY '';
GRANT ALL PRIVILEGES ON . TO 'admin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

I used admin as an example but you can use whatever you like. You can also omit the GRANT OPTION if you wish. Open Workbench and edit the connection to match the new user and you should be able to log in to MySQL. Check out this article. I got some answers from here https://askubuntu.com/questions/773446/unable-to-connect-via-mysql-workbench-to-localhost-in-ubuntu-16-04-passwordless.

Installing Composer

Installing composer is easy. Just head on over to https://getcomposer.org/download/ and follow the instructions. I would move the composer.phar and rename it to /usr/local/bin/composer.

sudo mv composer.phar /usr/local/bin/composer

Test command with…

composer about

Running composer about should tell you about composer and should tell you that the path is accessible throughout the system.

Install Laravel via Composer

Now that you have composer installed, you can use it to install Laravel installer.

composer global require "laravel/installer"

Next we need to add the composer vendor bin director to our path. Edit your ~/.profile file.

pico ~/.profile

Add the following…

export PATH=~/.config/composer/vendor/bin:$PATH

Save it and source the file.

source ~/.profile

You can now create a new app with the following command.

laravel new test

It should install the Laravel framework on the test directory. Go into the folder and run the built-in PHP web server.

php artisan serve

You will now see the website by opening your browser to http://localhost:8000.

laravel_new_browser.png

For bonus points, you can create an alias so you don’t have to keep typing php artisan. Edit your ~/.bashrc file and add the following.

alias pa='php artisan'

After saving, don’t forget to source the .bashrc file. Now you should be able to run pa and it will run php artisan for you. You can create different aliases to help your development workflow.

Laravel Development on Windows 10 Using Linux Subsystem

This is a guide on how to configure Windows 10 using the Fall Creators Update released in October 2017. I will be using Ubuntu for the Linux Subsystem.

Ubuntu

  1. Turn on Windows Subsystem for Linux in the Windows Feature
    windows_feature_wsl
  2. Restart Windows so it fully installs it
  3. Then go to the Windows Store app and download and install Ubuntu
    ubuntu_windows_store
  4. Launch it from the Windows Store screen. Terminal will open and will ask you questions to configure the environment such as username and password.
  5. You should update Ubuntu with the usual commands
    sudo apt-get update
    sudo apt-get upgrade -y
  6. Install build essentials
    sudo apt-get install build-essential -y

PHP7

Now your environment is up to date. The next step is to get PHP7 installed. Depending on the version of Ubuntu, you may not have access to the latest version of PHP. Just add a repository and install it. For this guide, I will be install PHP7.1.

  1. sudo apt-get install -y python-software-properties
  2. sudo add-apt-repository -y ppa:ondrej/php
  3. sudo apt-get update -y
  4. sudo apt-get install -y php7.1 php7.1-cli php7.1-common php7.1-mbstring php7.1-gd php7.1-mysql php7.1-mcrypt php7.1-zip php7.1-xml

NodeJS

  1. curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash –
  2. sudo apt-get install -y nodejs

Composer

Instructions can be found at https://getcomposer.org/download/. Please note, step 2 below changes. The hash value is generated automatically. Step 5 will move and rename the command so it is accessible throughout the environment. Step 6 will test it if it works.

  1. php -r “copy(‘https://getcomposer.org/installer’, ‘composer-setup.php’);”
  2. php -r “if (hash_file(‘SHA384’, ‘composer-setup.php’) === ‘[this is where the hash goes – do not copy this verbatim – go to the link above’) { echo ‘Installer verified’; } else { echo ‘Installer corrupt’; unlink(‘composer-setup.php’); } echo PHP_EOL;”
  3. php composer-setup.php
  4. php -r “unlink(‘composer-setup.php’);”
  5. sudo mv composer.phar /usr/local/bin/composer
  6. composer about

Laravel

Please be careful with copying and pasting. The quotes sometimes gets pasted with a different font – thus ending in a different result. After installation, make sure you add the path to your profile file (~/.profile) with step 3. If there’s already a line that exist, just add it. Keep in mind that each path is separated by a colon (:) and ends with $PATH.

  1. composer global require “laravel/installer”
  2. pico ~/.profile
  3. export PATH=~/.config/composer/vendor/bin:$PATH
  4. source ~/.profile

MySQL Server

Install MySQL server and follow the on-screen instructions. If the server is not started or you get a mysqld.sock error, you probably need to start the service. The command is in step 3.

  1. sudo apt-get install mysql-server -y
  2. sudo service mysql start
  3. sudo mysql_secure_installation

To help manage your database, download MySQL Workbench from https://dev.mysql.com/downloads/workbench/. You may need to download some prerequisite software from Microsoft.

mysql_workbench_requirements
Click the OK button
mysql_workbench_download_prerequisites
Click the Download Prerequisites button

Click on the Download Prerequisites button and it will take you to https://dev.mysql.com/resources/wb62_prerequisites.html. This page will list a couple of software but all I download is the Visual C++ Redistrubatble for Visual Studio 2015. Meeting that requirement seems to be enough for me. I download the 64bit version to match my operating system. Once installed, run the MySQL Workbench installer again and you should have no problem.

Create a new connection. The default values should work. All you have to do is provide the password.

mysql_connection_test
Connect to your new MySQL Server

Make sure that Ubuntu is running and that the MySQL service is also running or you won’t be able to connect.

That’s about it. I’m glad WSL is out of beta. So far it’s been running well.

I tried Virtualbox with Ubuntu workstation and after the Creator’s Update, it stopped working. I can log in but it won’t load the desktop on the console window. Oddly enough, the preview window shows the desktop loaded.

I also tried Homestead but I get an error with the shared folder. I run npm commands and it tells me that it can’t delete and write because of permission denied. I also tried Hyper-V and Ubuntu workstation is horrible on it. Moving the mouse lags. I’ve given it 2 CPU and 6GB of ram. I used different drivers – nothing.

I also posted a different way to develop Laravel on Windows check it out here. It works for the most part but it’s not as clean with SSH keys and GIT repositories. That article is actually pretty popular based on my analytics. I’m a bit surprised since I don’t really recommend using Windows for development unless you’re using Microsoft technology. It’s just a huge headache. We’ll see how well this environment last. Fortunately for me I have Apple computers and can run Ubuntu on spare hardware. This guide is just for my curiousity and to help others. Also, I like being able to use “better” hardware than my Apple devices.

Laravel Episode 5: MySQL and Auth Design


episode 5

Episode 5: MySQL and Auth Design

 

  • Show how to configure MySQL
  • Create a database
  • Create a user specific to the database
  • Design the Users and Roles table
  • Move roles migration file so it is created before users – rename it
  • Edit user migration file so it uses role_id as foreign key
  • Create log in form
  • Create log in/out controller method
  • Create authenticate controller method
  • Create if/else to show login link or logout link