1

I need to setup a development and production environment using subdomains with Laravel and Apache. For example:

dev.mydomain.com
app.mydomain.com

Where app is production and dev is development. Inside of my /var/www I have the folders dev.mydomain.com and app.mydomain.com. Inside of each of these folders I have identical Laravel projects (aside from the config files pointing to different subdomains and databases).

I wanted to separate the locations to be able to test on different databases and also to test out new development files before pushing to production (with little to no downtime). I am able to access either subdomain one at a time by temporarily removing the config of the other in my Apache config files, but this is cumbersome and it causes downtime for both applications.

My .htaccess files appear to be correct because I can view a test url on both subdomains (by using simple .html files). When I try to access the subdomains with laravel installed I am getting strange results like being able to connect to the dev database while in the app subdomain OR being able to log into the app env, but not being able to login into the dev environment or vice versa.

I have been searching around stackoverflow and googling to try to find a solution, but nothing has come up so far. I have mainly found tutorials on how to setup

detectEnvironment  

with laravel like in the link here:

http://chrishayes.ca/blog/code/laravel-4-setting-utilizing-environments-environment-configuration

Does anybody have a solution for this or a better way of working with a dev/test environment in Laravel along side a production environment?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
adam2k
  • 369
  • 5
  • 14
  • Actually laravel is doing just the same as what Marcin is proposing (see [source](https://github.com/laravel/framework/blob/4.2/src/Illuminate/Foundation/EnvironmentDetector.php#L96)). If you check your testpage from both domains, what is echoed when you print gethostname() in PHP? These values are used as indexes for the environments array. – martinczerwi Oct 14 '14 at 08:26
  • Edit: Not indexes, but values for the environments array. The indexes can be arbitrary names. – martinczerwi Oct 14 '14 at 11:08
  • Thanks for the response. The hostname is the same for both subdomains because I am hosting from the same server. Is there some other work around to accomplish this or will I need to use another server for development? – adam2k Oct 14 '14 at 16:54
  • I think you should fine tune your apache config. Use NameVirtualHosts and VirtualHost to define two virtual hosts in the apache config. Each should have ServerName of the appropriate subdomain. Then you can run both domains in parallel without switching anything at runtime. Both domains will have two different hostnames, which allows you to use the classic setup for laravel environments. There is some config work to do, but you'll benefit from the extra effort. – martinczerwi Oct 14 '14 at 21:13
  • Thanks @martinczerwi, I have set up this server using AWS. Would the only way to set NameVirtualHost for two subdomains be by creating multiple IP addresses? Is there a way to do this with a single IP address? – adam2k Oct 17 '14 at 22:01
  • I must admit, I don't know my way around AWS, but this is usually possible with a single IP address. Both subdomains point to the same IP, and apache will figure out which VirtualHost to use. I've looked around for tutorials on this, and it seems, like you can configure Apache on AWS like on any normal machine. See this [post](http://calebogden.com/multiple-websites-amazon-ec2-linux-virtual-hosts/) (http://calebogden.com/multiple-websites-amazon-ec2-linux-virtual-hosts/) for example. On your server check out /etc/apache2/. There's a sites-available folder, which keeps all the Vhost configs – martinczerwi Oct 21 '14 at 13:39

1 Answers1

0

In bootstrap/start.php what you can do is to set environment depending on used domain:

$env = $app->detectEnvironment(function(){
        if (!isset($_SERVER['HTTP_HOST']) ||
            $_SERVER['HTTP_HOST'] =='dev.mydomain.com') {
            return 'dev';
        }
        return 'production';
    });

The only problem here is with artisan I believe - you will be able to use it only for one environment. In above code artisan will be used for dev environment (because if you use artisan $_SERVER['HTTP_HOST'] is not set and in above code for this dev development is being chosen.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291