我看过这篇文章React,js on Apache server,但它只有一些指导方针。
首先,在您的react项目中打开 package.json 文件,并将以下代码行调整为与您的目标域名地址+文件夹匹配:
```json { "homepage": "http://your-website.com/your-project-folder" } ```"homepage": "https://yourwebsite.com/your_folder_name/",
其次,在你的React项目中进入终端,并键入以下命令:
npm run build
现在,将新创建的build文件夹中的所有文件与文件夹一起上传到您的文件夹名称,使用FileZilla放置在子文件夹中,如下所示:
public_html/your_folder_name
在浏览器中检查!
最终我能够弄清楚,我希望它可以帮助像我一样的人。
以下是 webpack 配置文件应该看起来的样子,请检查指定的 dist 目录和输出文件。我错过了指定 dist 目录路径的方法。
const webpack = require('webpack');
const path = require('path');
var config = {
entry: './main.js',
output: {
path: path.join(__dirname, '/dist'),
filename: 'index.js',
},
devServer: {
inline: true,
port: 8080
},
resolveLoader: {
modules: [path.join(__dirname, 'node_modules')]
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
},
}
module.exports = config;
然后是 package.json 文件
{
"name": "reactapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack --progress",
"production": "webpack -p --progress"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2",
"webpack": "^2.2.1"
},
"devDependencies": {
"babel-core": "^6.0.20",
"babel-loader": "^6.0.1",
"babel-preset-es2015": "^6.0.15",
"babel-preset-react": "^6.0.15",
"babel-preset-stage-0": "^6.0.15",
"express": "^4.13.3",
"webpack": "^1.9.6",
"webpack-devserver": "0.0.6"
}
}
注意脚本部分和生产部分,生产部分是为您提供最终可部署的index.js
文件(名称可以是任何内容)
其余的事情将取决于您的代码和组件
执行以下命令序列
npm install
这应该会为您获取所有依赖项(节点模块)
然后
npm run production
这应该会为您获取最终的index.js
文件,其中包含所有捆绑在一起的代码
完成后,将index.html
和index.js
文件放置在www/html或Web应用程序根目录下,就是这样。
cd /home/ubuntu/react-js
。npm run build
。 asset-manifest.json
favicon.ico
manifest.json
robots.txt
static
assets
index.html
precache-manifest.ddafca92870314adfea99542e1331500.js
service-worker.js
将build文件夹复制到您的apache服务器,即/var/www/html
:
sudo cp -rf build /var/www/html
进入sites-available目录:
cd /etc/apache2/sites-available/
打开000-default.conf
文件:
sudo vi 000-default.conf
并将DocumentRoot路径更改为/var/www/html/build
。
cd /etc/apache2
sudo vi apache2.conf
添加以下代码段:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
/var/www/html/build
目录下创建一个文件:sudo vi .htaccess
。
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
重新启动Apache服务器:
sudo service apache2 restart
.htaccess
文件? - MattRewriteEngine
requires the rewrite
module to be enabled with a2enmod rewrite
- Gabriel Jablonski在进行 npm 构建之前,
1)进入您的 React 项目根目录并打开 package.json
。
2)向 package.json
添加 "homepage" 属性。
如果您想提供绝对路径
"homepage": "http://hostName.com/appLocation",
"name": "react-app",
"version": "1.1.0",
如果您想提供相对路径
"homepage": "./",
"name": "react-app",
使用相对路径方法可能会在您的IDE中发出语法验证错误警告。但是编译过程中没有出现任何错误。
3)保存package.json
文件,并在终端中运行npm run-script build
4)将build/
文件夹的内容复制到您的服务器目录中。
PS:如果您想经常更改服务器上构建文件的位置,那么使用相对路径方法非常容易。
ProxyRequests On
ProxyVia On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /myreactapp http://127.0.0.1:8080/
ProxyPassReverse /myreactapp http://127.0.0.1:8080/
3.) 重新启动Apache
如 React 官方 文档 所述,如果您使用的路由器在后台使用 HTML5 的 pushState
历史 API,则只需将以下内容添加到您的 react-app 的 public
目录下的 .htaccess
文件中。
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
如果使用相对路径,请按照以下方式更新package.json
:
"homepage": ".",
注意: 如果您正在使用react-router@^4
,您可以在任何<Router>
上使用basename
属性来根目录<Link>
。
import React from 'react';
import BrowserRouter as Router from 'react-router-dom';
...
<Router basename="/calendar"/>
<Link to="/today"/>
sudo apt install apache2
安装 apache2
,然后在项目目录下运行以下命令。npm run build
或者
yarn run build
那么
sudo cp -rf build/* /var/www/html/mySite;
cd /etc/apache2/sites-available/;
sudo cp 000-default.conf mySite.conf;
sudo nano /mySite.conf
ServerName myDomin.com
ServerAlias www.myDomin.com
DocumentRoot /var/www/html/mySite
sudo nano /etc/hosts
192.168.1.1
,那么:127.0.0.1 localhost myDomin.com
192.168.1.1 myDomin.com www.myDomin.com
现在,如果您知道如何操作,请自行在VPS中设置您的名称服务器,或者使用Cloudflare或类似的工具。
注意:如果您想在本地主机中使用,请运行以下命令:
cp build/* /var/www/html/ && sudo systemctl restart apache2
pom.xml http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 it.megadix create-react-app-servlet 0.0.1-SNAPSHOT war
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<npm.output.directory>build</npm.output.directory>
</properties>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Standard plugin to generate WAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<directory>${npm.output.directory}</directory>
</resource>
</webResources>
<webXml>${basedir}/web.xml</webXml>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<!-- Required: The following will ensure `npm install` is called
before anything else during the 'Default Lifecycle' -->
<execution>
<id>npm install (initialize)</id>
<goals>
<goal>exec</goal>
</goals>
<phase>initialize</phase>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
</execution>
<!-- Required: The following will ensure `npm install` is called
before anything else during the 'Clean Lifecycle' -->
<execution>
<id>npm install (clean)</id>
<goals>
<goal>exec</goal>
</goals>
<phase>pre-clean</phase>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
</execution>
<!-- Required: This following calls `npm run build` where 'build' is
the script name I used in my project, change this if yours is
different -->
<execution>
<id>npm run build (compile)</id>
<goals>
<goal>exec</goal>
</goals>
<phase>compile</phase>
<configuration>
<executable>npm</executable>
<arguments>
<argument>run</argument>
<argument>build</argument>
</arguments>
</configuration>
</execution>
</executions>
<configuration>
<environmentVariables>
<CI>true</CI>
<!-- The following parameters create an NPM sandbox for CI -->
<NPM_CONFIG_PREFIX>${basedir}/npm</NPM_CONFIG_PREFIX>
<NPM_CONFIG_CACHE>${NPM_CONFIG_PREFIX}/cache</NPM_CONFIG_CACHE>
<NPM_CONFIG_TMP>${project.build.directory}/npmtmp</NPM_CONFIG_TMP>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<environmentVariables>
<PUBLIC_URL>http://localhost:8080/${project.artifactId}</PUBLIC_URL>
<REACT_APP_ROUTER_BASE>/${project.artifactId}</REACT_APP_ROUTER_BASE>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<environmentVariables>
<PUBLIC_URL>http://my-awesome-production-host/${project.artifactId}</PUBLIC_URL>
<REACT_APP_ROUTER_BASE>/${project.artifactId}</REACT_APP_ROUTER_BASE>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
注意:如果在运行项目后发现空白页面,则清除缓存或重新启动IDE。/etc/apache2/sites-available/
目录中添加代理,然后执行sudo nano your-domain.conf
,添加代理,最后使用pm2 restart all
运行。