TransWikia.com

How can I get ChromeOS/Linux to play nicely with localhost, VirtualHosts and local storage?

Super User Asked by Baddie on November 16, 2020

I’m pretty new to Linux and using localhost having really only developed using a self hosted, browser based IDE on a live site up until now. Using a Chromebook(v84, regular channel, Debian 10 Buster), I ultimately want to store my ongoing projects on the ChromeOS side, in various folders, and have them served in Chrome. Seems like it should be simple. Here’s some problems and other info.

  • Navigating to just localhost never resolves to anything
  • Navigating to penguin.linux.test serves me the Apache2 Debian Default Page or whatever index.* is at /var/www/html (Isn’t this what I’d expect to find when I navigate to localhost?)
  • PHPMyAdmin has been installed to /var/www/html/phpmyadmin and I can navigate to it at penguin.linux.test/phpmyadmin, but not at localhost/phpmyadmin
  • If I create /var/www/html/foo/index.php, it’s served at penguin.linux.test/foo without any further set up but not at localhost/foo
  • If I create a folder outside /var/www/html (this is my end game remember) and then create a symlink to it at /var/www/html/foo, nothing is served at penguin.linux.test/foo. Admittedly, this was expected, but it was worth a try.
  • If I use Web Server for Chrome, pointing to any local folder (either Linux side or ChromeOS side) I can find it at localhost:8887 but not at penguin.linux.test:8887. However, it only serves PHP files in plain text so it’s of limited use.
  • If I do something like docker run --rm -p 8080:80 -d hlsiira/atheos, it’s served at both localhost:8080 and penguin.linux.test:8080 just fine

I think its obvious I’ve fallen down a Google rabbit hole the last few days. lol. That’s why the story post. If anyone else falls down this hole, they’ll maybe get some help here too.

It doesn’t seem right that I have to use penguin.linux.test as my localhost… Docker and Web Server for Chrome are using it ok. Besides it being an awkward address to have to type before I even get to the good bit, having to store sites I’m developing in /var/www/html seems awkward as well. Even leaving my ChromeOS storage requirements out of it for now, any personal, ongoing projects should at least be stored in the Linux home folder, right?. For ease of access if nothing else. If it turns out it has to be a Linux folder and not ChromeOS, then I can cope if its the home folder. I think I’ve settled on VirtualHosts as being the answer, but it’s not working for me.

tl;dr

Assuming I want to store my site at ~/foo/public_html, I’ve created /etc/apache2/sites-available/foo.conf…

<VirtualHost *:80>
  ServerName local.foo
  DocumentRoot "~/foo/public_html"
</VirtualHost>

and run sudo a2ensite foo.conf and after that I added a line to /etc/hosts and run sudo systemctl restart apache2

127.0.1.1       penguin
127.0.0.1       localhost
127.0.0.1       local.foo      /* line I added */
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

If I go to local.foo, local.foo.localhost or localhost/local.foo I get "site can’t be reached". I’ve also tried various combinations of local.foo:80, local.foo:8080, 127.0.0.1, localhost:8080 with various combinations of… (in /etc/apache2/sites-available/foo.conf)

/* Using Listen (it just causes apache to crash) */
Listen 80
Listen 8080
Listen 127.0.0.1
Listen 127.0.0.1:80

/* changing the opening block */
<VirtualHost local.foo:80>
<VirtualHost localhost:80>
<VirtualHost 127.0.0.1:80>
<VirtualHost 127.0.0.1:8080>

/* various directives (not all at the same time obvs ) */
  ServerName local.foo
  ServerName localhost
  DocumentRoot "~/foo/public_html"
  ServerAlias local.foo
  ServerAlias localhost
  <Directory "~/foo/public_html">
    Options FollowSymLinks
    Options Indexes FollowSymLinks
    AllowOverride none
    AllowOverride All
    Require all denied
    Require all granted
  </Directory>       
</VirtualHost>

I’ve also repeated all this after running sudo a2dissite 000-default.conf.

And now I’ve run out of talent. What next?

One Answer

This answer also serves as a continuation of my journey (for anyone else coming behind me)

I think I've come to the conclusion that there's some kind of barrier that (currently) exists between the ChromeOS' localhost and Linux's localhost that I would need to fully understand and overcome if I'm gonna get what I want. Hopefully me spilling my brain out here inspires ideas of how to do that.

It seems to me that as far as the ChromeOS browser is concerned, Linux's localhost is located at penguin.linux.test or 100.115.92.197. So does this mean that Linux's /etc/hosts isn't actually in use until the ChromeOS browser URL is already pointing to those address? As far as I can tell, this renders ServerName and ServerAlias in Linux's /etc/hosts useless.

The ChromeOS browser obviously has its own hosts file which points to Linux's localhost, but as far as I can tell from Google its not editable. There must be some way of being able to manipulate the ChromeOS browser hosts though because Docker seems to be able to do it, as explained in my OP. I don't have any other leads down that rabbit hole though.

I know that if I was to install a browser into the Linux container (ironically, even a Linux version of Chrome itself in some kind of inception nightmare!) then it'd pretty much be smooth sailing as far as VirtualHosts and ServerName and /etc/hosts and localhost in that browser are concerned and I can locate my files wherever I want in the Linux environment. But I'm not installing a browser into Linux when I have a perfectly good browser in ChromeOS. (ok, maybe TOR Browser ;)


A working setup involves using Linux's localhost ports and a Chrome browser extension that works sufficiently for my needs for now, but I'm sure the real answer lies in manipulating ChromeOS's hosts somehow.

I restored the original /etc/hosts file, no need for changes there.

I found that if I want my VirtualHosts to point to a path in my home directory, I'll also need to tell /etc/apache2/apache2.conf to do it as me, which we can do from the Terminal with

/* Obviously, change 'baddie' to your user */

sudo sed -i "s|User root|User baddie|" /etc/apache2/apache2.conf
sudo sed -i "s|Group root|Group baddie|" /etc/apache2/apache2.conf

/* This only seems to be needed if you want to point your */
/* VirtualHosts to paths in your home folder */

For my VirtualHosts...

  • Since I don't seem to be able to use ServerName or ServerAlias, I think I have no choice but to handle them via ports.
  • I include the port number to Listen to and then open a VirtualHost block for that port number.
  • The DocumentRoot is the path to my project, wherever it is.
  • Since I want to store my projects outside /var/www/html I need to include a <Directory> directive to the same path as the DocumentRoot.
  • Provided my ChromeOS folders are shared with Linux, I can set the paths to point to something like /mnt/chromeos/MyFiles/projects/foo/public_html.
  • But I want to point to a Symlink in my home folder instead, so if I add Options +SymLinksIfOwnerMatch and Require all granted to the Directory directive, that will work too.
  • I need to use absolute paths for the DocumentRoot and Directory paths and not use shortcuts like ~.

What I end up with in /etc/apache2/sites-available/foo.conf is...

Listen 12001

/* Obviously, change 'baddie' to your user
/* Use absolute paths */

/* This points to a Symlink in my home folder which in turn */
/* points to /mnt/chromeos/MyFiles/projects/foo/public_html /*
<VirtualHost *:12001>
  DocumentRoot "/home/baddie/Projects/foo"
  <Directory "/home/baddie/Projects/foo">
    Options +SymLinksIfOwnerMatch
    Require all granted
  </Directory>
</VirtualHost>

After restarting the server, this successfully allows me to access that project at penguin.linux.test:12001. But I have lots of projects, so I have to create one of these .conf files for each one and then sudo a2ensite each of them and restart the server again.

The last piece of the puzzle is to be able to type in a human friendly URL without having to remember the associated port. Since I can't change ChromeOS' hosts file and ServerName and ServerAlias in Linux's /etc/hosts are useless, I'm using a Chrome browser extension called Host name rewriter. With this I can have a desired URL like foo.local and have that be rewritten to penguin.linux.test:12001.

This is the clunkiest part of the whole operation though. If I don't include http:// in the address bar then the browser just searches Google for foo.local instead. If it is included then the browser first of all resolves the page as 'The site can't be reached' and then a second or so later, the browser is finally redirected. After redirection, the URL shows penguin.linux.test:12001 which is to be expected, but it no longer looks human friendly. Ah well, at least I got there without having to remember the port number... eventually


XAMPP Through one trial and error attempt or another, I've ended up ditching a standalone Apache install and now have XAMPP. To make the above workaround work on XAMPP, there are a few differences.

Instead of creating individual .conf files for each project and then running sudo a2ensite project_name.conf we instead need to first edit /opt/lampp/etc/httpd.conf and tell it to use /opt/lampp/etc/extra/httpd-vhosts.conf. We can do that from the Terminal with

sudo sed -i "s|#Include etc/extra/httpd-vhosts.conf|Include etc/extra/httpd-vhosts.conf|" /opt/lampp/etc/httpd.conf

We also use /opt/lampp/etc/httpd.conf to tell Apache to run as me, which we can do from the Terminal with

/* Obviously, change 'baddie' to your user */

sudo sed -i "s|User daemon|User baddie|" /opt/lampp/etc/httpd.conf
sudo sed -i "s|Group daemon|Group baddie|" /opt/lampp/etc/httpd.conf

/* This only seems to be needed if you want to point your */
/* VirtualHosts to paths in your home folder */

/opt/lamp/etc/extra/httpd-vhosts.conf contains a bunch of example data that can all be removed and replaced with the same information that went into the individual .conf files above. So we get...

/* There doesn't seem to be a way to listen to a range */
/* of ports so each one you want to listen to has to be */
*/ individually stated */
Listen 12001
Listen 12002
Listen 12003

/* Obviously, change 'baddie' to your user */
/* Use absolute paths */

/* This points to a Symlink in my home folder which in turn */
/* points to /mnt/chromeos/MyFiles/projects/foo/public_html /*
<VirtualHost *:12001>
  DocumentRoot "/home/baddie/Projects/foo"
  <Directory "/home/baddie/Projects/foo">
    Options +SymLinksIfOwnerMatch
    Require all granted
  </Directory>
</VirtualHost>

/* This points to a Symlink in my home folder which in turn */
/* points to /mnt/chromeos/MyFiles/projects/bar/public_html /*
<VirtualHost *:12002>
  DocumentRoot "/home/baddie/Projects/bar"
  <Directory "/home/baddie/Projects/bar">
    Options +SymLinksIfOwnerMatch
    Require all granted
  </Directory>
</VirtualHost>

/* This points to a Symlink in my home folder which in turn */
/* points to /mnt/chromeos/MyFiles/projects/baz/public_html /*
<VirtualHost *:120003>
  DocumentRoot "/home/baddie/Projects/baz"
  <Directory "/home/baddie/Projects/baz">
    Options +SymLinksIfOwnerMatch
    Require all granted
  </Directory>
</VirtualHost>

As I said, this set up is satisfactory for me now... Or it would be except I'm back to storing my projects in the Linux system instead of the ChromeOS system because Git won't work from ChromeOS folders! Oh well. But that's for another post.

Answered by Baddie on November 16, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP