How to configure multiple Virtual Hosts on AWS EC2 Running Ubuntu?

21,611

It's pretty simple, as you have the apache already installed you'll be able to see a file called: /etc/apache2/sites-enabled/000-default which point to your www folder within var. The idea is that apache takes it as the main config file, hence you can define as many .conf files as you need so, you might want to have something like this:

/etc/apache2/sites-enabled/
├── 000-default -> ../sites-available/default
└── subsystems
    └── appA.conf
    └── appB.conf
    └── appMyWebSite.conf

So it's easier to handle because you have everything segmented into several conf files, as many as you need.

Now, what those must have? It depends on what you need but this is pretty much what you need to have:

<Directory "/var/www/mywebsite.com/">
    Options Indexes Includes FollowSymLinks MultiViews +ExecCGI
    AddHandler cgi-script cgi pl
    AllowOverride All
    Order allow,deny
    Allow from all
    DirectoryIndex index.cgi index.html
</Directory>

<VirtualHost *:80>
    ServerName mywebsite.com
    ServerAdmin [email protected]
    SetEnv VAR_IF_YOU_NEED_ONE VALUE
    RewriteEngine on

    DocumentRoot "/var/www/mywebsite.com/"

    LogLevel warn
    ServerSignature Off

    #And whatever you need take a look to apache documentation
</VirtualHost>

So you can point your domain names to the same server ip address, and apache will handle that depending on the name of those domains (i.e. same ip for all your domains)

Hope this helps.

Share:
21,611
reflyh2
Author by

reflyh2

Updated on November 15, 2020

Comments

  • reflyh2
    reflyh2 over 3 years

    I'm trying to configure a virtual host to my EC2 instance. I'm using ubuntu on my EC2 instance and apache2 as web server.

    I have 3 applications (web-based) in 3 different folders in /var/www: - /var/www/app_A - /var/www/app_B - /var/www/mywebsite

    I want people to only be able to access app_A and app_B using IP address (I've attached elastic IP to my EC2 instance) as in http://my.ip.address/app_A and http://my.ip.address/app_B. As for the application in /var/ww/mywebsite, I want people to be able to access that through my domain mywebsite.com. I've point the domain to my EC2 IP address (my.ip.address).

    I know I have to create virtual hosts to be able to achieve this, but I'm really new at this. I've been trying to google the solution, and tried several solutions but keep failing.

    Please tell me how to setup a virtual host correctly so I can achieve my purpose written above.