Laravel on localhost

http://localhost:8000

Laravel's built-in PHP development server runs on port 8000 by default when using php artisan serve. This covers local Laravel development, configuration, and best practices.

→ Open localhost:8000

Install Laravel Locally

Requirements

  • PHP 8.1 or higher
  • Composer (PHP package manager)
  • MySQL or other database
  • Node.js and NPM (for frontend assets)

Installation Methods

# Method 1: Using Composer (Recommended) composer create-project laravel/laravel my-laravel-app # Method 2: Using Laravel Installer composer global require laravel/installer laravel new my-laravel-app # Navigate into project cd my-laravel-app # Install dependencies composer install npm install # Start development server php artisan serve # Server starts at http://localhost:8000

Laravel Artisan Serve Commands

# Start on default port 8000 php artisan serve # Start on custom port php artisan serve --port=8080 # Start on custom host php artisan serve --host=0.0.0.0 # Start on custom host and port php artisan serve --host=192.168.1.100 --port=8080 # Access from other devices on network php artisan serve --host=0.0.0.0 --port=8000 # Then visit: http://YOUR_IP:8000

Laravel Project Structure

my-laravel-app/ ├── app/ │ ├── Http/ │ │ ├── Controllers/ │ │ └── Middleware/ │ └── Models/ ├── bootstrap/ ├── config/ │ ├── app.php │ ├── database.php │ └── ... ├── database/ │ ├── migrations/ │ ├── seeders/ │ └── factories/ ├── public/ ← Document root │ ├── index.php ← Entry point │ ├── css/ │ └── js/ ├── resources/ │ ├── views/ │ ├── css/ │ └── js/ ├── routes/ │ ├── web.php │ ├── api.php │ └── console.php ├── storage/ ├── tests/ ├── vendor/ ├── .env ← Configuration ├── artisan ← CLI tool └── composer.json

Configure Laravel for localhost

Environment Configuration (.env)

# Application APP_NAME=MyLaravelApp APP_ENV=local APP_KEY=base64:... APP_DEBUG=true APP_URL=http://localhost:8000 # Database - MySQL DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= # Database - PostgreSQL # DB_CONNECTION=pgsql # DB_HOST=127.0.0.1 # DB_PORT=5432 # Cache, Session, Queue CACHE_DRIVER=file SESSION_DRIVER=file QUEUE_CONNECTION=sync # Mail (for testing) MAIL_MAILER=log MAIL_HOST=localhost MAIL_PORT=1025

Generate Application Key

# Generate new key php artisan key:generate # This updates APP_KEY in .env

Setup Database

Create Database

# Using MySQL CLI mysql -u root -p CREATE DATABASE laravel CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; EXIT; # Or use phpMyAdmin # http://localhost/phpmyadmin # Click "New" → Enter "laravel" → Create

Run Migrations

# Run all migrations php artisan migrate # Rollback last migration php artisan migrate:rollback # Reset database and run all migrations php artisan migrate:fresh # Seed database with sample data php artisan db:seed # Fresh migrations with seeding php artisan migrate:fresh --seed

Laravel Routes and URLs

// routes/web.php Route::get('/', function () { return view('welcome'); }); Route::get('/about', function () { return view('about'); }); Route::get('/contact', function () { return view('contact'); }); // Controller routes Route::get('/users', [UserController::class, 'index']); Route::get('/users/{id}', [UserController::class, 'show']); // Resource routes (CRUD) Route::resource('posts', PostController::class); // API routes (routes/api.php) Route::prefix('api')->group(function () { Route::get('/users', [UserController::class, 'apiIndex']); });

Access URLs

  • localhost:8000/ - Home page
  • localhost:8000/about - About page
  • localhost:8000/users - Users list
  • localhost:8000/posts - Posts resource
  • localhost:8000/api/users - API endpoint

Laravel with XAMPP/WAMP

Alternative to artisan serve: Use Apache server

Method 1: Direct Access

  1. Place Laravel project in htdocs/
    C:\xampp\htdocs\my-laravel-app\
  2. Access public folder: localhost/my-laravel-app/public
  3. Configure .env with proper APP_URL

Method 2: Virtual Host (Recommended)

Edit C:\xampp\apache\conf\extra\httpd-vhosts.conf:

<VirtualHost *:80> ServerName laravel.local DocumentRoot "C:/xampp/htdocs/my-laravel-app/public" <Directory "C:/xampp/htdocs/my-laravel-app/public"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog "logs/laravel-error.log" CustomLog "logs/laravel-access.log" common </VirtualHost>

Edit C:\Windows\System32\drivers\etc\hosts:

127.0.0.1 laravel.local

Restart Apache and access: http://laravel.local

