"Day 2 Lecture Notes: Step-by-Step Guide to Setting Up Your Laravel Development Environment"
Day 2 Lecture Notes
Step-by-Step Guide to Setting Up Your Laravel Development Environment
Objective:
The goal of this session is to guide students through the process of setting up
a Laravel development environment on their local machines. By the end of this
class, students will have Laravel installed and configured with a running
development server, ready to start building applications.
Outcome:
Students will have a fully operational Laravel environment on their system and
will be able to run their first Laravel project locally.
1. Install
XAMPP or a Similar Stack
Before installing Laravel, it's
essential to have a local development environment that includes PHP, MySQL, and
Apache. XAMPP is an easy-to-use solution that includes these components.
Steps to
Install XAMPP:
1. Download
XAMPP:
o
Go to the official XAMPP website.
o
Download the appropriate version for your operating
system (Windows, macOS, or Linux).
2. Install
XAMPP:
o
After downloading, run the installer and follow the
installation wizard.
o
During installation, ensure that the following
components are selected:
§ Apache
(Web server)
§ MySQL
(Database server)
§ PHP
(Programming language)
3. Start
XAMPP:
o
Open the XAMPP Control Panel (installed along with
XAMPP).
o
Start the Apache and MySQL services. This will
launch the web server (Apache) and the database server (MySQL), allowing you to
run Laravel applications locally.
4. Verify
Installation:
o
Open a browser and type http://localhost/ in the
address bar. If XAMPP is installed correctly, you should see the XAMPP welcome
page.
Alternative
Stacks:
- WAMP
(Windows only): Another option for Windows users that
includes Apache, MySQL, and PHP.
- MAMP
(macOS and Windows): A similar stack for both macOS and Windows
users.
- LAMP
(Linux only): A stack for Linux users that includes Linux,
Apache, MySQL, and PHP.
2. Install
Composer
Composer is a dependency
management tool for PHP. Laravel relies on Composer to manage its dependencies,
so it's essential to have it installed.
Steps to
Install Composer:
1. Download
Composer:
o
Visit the official Composer website:
https://getcomposer.org/download/.
o
For Windows, use the Composer-Setup.exe file to
install Composer automatically.
o
For macOS/Linux, run the following command in your
terminal to install Composer globally:
bash
Copy code
curl -sS
https://getcomposer.org/installer | php
sudo mv
composer.phar /usr/local/bin/composer
2. Verify
Installation:
o
After installing Composer, open a terminal or
command prompt and run:
bash
Copy code
composer
--version
If the installation was
successful, you should see the installed version of Composer.
3. Install
Laravel
With Composer installed, you can
now install Laravel.
Steps to
Install Laravel:
1. Create a
Laravel Project:
o
Open your terminal (Command Prompt or PowerShell on
Windows).
o
Navigate to the directory where you want to create
your new Laravel project. You can use the cd command to change directories.
For example:
bash
Copy code
cd
C:\xampp\htdocs
o
Run the following Composer command to install a new
Laravel project:
bash
Copy code
composer
create-project --prefer-dist laravel/laravel my-laravel-app
Replace my-laravel-app with the
name you want for your project. Composer will download and install Laravel and
its dependencies into a new directory.
2. Verify
Laravel Installation:
o
Once the installation is complete, navigate into
your Laravel project folder:
bash
Copy code
cd my-laravel-app
o
To check if everything is set up correctly, run the
following command:
bash
Copy code
php
artisan --version
This should display the version
of Laravel you installed.
4. Configure
.env for Database Connection
Laravel uses an environment file,
.env, to store configuration settings like database credentials, app settings,
and environment variables.
Steps to
Configure the .env File:
1. Open the .env
File:
o
Inside your Laravel project folder, locate the .env
file. This file is automatically created when you install Laravel.
o
Open it in any text editor (e.g., Visual Studio
Code, Sublime Text).
2. Set Up
Database Configuration:
o
Find the following section in the .env file, which
is responsible for the database connection:
dotenv
Copy code
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
o
Update these values based on your local database
configuration:
§ DB_CONNECTION: Set
this to mysql if you are using MySQL.
§ DB_HOST: 127.0.0.1
is the local address for MySQL running on your system.
§ DB_PORT: The
default MySQL port is 3306.
§ DB_DATABASE: Set
this to the name of the database you want Laravel to use. You can create a new
database via phpMyAdmin (accessible at http://localhost/phpmyadmin).
§ DB_USERNAME: The
default MySQL username is root.
§ DB_PASSWORD: If
you're using the default MySQL setup, leave this blank.
For example:
dotenv
Copy code
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=root
DB_PASSWORD=
3. Create
the Database (if not already done):
o
Go to http://localhost/phpmyadmin in your browser.
o
Click on "Databases" and create a new
database with the name you specified in the .env file.
4. Test the
Database Connection:
o
Run the following artisan command to test the
connection:
bash
Copy code
php
artisan migrate
If everything is set up
correctly, Laravel will run database migrations without errors.
5. Run
the Development Server with php artisan serve
Laravel comes with a built-in
development server that makes it easy to test your application locally.
Steps to
Run the Development Server:
1. Run the
Command:
o
In the terminal, navigate to your Laravel project
directory if you're not already there.
o
Run the following command to start the development
server:
bash
Copy code
php
artisan serve
2. Access
the Application:
o
Once the server starts, you will see a message
like:
arduino
Copy code
Laravel
development server started: http://127.0.0.1:8000
o
Open a web browser and go to http://127.0.0.1:8000.
If everything is set up correctly, you should see the Laravel welcome page.
Conclusion
By following these steps,
students should now have a fully functional Laravel environment set up on their
local machines. They can start building and testing Laravel applications using
the development server and a local MySQL database.

Comments
Post a Comment