Learning Laravel: Installation and Configuration
Nathan Heffley Nov 11 '17
Laravel is the "PHP Framework For Web Artisans" and it's become a hot topic in PHP back-end development over the past few years. Before you can start learning it though, you need to learn how to set up a new project using it.
Note: this article will assume you already have PHP 7, Composer, NPM, and MySQL installed on your development computer. While this is a beginner lesson for Laravel, it isn't a lesson for these other foundational systems. PHP is notoriously difficult to set up on Windows, so for the sake of brevity, I will not be covering that in this article.
To get started with Laravel, you'll first need to install the command line tool. This tool will allow you to quickly spin up new projects. All you need to do is use Composer to require the dedicated installer.
composer global require "laravel/installer"
To be able to use this new command anywhere throughout your system, you'll need to make sure your Composer vendor bin as part of your $PATH system variable. Once you have the installer successfully configured, you can test it out by trying to create a new project.
laravel new sitename
If all goes well, you will have a new folder called sitename that now contains a brand new Laravel project.
Downloading The Dependencies
Now that you have your project scaffolding created,
cd into your sitename folder and get ready to work. The first thing you'll need to do is install the dependencies. With Composer, this is as easy as possible.
Wait a little bit depending on your computer and internet connection, and you'll be good to go!
npm install npm run dev
Anytime you want to compile your assets all you have to do is run the
dev command and everything will be compiled down to your public directory.
Once you go to launch your site on a production server, there is also an
npm run prodcommand that you should use instead of
dev. While developing, there is also a handy
npm run watchcommand which will recompile your assets any time you make a change to any of them.
With all of your dependencies installed, you can move onto the final step of setting up your project.
Configuring Your New Project
All that's left is to change the actual configuration files. Within your project, there should be a file named
.env. If you can't find it, copy the
.env.example file and create a
.env file yourself. Within this file are where you most important configuration settings belong. If you use source control like Git (and you should) do not commit this file to your repository. It will contain sensitive data, like your database username and password, and any other API tokens or passwords you need while developing your application.
For just getting Laravel up and running we don't actually need to change any settings in here for right now. The first settings you will most likely change though when working on your own projects are the
DB_PASSWORD variables. I'll let you figure out what you should put in each of those when the time comes!
Before you can actually view the site though, you do need to set a value for the
APP_KEY variable. You could just type a bunch of random letters and numbers there manually, or you can go back to your terminal and use a built-in command that comes with Laravel.
php artisan key:generate
You should see a message that your
Application key [base64:gibberishhere=] set successfully. If you were to look at your
.env file again you would see that
APP_KEY is now set, and you are all set to finally check out your project in the browser!
If you have a local server system set up like Apache or Nginx, you could create a server that points users to your
/sitename/public directory. For development purposes though, you can simply use another one of the built-in artisan commands to spin-up a local server.
php artisan serve
There will be a message saying that a
Laravel development server started and a URL. You should be able to visit either
http://127.0.0.1:8000 and see the default welcome page that comes pre-installed!
Congratulations! You're now ready to start creating amazing websites. I can't wait to see what incredible creations you come up with, I'd love to hear about them.
If you're interested in learning more about Laravel or just want to chat, check out my website.
I'd love to hear what you guys think. Anybody interested in more beginner Laravel tutorials?