Configure apache virtual host with the current ec2 public IP

Viewed 569

I have a user data script on an EC2 instance that installs and configures apache at launch for a Flask Rest API. All the files are pulled from my Git repo, and the VirtualHost config is as follows:

<VirtualHost *:80>
                ServerName *
                ServerAdmin admin@mywebsite.com
                WSGIPassAuthorization On
                WSGIScriptAlias / /var/www/FlaskApi/flaskapi.wsgi
                <Directory /var/www/FlaskApi/FlaskApi/>
                        Order allow,deny
                        Allow from all
                </Directory>
                Alias /static /var/www/FlaskApi/FlaskApi/static
                <Directory /var/www/FlaskApi/FlaskApi/static/>
                        Order allow,deny
                        Allow from all
                </Directory>
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

However, at launch, there is no way to know which public ip address the instance will get, therefore i would like to know if there is some sort of wildcard config for a VirtualHost so there is no need to specify a ServerName or IP.

Edit: As per Jorge's suggestion, i have added the following to my start up script and it is working like a charm!

ip=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)
sudo sed -i "s/replace/$ip/g" /etc/apache2/sites-enabled/FlaskApi.conf
2 Answers

Part of your question may be answered by the fact that (I believe) "VirtualHost *:80" tells it to listen on all network interfaces for that machine, regardless of the network device's assigned IP address. I may be missing something in your question, though.

From what I recall, the ServerName field specifies what host request the virtualhost should respond to. If that field says whateverdomain.com, then the virtual host will respond to requests asking for whateverdomain.com. If the field says *.whateverdomain.com, it will respond to requests for any subdomain of whateverdomain.com. With virtual hosts, you probably want to limit what domain each virtual host responds to. Overlapping might cause errors - don't know.

I'm not sure if there's a wildcard for ServerName tag to do what you want, but if you don't mind a bit of script editing, you can use the command described on this post:

curl http://169.254.169.254/latest/meta-data/public-ipv4

Running this will return your instance's current IP address. You can create a little script that runs this command at startup, read the result, edit your Apache .conf file containing your virtual server (I'd recommend having a separate file instead of working with httpd.conf itself) with a Regular Expression and then restart your httpd.

There is also another, simpler solution. AWS's Elastic IP. Yes, it costs money to maintain, but it's not that much actually and it makes your life so much easier, all you have to do is to set the Elastic IP to your instance through AWS Console, and every time you reboot your IP will be the same. If this alternative wasn't cheap, my company would have implemented the first method. I'd recommend you to at least take a look at it.

Related