1

I have two development environments: PC at work (env1), laptop at home (env2). Both machines run Ubuntu 15.10 with Apache 2.4.12. Once finished, the project shall be deployed to a public web server (env3). I use Git for version control and Bitbucket for distributed repository management.

My project is a WordPress powered website. Hence, I add the WordPress root directory, some additional source directories and the latest Mysql database dump SQL file to the development repository, that I am currently working on. If I pull and checkout the latest branch updates on the other environment, I also run the database dump file on the local database. So the dev environments stay consistent all the times including all content and setting changes made to WordPress via admin backend.

However, server names remain different. WP (and I assume other web apps too) have configuration settings like hostname, which are stored in the database, but are actually different on each of the environments. Env1 may operate at a private IP, say 192.168.xxx.yy while env2 may not be connected to a LAN and thus, listen to localhost / 127.0.0.1 only and env3 will need the official host name, say mydomain.com.

My question is, how can I configure the Apache web servers on env1 and env2, that they use some artificial host name, say localdevname for the respective virtual hosts? So I could set WP configuration hostname = localdevname:91 and call localdevname:91 at env1 which resolves to 192.168.xxx.yy:91 while localdevname:91 called at env2 will resolve to localhost:91?

I found a similiar question here. But the only answer doesn't solve my issue. Also, I played around with directives ServerName, ServerAlias, VirtualHostName without success.

Bunjip
  • 111
  • 1
  • 1
    Can you use an entry in your /etc/hosts file? I do this all the time where I point a production URL to a dev system for testing. This way, apache configs do not need to change. On env2 - you add an entry for `www.wordpresssite.com` to 127.0.0.1. Do the same in env1 for the 192.xxx ip. When you need the production URL, comment out the hosts entry. – Gmck Jan 25 '16 at 15:56
  • I think I can do that. Anything to watch out for while doing that? Does any system need to be re-started after changing /etc/hosts ? – Bunjip Jan 25 '16 at 16:00
  • [This question has been discussed on Meta.SE](http://meta.stackexchange.com/q/273635/148099). And this is much clearer than your previous formulation of the question. – 200_success Jan 25 '16 at 18:32
  • No restarts needed. All you are doing is pointing a FQDN at an IP on the machine you are working on. I find it quite a bit more convenient than adding/changing apache configs. – Gmck Jan 26 '16 at 20:50

1 Answers1

0

My recommendation would be to create a different /etc/apache2/conf-available/network.conf file for each environment (and a2enconf it). Those files might look like this:

/etc/apache2/conf-available/network.conf on env1 host

<VirtualHost 192.168.1.1:91>
    ServerName localdevname
    SetEnv environment env1
    SetEnv dbserver 192.168.1.1
    Include /etc/apache2/sites-available/wordpress-project.conf
</VirtualHost>

/etc/apache2/conf-available/network.conf on env2 host

<VirtualHost 127.0.0.1:91>
    ServerName localdevname
    SetEnv environment env2
    SetEnv dbserver 127.0.0.1
    Include /etc/apache2/sites-available/wordpress-project.conf
</VirtualHost>

/etc/apache2/conf-available/network.conf on production host

<VirtualHost 11.22.33.44:80>
    ServerName mydomain.com
    SetEnv environment prod
    SetEnv dbserver mysql-prod.mydomain.com
    Include /etc/apache2/sites-available/wordpress-project.conf
</VirtualHost>

Your wordpress-project.conf and WordPress configuration files can be identical on all hosts. They can make use of environment variables to vary their behaviour.

In addition, you may find mod_macro to be useful.

You'll need to configure your /etc/hosts and/or local DNS configuration as well to make localdevname resolve the way you want.

200_success
  • 4,701
  • 1
  • 24
  • 42