Install Laravel 5.4 on Cloud9 + Bonus Tips
This is my process for setting up Laravel 5.4 in a Cloud9 workspace.
I also wrote a tutorial for 5.3 if you’re interested: Laravel 5.3 Installation on Cloud9
I had a few errors when installing 5.4 so thought it would be useful to start another topic to help others avoid similar issues, and also have a fresh comment stream related to 5.4 only.
Remove unnecessary files from you workspace
Delete hello-world.php, php.ini and README.md from your workspace - you don’t need them. You can do this in the command line if you prefer:
rm hello-world.php php.ini README.md
Upgrading to PHP 5.6
Firstly, I would like to point out that I have not written instructions for PHP 7 (if you want you can see the 5.3 topic for instructions) because I don’t have the full knowledge to make sure all of the correct packages are installed that are needed to guarantee it will work. Please seek help elsewhere or leave a comment that somebody else may be able to answer in relation to getting PHP 7 set up correctly.
- To upgrade to PHP 5.6 you can copy and paste the following code block into the command line (including extra returns for where asked for confirmation):
sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get install libapache2-mod-php5.6 sudo a2dismod php5 sudo a2enmod php5.6 sudo add-apt-repository ppa:ondrej/php5-compat sudo apt-get update sudo apt-get dist-upgrade Y
- Install composer, add folder containing laravel command to the PATH variable and install a Laravel Project using the following commands:
sudo composer global require 'laravel/installer' export PATH=~/.composer/vendor/bin:$PATH laravel new project
- Move the files and folders from the ‘project’ folder to you workspace folder.
- Remove the empty ‘project’ folder.
Set the public folder as the root for running your project
- Enter the following command to open the cloud9 config file:
sudo nano /etc/apache2/sites-enabled/001-cloud9.conf
and add /public to the end of line 2 e.g.
Press F2 to exit, then y and return to save.
Set up your database
- Run the following commands to make sure your workspace is using the default MySQL database:
mysql-ctl install mysql-ctl cli use c9; exit;
- Update your .env file so that Laravel uses the correct database.
Change the database settings in your .env file to look like this:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=c9 DB_USERNAME=your_cloud9_username DB_PASSWORD=
Note: the username should be your Cloud9 username.
You may also change your .env file to use sqlite as Jeffrey Way often does in lessons on Laracasts. Remember to add a sqlite.database file to your database folder.
I recommend you use MySQL because it is so easy on Cloud9 and is most likely what you will use in production.
You can also install PHPMyAdmin which will be hosted on your c9 domain for access to your database, by entering the following command like so:
You will see that your username for loggin in is the same as your c9 username and the password is empty (similar to the database). You will also be provided with a link to acces it e.g. c9username-workspacename-c9user.io/phpmyadmin.
Upgrade npm and node
Upgrading to the latest versions of npm and node is necessary for using Laravel Mix (to avoid errors).
- Enter the following in the command line to upgrade npm and node:
sudo npm install -g npm npm install nvm install 7.5.0 npm rebuild node-sass
Note: when you clone your workspace, the clone may have reverted back to older versions (usually just node). Just enter the command/s again.
Compile css and js assets
- Run the following command to compile your assets using Laravel Mix
npm run dev
That’s it! Your Application is ready to run. Kind of…
If you run your application, it should work and you will get the Laravel splash screen. You can now do whatever you want to create your (application) website.
Yes, website. I like to think of an application in the simplest sense as something that remembers what your users do / lets your users save what they have done.
At the moment, your users can’t even sign up to your application. If you want to make it so that your application can have users and remember what they have done, then there are a couple of easy ways to set this up with Laravel. The next section will show you a couple of different ways to do this (depending on what features you want).
This section includes a couple of different ways to add user accounts and authentication to your application. I have also added a bonus tutorial based on a personal preference for adding real-time functionality to your application.
Bonus Tip A: Laravel Auth
This is the simplest way to add authentication to your application. Just enter the following in the command line:
php artisan make:auth
If you run your project now, you will see that there are now ‘Login’ and ‘Register’ links on the Laravel welcome screen.
You probably already know what’s comes next, but read on for an important tip.
These won’t work just yet, first we need to run the database migrations using the following command:
php artisan migrate
If you do this, you probably got the following error:
[Illuminate\Database\QueryException] SQLSTATE: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_emai l_unique`(`email`))
Don’t worry there’s an easy fix. You just need to do the following:
- Add the following to the app/Providers/AppServiceProvider boot method:
- Don’t forget to include the following at the top of the file:
- Now you’re safe to run ‘php artisan migrate’
Note: if you ran migrate before updating AppServiceProvider, you will need to manually clear out the tables in your database before you try to do the migrations again, even if you rollback the migrations first it proboably won’t work.
If you managed to run your migrations successfully without any errors -
Yippee! You now have an application with user authentication. Make something awesome!
Bonus Tip B: Authentication scaffolding with Roles and Permissions
Do you want to build an application where users have different roles and permissions? I recently came accross a really cool setup to save you from having to work out how to implement this by yourself.
Centaur is ‘an opinionated implementation of Cartalyst’s Sentinel package for Laravel 5’.
It’s already been updated to work with 5.4 . I also made my first contribution on GitHub to the repository to make sure the account activation and password reset emails worked properly.
Basically it adds user management (for admins) and roles (which you can create and customise) for users. I also like that it gives your app the functionality to add users without them having to register themselves.
Please read the github repository for more info and installation instructions https://github.com/SRLabs/Centaur
Bonus Tip C: Using Firebase (Google)
I recommend using firebase to add real-time features to your app using its Real-Time database, or when you need to store unstructured data. It also has other features including client side authentication, file storage, push messaging and more.
You can use firebase in combination with a basic Laravel installation, or either of the previously mentioned auth installations. I’ll tell you the most basic way to use firebase in your Laravel application, and let you decide whether you use it instead of the standard Laravel authentication and database setup, or mix the two for different purposes as I like to.
Note: it’s not currently possible to use firebase hosting with a Laravel application since there is no way to host/run server side code. This may change in the future.
I haven’t mentioned Vue, and I’m not going to talk about it specifically. I recommend you check out the lessons on Laracasts if you don’t already know anything about it. What I am going to show you is VueFire - a NPM package that allows you to easily connect the firebase real-time database to your application.
VueFire is purely for using the firebase database, and not any of the other features. I recommend searching youtube and reading the firebase documentation to learn how the others can be used. (You will want to use firebase authentication in order to protect the data in your firebase database. At the time of writing this, I have not yet tried to implement firebase’s custom authentication that would be required to integrate it with the Laravel auth setup. I will add this when I get round to it.
[to be continued]
- Use Laravel (or Centaur) authentication and database connection for any structured user related data that a SQL database is ideal for.
- Use Laravel for greater control over API implementation, when you need to perform complex database queries and build API integrations with other applications.
- Add firebase for features that demand real-time updates to the user interface e.g. live chat, collaborative note taking; or for unstructured data (since firebase uses a noSQL-like JSON database where the data structure does not need to be explicit).
I’m currently building an app where I use both an SQL database with Laravel (Centaur setup) to manage users and their permissions to view different routes, and a firebase database to store unstructured form submissions from a third party application, where different users will have different form fields in the other application and I need to be able to accept and store any data structure they may have.