cromartie
By:
cromartie

Is it possible to install laravel alongside other websites (on localhost) ?

May 23, 2017 141 views
Apache Ubuntu 16.04

I installed laravel on ubuntu 16.04 in that folder /var/www/html/
But when i use this :
sudo a2dissite 000-default.conf
sudo a2ensite laravel.conf
My localhost does not display the contents of the folder.
What i want is localhost/laravel to show the laravel page
and localhost to show the other websites

4 Answers

@cromartie

One VirtualHost handles one domain or sub-domain, such as domain.com (basic example).

So if, for example, 000-default.conf was setup to handle domain.com, it would also handle:

domain.com/dir01
domain.com/dir02
domain.com/dir03

or even:

domain.com/laravel

VirtualHosts only need to be setup for domains and sub-domains.

If you can post the contents of both files to a code block (click the </> icon and post the contents between the backticks), we can take a look at them to see if there's anything that may cause issues.

So i need to create a virtual host file for each of my website , even a wordpress one?

/var/www/html/domain.com -> /etc/apache2/sites-available/domain.com.conf
/var/www/html/test.com -> /etc/apache2/sites-available/test.com.conf

and disable the default conf -> sudo a2dissite 000-default.conf

Or is it better to put all the websites not done with laravel on a folder inside /var/www/html/examples
and create a virtual host file for this folder -> /etc/apache2/sites-available/examples.conf

@cromartie

It really depends on how you want to set things up. For example, if I was to setup a new site, I would probably use something such as this for my VirtualHost. I'll use domain.com as my filler domain -- you'd simply replace that with your own.

<VirtualHost *:80>
    ServerName domain.com
    ServerAlias www.domain.com
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The above sets up a standard Port 80 (HTTP) VirtualHost block that responds to:

http://domain.com
http://www.domain.com

and

domain.com
www.domain.com

The type of site doesn't matter. You could drop WordPress in ./html, or Laravel, or a static site. The configuration, in this case, doesn't dictate what kind of sites you're hosting.

For each domain or sub-domain, you do need a VirtualHost set up though. So if you wanted to set up domain02.com, domain03.com, domain04.com, etc, you'd need VirtualHosts for each of them, and they would be similar.

For each one, you need to change ServerName, ServerAlias, and DocumentRoot.

How you access the sites depends on setup as well.

You can't access multiple sites on the same domain, but you could separate them by directory or even by sub-domain. As in, you can't visit domain.com and choose which site you want to navigate to, you need to separate them out by directory or sub-domain, or even domain.

For example, you'd create directories for each site:

/var/www/html/laravel01
/var/www/html/laravel02
/var/www/html/laravel03
/var/www/html/wordpress01
/var/www/html/wordpress02
etc

With the VirtualHost I used above, you'd then access them using:

domain.com/laravel01
domain.com/laravel02
domain.com/laravel03
domain.com/wordpress01
domain.com/wordpress02

You can install Laravel in sub folder along side of your other websites. To install Laravel in a subfolder let’s walk through a typical server setup to demonstrate it. Typically the path to the web root is something like this, but it can vary depending on your host:

/var/www/html

With this path, the “html” folder is the web root, and your domain will typically point here. Now, let’s say you are building a blog and you create /var/www/html/blog. All the contents of your Laravel “public” folder would go in this folder. For example:

/var/www/html/blog/css/
/var/www/html/blog/js/
/var/www/html/blog/index.php

You will then put the rest of your Laravel folders and files in another place outside of “html”, for example:

/var/www/laravel/
/var/www/laravel/app/
/var/www/laravel/bootstrap/
/var/www/laravel/.env
/var/www/laravel/...

Then for the final steps modify your “blog/index.php” file and adjust the following two paths.

// blog/index.php change to point to /var/www/laravel/

require __DIR__.'/../bootstrap/autoload.php'; 
$app = require_once __DIR__.'/../bootstrap/app.php';

This will keep everything outside your web root, make your install more secure, and prevent unauthorized access to import items.

Have another answer? Share your knowledge.