DEV Community

Aditya Rawas
Aditya Rawas

Posted on • Edited on

Hosting react on apache.(Ubuntu, AWS)

Are you willing to host react on Apache ??
Here are few steps.

STEP 1 - Install Apache.

sudo apt install apache2
Enter fullscreen mode Exit fullscreen mode

This command will install apache on you machine.

STEP 2 - Pull code.

Go to

cd /var/www/html
Enter fullscreen mode Exit fullscreen mode

and pull your react code using git

sudo git clone <your-git-repo>
Enter fullscreen mode Exit fullscreen mode

Install dependencies

sudo npm install
Enter fullscreen mode Exit fullscreen mode

and build your react project using

sudo npm run build
Enter fullscreen mode Exit fullscreen mode

you production will be in build folder now

STEP 3 - Configure your Apache config files

You will have to mention path of build folder in .config files.
edit /etc/apache2/apache2.conf

sudo vi /etc/apache2/apache2.conf
Enter fullscreen mode Exit fullscreen mode

Comment existing Directory tags to avoid conflicts for example in my case I commented by adding # as shown below

#<Directory />
#       Options FollowSymLinks
#       AllowOverride None
#       Require all denied
#</Directory>
#
#<Directory /usr/share>
#       AllowOverride None
#       Require all granted
#</Directory>

#<Directory /var/www/>
#       Options Indexes FollowSymLinks
#       AllowOverride None
#       Require all granted
#</Directory>

#<Directory /srv/>
#       Options Indexes FollowSymLinks
#       AllowOverride None
#       Require all granted
#</Directory>
Enter fullscreen mode Exit fullscreen mode

STEP 4 - Add our React build directory into apache.conf.

Here in my case build of react is at /var/www/html/reactproject/build it may be different in your case replace this with you present build directory in following code

<VirtualHost *:80>
    #ServerName yourserver.com
    DocumentRoot /var/www/html/reactproject/build

    # Relax Apache security settings
    <Directory /var/www/html/reactproject/build>
      Allow from all
      Options -MultiViews
       Require all granted
    </Directory>
</VirtualHost>

Enter fullscreen mode Exit fullscreen mode

STEP 5 - Restart and test

Every time you edit Apache files restart apache by using following command

sudo systemctl restart apache2.service
Enter fullscreen mode Exit fullscreen mode

Check status

sudo systemctl status apache2.service
Enter fullscreen mode Exit fullscreen mode

You are good to go. for further assistance feel free to contact me will love to help you out
IF YOU LIKED THIS ARTICLE MAKE SURE TO FOLLOW ME ON DEV.TO AND ON INSTAGRAM

Aditya Rawas Coffee

Top comments (0)