How to deploy a React App on Apache web server

后端 未结 8 2146
谎友^
谎友^ 2020-11-29 18:11

I have created a basic React App from https://www.tutorialspoint.com/reactjs/reactjs_jsx.htm here , I want to run this test code on Apache based server, I know that I need t

相关标签:
8条回答
  • 2020-11-29 19:09

    Ultimately was able to figure it out , i just hope it will help someone like me.
    Following is how the web pack config file should look like check the dist dir and output file specified. I was missing the way to specify the path of dist directory

    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;
    

    Then the package json file

    {
      "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"
      }
    }
    

    Notice the script section and production section, production section is what gives you the final deployable index.js file ( name can be anything )

    Rest fot the things will depend upon your code and components

    Execute following sequence of commands

    npm install

    this should get you all the dependency (node modules)

    then

    npm run production

    this should get you the final index.js file which will contain all the code bundled

    Once done place index.html and index.js files under www/html or the web app root directory and that's all.

    0 讨论(0)
  • 2020-11-29 19:11

    You can run it through the Apache proxy, but it would have to be running in a virtual directory (e.g. http://mysite.something/myreactapp ).

    This may seem sort of redundant but if you have other pages that are not part of your React app (e.g. PHP pages), you can serve everything via port 80 and make it apear that the whole thing is a cohesive website.

    1.) Start your react app with npm run, or whatever command you are using to start the node server. Assuming it is running on http://127.0.0.1:8080

    2.) Edit httpd-proxy.conf and add:

    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.) Restart Apache

    0 讨论(0)
提交回复
热议问题