Search code examples
reactjsdjangoamazon-web-servicesamazon-ec2

How to host a react and django application on ec2


I have a React app with Django for the backend and mysql database. I want to host this website on ec2. I have also purchased a domain. currently, I am running the React server and Django server on the ec2 instance and I am able to access this website using the elastic IP address assigned to the instance, something like public-address:3000.

I want to be able to link the domain with the ec2 instance. I have configured my domain to be linked to my instance using DNS, but the problem is the react server is running on the local host of the ec2 instance, and when I access the domain, I can't view the website.

Can someone help me out with this


Solution

  • You are having the app running on port 3000 which is why it is accessible to you when using the EIP:3000. The same will work for domain too but you have to mention the port like http://<domain-name>:3000. While you can change the app configuration to run on port 80, it is recommended to have some "middle-man" to do that job for you. You can use an NGINX reverse proxy on the same instance to listen on port 80 and then forward that to port 3000 towards your app.

    If security is your priority (and it should be), it is recommended to expose the app via an Application Load Balancer or via CloudFront (if your clients are global). In both these options, you can get free certificate for your domain from AWS using ACM (Certificate Manager).

    For ALB, use the link to set this up. Steps in short are as below:

    1. Navigate to EC2 Console
    2. Click on Target Groups under Load Balancers
    3. Click create target-group and select options
      • Target type - Instance
      • Target Protocol - HTTP
      • Target port - 3000
    4. Click next and then select your EC2 to add to this target group
    5. Navigate to ACM for your region and request for a certificate covering your domain link
    6. Proceed with creation of load balancer as mentioned in the link. For listeners use HTTP:80 and HTTPS:443 with the cert you created