Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 🚀 Laravel Project Setup Guide — By VeeRa
- A complete guide to setting up a new Laravel project, configuring environment files, and preparing it for development or deployment.
- ---
- ## 🧰 1. Create or Download a Laravel Project
- You can create a new Laravel project in two ways:
- ### Option 1: Via Composer
- ```bash
- composer create-project --prefer-dist laravel/laravel my-laravel-app
- ```
- **Explanation:**
- - `composer create-project` — Creates a new project from a package.
- - `--prefer-dist` — Uses distribution archives (ZIP files) for faster installation.
- - `laravel/laravel` — The official Laravel starter package.
- - `my-laravel-app` — The directory name for your new project.
- ### Option 2: Manual Download
- You can also download it manually:
- 👉 [Laravel GitHub Releases](https://github.com/laravel/laravel/tags)
- ---
- ## ⚙️ 2. Configure the Environment File
- In your Laravel project directory, copy the example `.env` file:
- ```bash
- cp .env.example .env
- ```
- > The `.env` file stores your app configuration, including database settings, app name, environment, and more.
- ---
- ## 🧩 3. Add Files to the Root Directory (Optional)
- If you want to run your Laravel app directly from the **root folder** (instead of `/public`), add the following files in the **main project directory**.
- ### 📄 `index.php`
- ```php
- <?php
- /**
- * Laravel - A PHP Framework For Web Artisans
- *
- * @package Laravel
- * @author Taylor Otwell
- */
- $uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
- // This emulates Apache's "mod_rewrite" using PHP's built-in server.
- if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
- return false;
- }
- require_once __DIR__.'/public/index.php';
- ```
- ### ⚙️ `.htaccess`
- ```
- <IfModule mod_rewrite.c>
- RewriteEngine On
- RewriteCond %{REQUEST_FILENAME} -d [OR]
- RewriteCond %{REQUEST_FILENAME} -f
- RewriteRule ^ ^$1 [N]
- RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
- RewriteRule ^(.*)$ public/$1
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteRule ^ index.php
- # Handle Authorization Header
- RewriteCond %{HTTP:Authorization} .
- RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
- </IfModule>
- ```
- > ✅ These files are **optional** — only use them if your hosting environment doesn’t support pointing the web root to the `/public` folder.
- ---
- ## 🛠️ 4. Add `.htaccess` Inside the `public` Folder
- Create a `.htaccess` file inside the `/public` directory with this content:
- ```
- <IfModule mod_rewrite.c>
- <IfModule mod_negotiation.c>
- Options -MultiViews -Indexes
- </IfModule>
- RewriteEngine On
- # Handle Authorization Header
- RewriteCond %{HTTP:Authorization} .
- RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
- # Redirect Trailing Slashes If Not A Folder
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteCond %{REQUEST_URI} (.+)/$
- RewriteRule ^ %1 [L,R=301]
- # Send Requests To Front Controller
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteRule ^ index.php [L]
- </IfModule>
- ```
- ---
- ## 📦 5. Install Composer Dependencies
- Inside your project directory, run:
- ```bash
- composer install
- ```
- > Installs all PHP dependencies defined in `composer.json`.
- ---
- ## 🔑 6. Generate the Application Key
- ```bash
- php artisan key:generate
- ```
- > This generates a unique `APP_KEY` value in your `.env` file — required for encryption and security.
- ---
- ## 🧱 7. Run Database Migrations (Optional)
- If you’ve set up your database connection in `.env`, run:
- ```bash
- php artisan migrate
- ```
- > ⚠️ Ensure your database is running and credentials are correct before running migrations.
- ---
- ## 🧹 8. Clear and Optimize the Cache
- To clear and rebuild all Laravel caches, run:
- ```bash
- php artisan optimize:clear
- php artisan cache:clear
- php artisan config:cache
- php artisan config:clear
- ```
- ---
- ## ▶️ 9. Start the Development Server
- Start the Laravel local development server:
- ```bash
- php artisan serve
- ```
- Then open your browser and go to:
- 👉 [http://localhost:8000](http://localhost:8000)
- ---
- ## 💡 Notes & Best Practices
- - In **production**, always set your web server’s **document root** to the `/public` directory.
- - The root-level `.htaccess` and `index.php` files are **only needed** if you can’t change the server’s root directory.
- - Ensure **Apache’s `mod_rewrite`** module is enabled.
- - After cloning any Laravel project, always run:
- ```bash
- composer install
- php artisan key:generate
- ```
- - Never commit your `.env` file to Git — it contains sensitive information.
- ---
- ## ✅ Setup Complete!
- Your Laravel project is now ready! 🎉
- Happy coding with Laravel ❤️
- ---
Advertisement
Add Comment
Please, Sign In to add comment