简体   繁体   English

部署 Laravel 和 React spa

[英]Deploy Laravel and React spa

How can I deploy these two together, I don't like the Laravel React preset, I want to separate both, bundle the React app and deploy them together with any web server (apache, nginx...)如何将这两个部署在一起,我不喜欢 Laravel React 预设,我想将两者分开,捆绑 React 应用程序并将它们与任何 Web 服务器(apache、nginx...)一起部署

EDIT编辑

This is my config for Laravel, but it isn't loading the routes这是我的 Laravel 配置,但它没有加载路由

server {
    listen 8000;
    server_name 127.0.0.1
    root "..\..\Proyecto\Backend\JWT\public";

    add_header 'Access-Control-Allow-Origin' '*';
    add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

index index.html index.htm index.php;

charset utf-8;

location / {
    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 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    include fastcgi_params;
}

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

This was proving to be very tricky and it took me at least 3 days to put everything together.事实证明,这非常棘手,我至少花了 3 天的时间将所有东西放在一起。 Here is what you have to do.这是你必须做的。

Run npm run build in the react project.在 react 项目中运行npm run build

Copy the contents of the build folder to the server将 build 文件夹的内容复制到服务器

scp react_project/build/* <server name or ip>:/var/www/html/react

Change the ownership of the project folders to the user www-data or add your user id to the group www-data .将项目文件夹的所有权更改为用户www-data或将您的用户 ID 添加到组www-data

Now.现在。 set up the Laravel project in a different directory (in /var/www/html/laravel, for example).在不同的目录(例如 /var/www/html/laravel)中设置 Laravel 项目。 Set up the database, environment variables.设置数据库,环境变量。 Run

php artisan key:generate
php artisan config:clear
php artisan config:cache

Now, proceed with nginx configuration.现在,继续进行 nginx 配置。 Create 2 configs for react and laravel projects as given below.为 react 和 laravel 项目创建 2 个配置,如下所示。 Make sure that the listen ports are different for both projects.确保两个项目的监听端口不同。 Create configuration files for react and laravel projects under /etc/nginx/sites-available Create symlinks to the created configs under /etc/nginx/sites-enabled as given below/etc/nginx/sites-available下为 react 和 laravel 项目创建配置文件在/etc/nginx/sites-enabled下创建指向已创建配置的符号链接,如下所示

sudo ln -s /etc/nginx/sites-available/react_conf /etc/nginx/sites-enabled/react_conf
sudo ln -s /etc/nginx/sites-available/laravel_conf /etc/nginx/sites-enabled/laravel_conf

And for the contents, react_conf:对于内容,react_conf:

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_conf: laravel_conf:

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;

}

Now, delete the default config present in /etc/nginx/sites-enabled现在,删除/etc/nginx/sites-enabled的默认配置

Also, verify that /etc/nginx/nginx.conf contains the following include directive where the server configs are expected(under http)此外,验证/etc/nginx/nginx.conf包含以下包含指令,其中需要服务器配置(在 http 下)

include /etc/nginx/sites-enabled/*;

Verify that the config is fine by running通过运行验证配置是否正常

sudo nginx -t

Restart the server重启服务器

sudo service nginx restart

Now, you should be up and running.现在,您应该启动并运行了。

You can run them separately using nginx您可以使用 nginx 单独运行它们

you run each on separate ports and use methods (POST/GET) to push/get data您在单独的端口上运行每个并使用方法(POST/GET)来推送/获取数据

use pm2 ( http://pm2.keymetrics.io/ ) for running React (I recommend it because you can monitor the activity of the react app and if you want to do maintenance you can stop the current app process and run a "under maintenance" app process)使用 pm2 ( http://pm2.keymetrics.io/ ) 来运行 React(我推荐它,因为你可以监控 React 应用程序的活动,如果你想进行维护,你可以停止当前的应用程序进程并运行一个“under维护”应用程序过程)

you can read more about running laravel on nginx here ( https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04 )您可以在此处阅读有关在 nginx 上运行 laravel 的更多信息( https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04

as for running react without pm2, you have to build the project yarn build and tell nginx that the file you want to load is the index.html inside of the build file至于在没有pm2的情况下运行react,您必须构建项目yarn build并告诉nginx您要加载的文件是构建文件中的index.html

assuming that you are using an ubuntu server and you uploaded your code to github or gitlab假设您使用的是 ubuntu 服务器并将代码上传到 github 或 gitlab

server {
  listen 50;
  root /var/www/[Your repo name]/build;
  server_name [your.domain.com] [your other domain if you want to];
  index index.html;
  location / {
    try_files $uri /index.html;
  }
}

you write this inside of your nginx configuration along with the laravel configuration on a separate port你把它写在你的 nginx 配置中,以及在一个单独的端口上的 laravel 配置

hope my answer helped a bit希望我的回答有点帮助

You can approach it by two ways .您可以通过两种方式来处理它。

First one is when the you are creating react-app in different folder than the laravel project folder .第一个是当您在与 laravel 项目文件夹不同的文件夹中创建 react-app 时。 In such case just deploy laravel app and react app in two different url .在这种情况下,只需在两个不同的 url 中部署 laravel app 和 react app。

The second condition is when the react-app is inside the laravel app .第二个条件是 react-app 位于 laravel app 内。 In such case build the react project and put the dist folder in views folder of the laravel project .在这种情况下,构建 react 项目并将 dist 文件夹放在 laravel 项目的 views 文件夹中。 So in routes/web.php add this所以在 routes/web.php 添加这个

//Used for handling the html file of react project
 View::addExtension('html', 'php');

Route::get('/{any}', function () {
   //path to dist folder index.html inside the views directory
   return view('build/index');
})->where('any', '.*');

Laravel will not server the required js and css file from inside the views folder . Laravel 不会从 views 文件夹中提供所需的 js 和 css 文件。 So you need copy and paste all the content of the dist folder to public folder of the laravel project .所以你需要将dist文件夹的所有内容复制粘贴到laravel项目的public文件夹中。 No need to copy paste index.html file but other file need to placed in the pubic folder .无需复制粘贴 index.html 文件,但其他文件需要放置在公共文件夹中。

After that visit the root url of the laravel project in the browser the react app should be working在浏览器中访问 laravel 项目的根 url 之后,react 应用程序应该可以正常工作

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

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