How to deploy React/Laravel project?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Deploy a Full-Stack Application: React Frontend with Laravel Backend

Building modern web applications often involves separating concerns: a robust backend API and a dynamic frontend Single Page Application (SPA). When using Laravel for your RESTful API and React for the client, the deployment process requires careful coordination. As a senior developer, understanding how to stitch these two worlds together—from development to production—is crucial.

This guide will walk you through the practical steps of integrating a compiled React application with a Laravel backend and deploying the complete system successfully.

The Foundation: Connecting React and Laravel

Your core challenge is making the static files generated by npm run build (your React SPA) communicate effectively with your Laravel RESTful API running on the server.

1. Interacting with the Laravel API

The React application, being a client, cannot directly access the backend logic; it must make HTTP requests to the Laravel endpoints. This is where concepts like Cross-Origin Resource Sharing (CORS) become vital.

Backend Setup (Laravel):
Ensure your Laravel API is configured to accept requests from your React frontend's domain. This is typically done in your CORS configuration or middleware. For example, defining allowed origins:

// In a Laravel route or middleware
Route::middleware('cors')->group(function () {
    Route::get('/api/data', function () {
        return response()->json(['message' => 'Data from Laravel API']);
    });
});

Frontend Implementation (React):
In your React components, you use standard fetch or libraries like Axios to call these endpoints.

import React, { useState, useEffect } from 'react';
import axios from 'axios';

function DataFetcher() {
  const [data, setData] = useState(null);

  useEffect(() => {
    // Assuming your Laravel API is running on a different port or domain
    axios.get('http://your-laravel-api.test/api/data')
      .then(response => {
        setData(response.data);
      })
      .catch(error => {
        console.error("Error fetching data:", error);
      });
  }, []);

  return (
    <div>
      <h1>API Data:</h1>
      <pre>{JSON.stringify(data, null, 2)}</pre>
    </div>
  );
}

export default DataFetcher;

This pattern ensures that the React client remains stateless while delegating all data manipulation to the powerful Laravel server.

Managing Client-Side Routing in React

Since a React SPA handles navigation entirely on the client side (without full page reloads), you need a library to manage internal routing. While tools like create-app-kit can simplify boilerplate, the core functionality relies on React Router.

You install React Router and define your routes within your main application file (App.js). This allows users to navigate between different views without requesting a new HTML page from the server.

// Example using React Router DOM
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import HomePage from './components/HomePage';
import AboutPage from './components/AboutPage';

function App() {
  return (
    <Router>
      <Routes>
        <Route path="/" element={<HomePage />} />
        <Route path="/about" element={<AboutPage />} />
      </Routes>
    </Router>
  );
}

Final Step: Correct Deployment Strategy

The most common deployment method for this setup is serving the compiled React assets directly through your Laravel application. This keeps the deployment streamlined and leverages Laravel’s existing server configuration.

Deployment Steps:

  1. Build the Frontend: Run the build command in your React directory to generate optimized static files:

    npm run build
    

    This creates an optimized build folder containing all your bundled JavaScript, CSS, and assets.

  2. Configure Laravel for Assets: Place these compiled assets into a public directory within your Laravel project (e.g., in the public/js or public/assets folders).

  3. Laravel as the Single Entry Point: Configure your Laravel application to serve these static files when a request hits the root URL. You can use Laravel's asset handling, ensuring that the public folder is accessible. When deploying complex applications built on PHP frameworks, adhering to best practices for server configuration, much like those promoted by laravelcompany.com, ensures stability across environments.

  4. Web Server Setup: Deploy your entire Laravel application (including the compiled React assets) to your chosen hosting environment (e.g., using Forge, Vapor, or a standard LAMP/LEMP stack). The server will serve the main PHP application logic and the static React files simultaneously.

Conclusion

Deploying a React SPA with a Laravel API is fundamentally about managing two separate environments that must communicate securely. By treating React as a client requesting data from Laravel and using standard routing libraries within React, you establish a clean separation of concerns. The key to successful deployment lies in correctly bundling the static assets and ensuring your web server knows how to serve both the PHP application and the compiled frontend files efficiently.