Score:0

How to server a react application in apache properly?

iq flag

I am using apache to run a moodle application (PHP), but now I need to add a React app inside this moodle application but when try to start the React app it just shows an empty index.html

I have modified the apache config file to serve react apps like this:

<VirtualHost ipserver:443>
        ServerAdmin [email protected]
        ServerName moodleapp

        DocumentRoot /var/www/moodleapp
        ErrorLog ${APACHE_LOG_DIR}/upficerror.log
        CustomLog ${APACHE_LOG_DIR}/upficaccess.log combined

        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/aula/moodleapp.crt
        SSLCACertificateFile /etc/ssl/certs/aula/moodleapp.ca
        SSLCertificateKeyFile /etc/ssl/certs/aula/moodleapp.key

        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                SSLOptions +StdEnvVars
        </Directory>

        <Directory /var/www/moodleapp/>
                Options -Indexes
                Order allow,deny
                Allow from all
        </Directory>

        <Directory /var/www/moodleapp/react/build>
            Options -Indexes
            Order allow,deny
            Allow from all
            DirectoryIndex index.html
            FallbackResource /react/index.html
        </Directory>

        Alias /react /var/www/moodleapp/react/build

        <Location /react>
            ProxyPass http://localhost:3000
            ProxyPassReverse http://localhost:3000
        </Location>

        <Directory /var/www/moodleapp/react/src>
            Options -Indexes
            Order allow,deny
            Allow from all
        </Directory>

        Alias /react/src /var/www/moodleapp/react/src

</VirtualHost>

And also inside the Rreact app root I have an .htaccess:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /react/index.html [L]

My react app was built with vite and this is the file config:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  build: {
    outDir: 'build',
  },
  server: {
    port: 3000,
    host: true,
  }
});

Inside the package.json I also added the "homepage" like this:

  "name": "react",
  "private": true,
  "version": "1.0.0",
  "type": "commonjs",
  "homepage": "/react/",

Once started the app with yarn start or even using pm2 I checked the path using domain/react or ip/react, but it doesn't work. Inspecting the Network in the browser I see that the page is receiving 404 errors from https://mydomain/@vite/client, https://mydomain/src/main.jsx and https://mydomain/@react-refresh, but obviously the path is wrong because main.jsx is inside mydomain/react/src/main.jsx not mydomain/src/main.jsx. I am not sure where the error is.

David avatar
cn flag
What is the version of Ubuntu?
Ander Canales Medina avatar
iq flag
I am using Ubuntu 22.04.2 LTS
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.