How to deploy a React App on Apache web server
Chạy lệnh sau ở folder chứa code project của bạn để build project reactjs
yarn build hay npm run build
Tạo file .htaccess trong folder build
Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.html [QSA,L]
Sau đó copy folder build lên server apache của bạn ở thư mục /var/www/html
Bạn có thể đối tên folder build theo ý mình nhé
Cấu hình virtual host apache
<VirtualHost *:80> ServerAdmin haitv1@spt.vn ServerName hai.test.com ServerAlias www.hai.test.com DocumentRoot /var/www/html/build <Directory "/var/www/html/build"> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> ErrorLog /var/www/html/error.log CustomLog /var/www/html/access.log combined </VirtualHost>
Chú ý nếu bạn deploy react app ở sub folder ví dụ:https://myapp.com/the-app thì có thể cấu hình ở BrowserRouter như sau:
<BrowserRouter basename="/the-app">
Bây giờ bạn có thể truy cập http://myapp.com/the-app/contacts.