Essential Laravel Artisan Commands

# View all commands php artisan list # Get help for command php artisan help migrate # Clear caches php artisan cache:clear php artisan config:clear php artisan route:clear php artisan view:clear # Generate files php artisan make:controller UserController php artisan make:model Post -m # with migration php artisan make:migration create_posts_table php artisan make:seeder UserSeeder php artisan make:request StorePostRequest php artisan make:middleware CheckAge # Database php artisan migrate php artisan migrate:rollback php artisan db:seed # View routes php artisan route:list # Optimize for production php artisan optimize php artisan config:cache php artisan route:cache php artisan view:cache # Queue workers php artisan queue:work php artisan queue:listen # Schedule commands php artisan schedule:run # Tinker (REPL) php artisan tinker

Frontend Assets

Using Vite (Laravel 9+)

# Install dependencies npm install # Development server npm run dev # Runs on http://localhost:5173 # Build for production npm run build # In blade templates @vite(['resources/css/app.css', 'resources/js/app.js'])

Using Laravel Mix (Laravel 8)

# Install dependencies npm install # Watch for changes npm run watch # Build for production npm run production # In blade templates <link rel="stylesheet" href="{{ mix('css/app.css') }}"> <script src="{{ mix('js/app.js') }}"></script>

Fix "Laravel localhost Not Working"

Issue: Server won't start

# Check if port 8000 is in use # Windows netstat -ano | findstr :8000 # Linux/Mac lsof -i :8000 # Use different port php artisan serve --port=8080

Issue: 404 on routes

  • Check routes/web.php for defined routes
  • Clear route cache: php artisan route:clear
  • Verify .htaccess in public/ folder

Issue: Database connection error

# Verify .env settings DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= # Test database connection php artisan migrate:status # Clear config cache php artisan config:clear

Issue: Permission errors

# Linux/Mac - Set permissions chmod -R 775 storage bootstrap/cache chown -R www-data:www-data storage bootstrap/cache # Or for development chmod -R 777 storage bootstrap/cache

Issue: APP_KEY not set

# Generate application key php artisan key:generate # This creates/updates APP_KEY in .env

Laravel Development Tools

Laravel Debugbar

# Install debugbar composer require barryvdh/laravel-debugbar --dev # Automatically enabled in local environment

Laravel Telescope

# Install telescope composer require laravel/telescope --dev # Publish assets php artisan telescope:install # Run migrations php artisan migrate # Access at: localhost:8000/telescope

Laravel Tinker (REPL)

# Enter tinker shell php artisan tinker # Test code interactively >>> $users = App\Models\User::all(); >>> User::find(1)->name >>> DB::table('users')->count() >>> exit

Testing Laravel Applications

# Run all tests php artisan test # Or using PHPUnit ./vendor/bin/phpunit # Run specific test php artisan test --filter UserTest # With coverage php artisan test --coverage # Create test php artisan make:test UserTest php artisan make:test UserTest --unit

Laravel Performance Optimization

# Cache configuration php artisan config:cache # Cache routes php artisan route:cache # Cache views php artisan view:cache # Optimize composer autoload composer dump-autoload -o # Run all optimizations php artisan optimize # Clear all caches php artisan optimize:clear
Pro Tip: Use php artisan serve for quick development, but configure a virtual host for a more production-like environment with proper URL rewriting and better performance.

Laravel API Development

// routes/api.php Route::middleware('auth:sanctum')->group(function () { Route::apiResource('posts', PostController::class); }); // Access API endpoints // GET localhost:8000/api/posts // POST localhost:8000/api/posts // GET localhost:8000/api/posts/{id} // PUT localhost:8000/api/posts/{id} // DELETE localhost:8000/api/posts/{id} // Install Sanctum for API authentication composer require laravel/sanctum php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" php artisan migrate
Security Note: Never commit your .env file to version control. It contains sensitive credentials. Always use .env.example as a template.

Frequently Asked Questions

What port does Laravel use?

Laravel's built-in server uses port 8000 by default when running php artisan serve. You can change this with the --port option.

Can I use Laravel without artisan serve?

Yes, you can use Apache/Nginx by pointing the document root to the public/ folder. Configure a virtual host for better URL structure.

Why can't I access my Laravel site?

Common issues: server not running, wrong port, firewall blocking, database connection error, or missing APP_KEY. Check each systematically.

Do I need XAMPP for Laravel?

No, Laravel includes a built-in server. However, XAMPP provides MySQL and phpMyAdmin which are useful for database management.

How do I deploy Laravel from localhost to production?

Change APP_ENV to production, disable debug mode, cache config/routes, use proper database credentials, and configure a web server with proper document root.

Related Resources