2

I'm quite newbie in react , and, even I have seen similar answers in stackoverflow and google, I can't make it work.

I have created an application via 'react-scripts' , and it worked awesome (just npm start . Nothing fancy).

My problem arrives when I try to deploy this application in my Apache server .

1) I execute 'npm build' . Build folder is created with (I guess ) a deployable version .

2) I copy this build folder to my Apache server and, when I try to access, I see the head and the tittle, but no content. Everything white .

I receive this message after 'npm run build' :

The project was built assuming it is hosted at the server root. To override this, specify the homepage in your package.json. For example, add this to build it for GitHub Pages:

"homepage": "http://myname.github.io/myapp",

The build folder is ready to be deployed. You may serve it with a static server:

npm install -g serve serve -s build

I guess 'serve' is a http server, but I want to deploy my app in Apache . As I understand, should be enough to copy to Apache folder . There's something I'm missing . Could you help me, please ?

2
  • Ok ... seems an Apache issue . I have seen in chrome developer console that I haven't put files in correct place : Failed to load resource: the server responded with a status of 404 (Not Found) localhost:9080/static/js/main.80cf07c6.js Failed to load resource: the server responded with a status of 404 (Not Found) localhost:9080/static/css/main.9a0fe4f1.css Failed to load resource: the server responded with a status of 404 (Not Found) I haven't tested yet, but seems my mistake Commented Mar 14, 2017 at 6:25
  • I was stuck in the same issue. I just solve this by replacing '/static' by './static' in the index.html. Commented Dec 6, 2017 at 14:47

2 Answers 2

1

step 1: create a .htaccess file in public folder with the following content

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

step 2: execute npm run build
step 3: copy files from build folder to document root of Apache

you need to make sure mod_rewrite is enabled, before using a .htaccess file.
In ubuntu, execute the command sudo a2enmod rewrite
In CentOS 7, open /etc/httpd/conf/httpd.conf file, Locate the <Directory /var/www/html> section and change the AllowOverride directive from None to All

Sign up to request clarification or add additional context in comments.

Comments

0

Check here https://github.com/mgechev/angular-seed/wiki/Deploying-prod-build-to-Apache-2

reloading pages, besides root, and deep linking will cause 404 errors... The reason is that all Angular2 routes should be served via the index.html file.

2 Comments

question is about deploying react application.
The article is still relevant as it discusses configuring Apache server, mod_rewrite specifically, for single page apps (which react apps are). In fact, your answer is very similar to what's on that page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.