AWS EC2 Dedicated Server deployment


After finishing the implementation of base game functionalities, I had to host the dedicated server somewhere in the cloud so that clients can connect and play the game. So I went through the Mirror documentation and found a few options where I can host my dedicated game server and these options were AWS, Google cloud server & Oracle Free Tier. Previously I was a bit friendly with AWS, so I chose AWS (Link to AWS cloud hosting documentation). 

The Mirror documentation is guiding for a setup of Windows EC2 instance, But I wanted to use Linux instead of Windows due to some benefits like better Performance, Flexibility & Cost-effectiveness (by the way I am using a Linux instance which is free for 1 year). I followed the same mirror documentation as the setup for the Windows instance and Linux instance are almost the same with few minor differences. You can check on this video link[Video link for EC2 instance setup(Thanks to "gameDev TITAN")] for Linux  EC2 instance setup.

After the setup of the Linux instance, I uploaded my server build into it and started running it. It was working fine if I try to connect the client from my Unity editor or using a Windows client build, But it was not working in the WebGL build which I had to upload on Itch.io. I was getting a CORS Exception when I tried to connect to the server through my WebGL build on Itch.io. After some research, I found the fix for it which I have explained below with some overview.
[Note: If you are implementing something like this for your project you may face some other challenges while implementing it, at that time you can take the help of ChatGPT most of the solutions you can get from there else you can comment here with your issue, I will give my best to help you.]

CORS setup with Apache:

-> Install Apache in the Linux Instance:

  • Install Apache:
     sudo apt install apache2
  • Enable and start the Apache service:
    sudo systemctl enable apache2
    sudo systemctl start apache2

Once Done installing the Apache server.

-> Open the Apache Configuration File and Add CORS Headers:

You can find the Apache configuration file, typically named httpd.conf  The file is usually located in /etc/httpd/conf/httpd.conf 

  • Use the below command and add the following lines to your configuration file to include CORS headers:
    sudo nano /etc/apache2/apache2.conf

    <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
    Header set Access-Control-Allow-Headers "Content-Type, Authorization"
    </IfModule>

Now is the time to set SSL:

  • I used "Let's Encrypt" to obtain an SSL certificate.
    We need to install Certbot in the EC2 instance to obtain SSL certificate from Let's Encrypt.
    I followed the following commands to install:
    sudo amazon-linux-extras install
    sudo yum install certbot-apache 

    Then for the left of the setup, I followed the same documentation from the Mirror Website:https://mirror-networking.gitbook.io/docs/manual/transports/websockets-transport...

IMP Note: if your ssl cert.json path is getting wrong even after giving the right path of cert.json, and file not found exception occurs then go check the pfx file path inside cert.json if it is something like "./cert/pfx" which may lead to an issue, change it to the absolute path where your cert.pfx file exist(Ex: /path/to/your/cert.pfx)

The issue faced at last:

---Unity WebGL throws Error: "ReferenceError: Runtime is not defined"---Solution: https://stackoverflow.com/questions/70411564/unity-webgl-throws-error-referencee...

After all the above setup my WebGL multiplayer build started to run as a client on Itch.io.

Files

projectGrid.zip Play in browser
34 days ago

Leave a comment

Log in with itch.io to leave a comment.