Virajsinh

Laravel Project Setup Steps By VeeRa README.md

Sep 13th, 2024 (edited)
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. # 🚀 Laravel Project Setup Guide — By VeeRa
  2.  
  3. A complete guide to setting up a new Laravel project, configuring environment files, and preparing it for development or deployment.
  4.  
  5. ---
  6.  
  7. ## 🧰 1. Create or Download a Laravel Project
  8.  
  9. You can create a new Laravel project in two ways:
  10.  
  11. ### Option 1: Via Composer
  12.  
  13. ```bash
  14. composer create-project --prefer-dist laravel/laravel my-laravel-app
  15. ```
  16.  
  17. **Explanation:**
  18. - `composer create-project` — Creates a new project from a package.
  19. - `--prefer-dist` — Uses distribution archives (ZIP files) for faster installation.
  20. - `laravel/laravel` — The official Laravel starter package.
  21. - `my-laravel-app` — The directory name for your new project.
  22.  
  23. ### Option 2: Manual Download
  24.  
  25. You can also download it manually:
  26. 👉 [Laravel GitHub Releases](https://github.com/laravel/laravel/tags)
  27.  
  28. ---
  29.  
  30. ## ⚙️ 2. Configure the Environment File
  31.  
  32. In your Laravel project directory, copy the example `.env` file:
  33.  
  34. ```bash
  35. cp .env.example .env
  36. ```
  37.  
  38. > The `.env` file stores your app configuration, including database settings, app name, environment, and more.
  39.  
  40. ---
  41.  
  42. ## 🧩 3. Add Files to the Root Directory (Optional)
  43.  
  44. 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**.
  45.  
  46. ### 📄 `index.php`
  47.  
  48. ```php
  49. <?php
  50. /**
  51. * Laravel - A PHP Framework For Web Artisans
  52. *
  53. * @package Laravel
  54. * @author Taylor Otwell
  55. */
  56.  
  57. $uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
  58.  
  59. // This emulates Apache's "mod_rewrite" using PHP's built-in server.
  60. if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
  61. return false;
  62. }
  63.  
  64. require_once __DIR__.'/public/index.php';
  65. ```
  66.  
  67. ### ⚙️ `.htaccess`
  68.  
  69. ```
  70. <IfModule mod_rewrite.c>
  71. RewriteEngine On
  72.  
  73. RewriteCond %{REQUEST_FILENAME} -d [OR]
  74. RewriteCond %{REQUEST_FILENAME} -f
  75. RewriteRule ^ ^$1 [N]
  76.  
  77. RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
  78. RewriteRule ^(.*)$ public/$1
  79.  
  80. RewriteCond %{REQUEST_FILENAME} !-d
  81. RewriteCond %{REQUEST_FILENAME} !-f
  82. RewriteRule ^ index.php
  83.  
  84. # Handle Authorization Header
  85. RewriteCond %{HTTP:Authorization} .
  86. RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
  87. </IfModule>
  88. ```
  89.  
  90. > ✅ These files are **optional** — only use them if your hosting environment doesn’t support pointing the web root to the `/public` folder.
  91.  
  92. ---
  93.  
  94. ## 🛠️ 4. Add `.htaccess` Inside the `public` Folder
  95.  
  96. Create a `.htaccess` file inside the `/public` directory with this content:
  97.  
  98. ```
  99. <IfModule mod_rewrite.c>
  100. <IfModule mod_negotiation.c>
  101. Options -MultiViews -Indexes
  102. </IfModule>
  103.  
  104. RewriteEngine On
  105.  
  106. # Handle Authorization Header
  107. RewriteCond %{HTTP:Authorization} .
  108. RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
  109.  
  110. # Redirect Trailing Slashes If Not A Folder
  111. RewriteCond %{REQUEST_FILENAME} !-d
  112. RewriteCond %{REQUEST_URI} (.+)/$
  113. RewriteRule ^ %1 [L,R=301]
  114.  
  115. # Send Requests To Front Controller
  116. RewriteCond %{REQUEST_FILENAME} !-d
  117. RewriteCond %{REQUEST_FILENAME} !-f
  118. RewriteRule ^ index.php [L]
  119. </IfModule>
  120. ```
  121.  
  122. ---
  123.  
  124. ## 📦 5. Install Composer Dependencies
  125.  
  126. Inside your project directory, run:
  127.  
  128. ```bash
  129. composer install
  130. ```
  131.  
  132. > Installs all PHP dependencies defined in `composer.json`.
  133.  
  134. ---
  135.  
  136. ## 🔑 6. Generate the Application Key
  137.  
  138. ```bash
  139. php artisan key:generate
  140. ```
  141.  
  142. > This generates a unique `APP_KEY` value in your `.env` file — required for encryption and security.
  143.  
  144. ---
  145.  
  146. ## 🧱 7. Run Database Migrations (Optional)
  147.  
  148. If you’ve set up your database connection in `.env`, run:
  149.  
  150. ```bash
  151. php artisan migrate
  152. ```
  153.  
  154. > ⚠️ Ensure your database is running and credentials are correct before running migrations.
  155.  
  156. ---
  157.  
  158. ## 🧹 8. Clear and Optimize the Cache
  159.  
  160. To clear and rebuild all Laravel caches, run:
  161.  
  162. ```bash
  163. php artisan optimize:clear
  164. php artisan cache:clear
  165. php artisan config:cache
  166. php artisan config:clear
  167. ```
  168.  
  169. ---
  170.  
  171. ## ▶️ 9. Start the Development Server
  172.  
  173. Start the Laravel local development server:
  174.  
  175. ```bash
  176. php artisan serve
  177. ```
  178.  
  179. Then open your browser and go to:
  180. 👉 [http://localhost:8000](http://localhost:8000)
  181.  
  182. ---
  183.  
  184. ## 💡 Notes & Best Practices
  185.  
  186. - In **production**, always set your web server’s **document root** to the `/public` directory.
  187. - The root-level `.htaccess` and `index.php` files are **only needed** if you can’t change the server’s root directory.
  188. - Ensure **Apache’s `mod_rewrite`** module is enabled.
  189. - After cloning any Laravel project, always run:
  190. ```bash
  191. composer install
  192. php artisan key:generate
  193. ```
  194. - Never commit your `.env` file to Git — it contains sensitive information.
  195.  
  196. ---
  197.  
  198. ## ✅ Setup Complete!
  199.  
  200. Your Laravel project is now ready! 🎉
  201. Happy coding with Laravel ❤️
  202.  
  203. ---
  204.  
Advertisement
Add Comment
Please, Sign In to add comment