Using Elastic Beanstalk from Amazon Web Services is a create way to deploy large projects and be ready for a gain in visitors. Because I have a few projects which are too small to deploy each project as its own Elastic Beanstalk application I looked for a way to deploy several projects with multiple Domains into one AWS EB application.
In the past the only way to configure application environments was to use customized AMIs. This would mean that I can not update my application to newer Amazon AMIs. So I would have to update my AMI regularly on my own.
A new and better way is to use the new configuration files. After deploying an application the configuration files, stored in the folder .ebextensions on the root directory of the applichation, are processed. All configuration files must end with ‚.config‘.
The Elastic Beanstalk uses regular EC2 instances with an apache webserver. So I used the configuration files to create an file inside the apache folder to tell the webserver how to handle multiple domains. The deployed PHP application is stored inside the /var/www/html folder.
Here is the configuration file. I saved it under .ebextensions/vhost.config
files: "/etc/httpd/conf.d/vhost.conf": mode: "000644" owner: root group: root encoding: plain content: | NameVirtualHost *:80 <VirtualHost *:80> ServerName www.projekt1.de ServerAlias projekt1.de DocumentRoot /var/www/html/projekt1/ </VirtualHost> <VirtualHost *:80> ServerName static.projekt1.de DocumentRoot /var/www/html/static/ </VirtualHost>
The lines after the content entry were copied into a new file /etc/httpd/conf.d/vhost.conf. The webserver reads this file on startup an creates new vhosts with different root folders. All requests for www.projekt1.de or projekt1.de use /var/www/html/projekt1/ als document root. Requests to static.projekt1.de use folder static inside the application as document root.
For detailed information on how to configure multiple domains in apache read the VirtualHost Examples from the apache project page.
If you have any questions, problems or suggestions please write a comment on this post.
Hi Mr. Roth,
I followed your example of how to set-up a single beanstalk instance with multiple domains and I am stuck. I try to have two distinct domains. The first resolves not at all but the second does. Additionally, I can not set-up a simple wordpress blog, the database connection doesn’t work. Strangely, when I did not use no custom domain at all, just the standard xxx.elasticbeanstalk.com, the db connection worked fine. The domains have been set-up using Amazon Route 53 and both A entries point to the loadbalancer instance. Here’s my config file:
files:
„/etc/httpd/conf.d/vhost.conf“:
mode: „000644“
owner: root
group: root
encoding: plain
content: |
NameVirtualHost *:80
ServerName http://www.auto-leasing-ohne-bank.info
ServerAlias auto-leasing-ohne-bank.info
DocumentRoot /var/www/html/auto-leasing-ohne-bank/
ServerName http://www.leasing-ohne-bank.info
ServerAlias leasing-ohne-bank.info
DocumentRoot /var/www/html/leasing-ohne-bank/
Hey,
So I have recently been trying to add vhosts, your example didn’t work I was either getting time-outs or forbidden errors. If anyone is in the same problem, try something like
ServerName your-custom-domain-here.com
DocumentRoot „/var/app/current/your-new-webroot“
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted