简体   繁体   English

在同一台服务器上创建-react-app + Laravel

[英]Create-react-app + Laravel in the same server

  • I have developed a RESTful API supported by Laravel .我开发了一个Laravel支持的 RESTful API。
  • And I want to consume it by a ReactJS application created with create-react-app UI kit.我想通过使用create-react-app UI 工具包创建ReactJS应用程序来使用它。

Two different technologies for frontend and backend, so far so good.前端和后端两种不同的技术,到目前为止都很好。

The problem (which is, in turn, the most common situation) is that both frontend and backend have to be served from the same domain (or server/file structure).问题(反过来,这是最常见的情况)是前端和后端都必须从同一个域(或服务器/文件结构)提供服务。

What is the best approach to make the two projects coexist in the same server?使两个项目在同一服务器中共存的最佳方法是什么?

EDIT : The main problem here is that neither Laravel Mix nor any Laravel frontend capabilities can be used because of the create-react-app stack.编辑:这里的主要问题是 Laravel Mix 和任何 Laravel 前端功能都不能使用,因为 create-react-app 堆栈。 The rules it imposes make very difficult the integration without first ejecting the app, which is not recommendable from the maintenance point of view.它强加的规则使得在不首先弹出应用程序的情况下进行集成非常困难,从维护的角度来看这是不推荐的。

If you are using Apache HTTPd, you have two options.如果您使用的是 Apache HTTPd,您有两个选择。

Lets say you have copied your front-end and back-end directories as follows:假设您已按如下方式复制了前端和后端目录:

Back-end (assumed Laravel 5.x) under /var/www/yourappname/api/ /var/www/yourappname/api/下的后端(假设为 Laravel 5.x)

Front-end under /var/www/yourappname/frontend/ /var/www/yourappname/frontend/

You can access your app using app.yourdomain.com .您可以使用app.yourdomain.com访问您的应用。 (I normally prefer www.yourdomain.com to be a separate server / Apache instance for security and performance reasons). (出于安全和性能原因,我通常更喜欢www.yourdomain.com作为单独的服务器/Apache 实例)。

1. Two sub-domains 1. 两个子域

Have two sub-domains pointing to same IP address, say app.yourdomain.com for front-end, and api.yourdomain.com for Laravel back-end.有指向同一个IP地址的两个子域,说app.yourdomain.com的前端,并api.yourdomain.com为Laravel后端。 Create two virtual hosts in your Apache configuration and document root as follows在您的 Apache 配置和文档根目录中创建两个虚拟主机,如下所示

For api.yourdomain.com对于api.yourdomain.com

/var/www/yourappname/api/public

For app.yourdomain.com对于app.yourdomain.com

/var/www/yourappname/frontend/

Your back-end base URL to be included in front-end app will be api.yourdomain.com/要包含在前端应用程序中的后端基本 URL 将是api.yourdomain.com/

Advantage: if you wish to split your front-end and back-end on two different Apache instances or separate servers in future, you can do it easily.优点:如果你想在未来将你的前端和后端拆分到两个不同的 Apache 实例或单独的服务器上,你可以很容易地做到。 Also, front-end is static content and hence could be served using other low cost options like S3 based sites.此外,前端是静态内容,因此可以使用其他低成本选项(如基于 S3 的站点)提供服务。

Caveat: You will have to take care of CORS ( https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS ) issue.警告:您必须处理 CORS ( https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS ) 问题。 Also, you will need two SSL certificates or multi-domain certificate or wild-card SSL certificate.此外,您将需要两个 SSL 证书或多域证书或通配符 SSL 证书。

I prefer this option and I have used this in one of my setup considering future load.我更喜欢这个选项,考虑到未来的负载,我在我的设置之一中使用了它。

2. Single Domain and Alias 2. 单域和别名

Set Document Root as将文档根设置为

/var/www/yourappname/frontend

Add Alias as follows ( http://httpd.apache.org/docs/2.4/mod/mod_alias.html#alias )添加别名如下( http://httpd.apache.org/docs/2.4/mod/mod_alias.html#alias

Alias "/api/" "/var/www/yourappname/api/public/

Your back-end base URL to be included in front-end app will be app.yourdomain.com/api/要包含在前端应用程序中的后端基本 URL 将是app.yourdomain.com/api/

(I have not yet verified this on my setup) (我还没有在我的设置中验证这一点)

Advantage: You have single domain, and need single SSL certificate.优点:您有一个域,并且需要一个 SSL 证书。

Caveat: All hits will be on same Apache service and hence difficult to segregate load of compute intensive API requests and static content.警告:所有命中都将在同一个 Apache 服务上,因此难以分离计算密集型 API 请求和静态内容的负载。

Note: In either case, I have pointed to "public" directory of Laravel framework, to avoid exposing configuration and other directories of Laravel to outside world.注意:无论哪种情况,我都指向 Laravel 框架的“public”目录,以避免将 Laravel 的配置和其他目录暴露给外界。

I have had to spend considerable time on fixing the issues I faced while deploying my project with React front-end and Laravel as the API.我不得不花费大量时间来解决在使用 React 前端和 Laravel 作为 API 部署我的项目时遇到的问题。 Hope this helps someone who is trying to deploy similar stack to production.希望这可以帮助那些试图将类似堆栈部署到生产中的人。

To summarize, I had separate configs for react and laravel projects, each with different listen ports, and different location .总而言之,我为 react 和 laravel 项目设置了单独的配置,每个配置都有不同的监听端口和不同的location

Since I was using Laravel as my API, I used /api as the location config for laravel and / as location config for react.由于我使用 Laravel 作为我的 API,我使用/api作为 Laravel 的位置配置和/作为反应的位置配置。

Please find the nginx configs I used for reference请找到我用作参考的 nginx 配置

React:反应:

server {
    listen     80;
    server_name <server_ip or hostname>;
    charset utf-8;
    root /var/www/html/react;
    index index.html index.htm;
    # Always serve index.html for any request
    location / {
        root /var/www/html/react;
        try_files $uri /index.html;
    }
    error_log /var/log/nginx/react-app-error.log;
    access_log /var/log/nginx/react-app-access.log;

}

Laravel:拉拉维尔:

server {
    listen     90;
    server_name <server ip or hostname>;
    charset utf-8;
    root /var/www/html/laravel/public;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.php index.html index.htm;
    # Always serve index.html for any request
    location /api {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

    error_log /var/log/nginx/laravel-app-error.log;
    access_log /var/log/nginx/laravel-app-access.log;

}

I know it's a question from some time ago, but in case someone stumbles upon this in search for answers, it is now possible to use Create React App in Laravel (without ejecting): https://github.com/mjsarfatti/create-react-app-laravel/我知道这是前一段时间的问题,但如果有人在寻找答案时偶然发现了这个问题,现在可以在 Laravel 中使用 Create React App(无需弹出): https : //github.com/mjsarfatti/create-反应-应用-laravel/

create-react-app-laravel is essentially a fork of Create React App that runs in your Laravel project. create-react-app-laravel本质上是在 Laravel 项目中运行的 Create React App 的一个分支。

You can see https://github.com/mjsarfatti/create-react-app-laravel/wiki/ for the installation guide, and https://dev.to/mjsarfatti/introducing-cral-create-react-app-laravel-4n90 for the announcement.您可以查看https://github.com/mjsarfatti/create-react-app-laravel/wiki/获取安装指南,以及https://dev.to/mjsarfatti/introducing-cral-create-react-app-laravel -4n90为公告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM