Get localhost to load website in directory not in /var/www/html

46 Views Asked by At

I am not an experienced Apache coder/developer. I've edited .conf files before, but it's been a minute or several.

I have Apache/2.4.52 on my Ubuntu 22.04 machine. For what it's worth, I use PHP8.1.2

How can I view my development websites (dev-sites) via localhost when the directory I have my websites in is not /var/www/html?

I build my sites(code) in /home/jdc44/Websites/ I.e: one such site is: /home/jdc44/Websites/LivingMuayThai There is a live website, but I want to make changes to the site/code and be able to view it locally before pushing any issues live. I would like to view the site at: localhost/livingmuaythai or camel-cased: localhost/LivingMuayThai as that is the name of the directory the code is in.

I'll post my code here and hope someone can tell me what I'm missing or how to do it if this is all wrong. I followed this tutorial from 2009, but it is not working. Maybe things have changed...

How to configure apache webserver on linux

I created a livingmuaythai.conf file in /etc/apache2/sites-available/

## /etc/apache2/sites-available/livingmuaythai.conf
## I removed all the commented lines for space

<VirtualHost *:80>
    
  # ServerName www.example.com
  ServerName livingmuaythai  // do I need a .com on this?

  ServerAdmin [email protected]
  DocumentRoot /home/jdc44/Websites/LivingMuayThai/

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

  <Directory /home/jdc44/Websites/LivingMuayThai/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>

  <FilesMatch "\.*">
    SetHandler application/x-httpd-php
  </FilesMatch>
  
</VirtualHost>

## /etc/hosts

127.0.0.1   localhost livingmuaythai

I enabled the site:

sudo a2ensite livingmuaythai

A symlink was created in /etc/apache2/sites-enabled/. The setup created this, not me.

livingmuaythai.conf -> ../sites-available/livingmuaythai.conf

Then restarted/reloaded Apache

When I go to http://localhost/livingmuaythai/index.html I get a 404 Not Found page.

Note: I have a firewall and port 80 is open.

Thoughts? Thanks for any help.

1

There are 1 best solutions below

1
IMSoP On

You have talked about enabling your new site, but not about disabling the default site, so my guess is you have both still set up. You can check this in two ways:

  • Look at the content of /etc/apache2/sites-enabled/ - as you've seen, this is the folder managed by the a2ensite and a2dissite commands, as a convenient way of turning sites on and off
  • Run sudo apache2ctl -S, which gives a breakdown of all the "virtual hosts" Apache thinks are configured, and where they're defined

Probably, you just want to run sudo a2dissite some-default-site-in-the-list


At this point, it's worth a quick detour to understand how Apache resolves sites:

  1. All configuration files are processed and combined, as though they were all in one file. All the Include lines and symlinks are just for human convenience.
  2. Apache receives a request from the browser, on a particular destination IP address and port, with a particular HTTP Host header or TLS SNI hostname. For instance, a request to the URL http://example.com/foo/bar/baz will have IP address 93.184.216.34 (that's what I currently get when I run nslookup example.com), port 80 and Host header example.com
  3. The IP address and port are matched against the <VirtualHost> blocks in the configuration.
  4. If no blocks mention the specific IP address and port, <VirtualHost> blocks specifying an IP address and/or port of * are matched.
  5. The matching blocks are examined in order, looking at the ServerName and ServerAlias lines they contain. The first time one is found that matches the requested hostname, that VirtualHost block is used.
  6. If none of the blocks has a matching ServerName or ServerAlias, the first block is used (out of the list matching IP address and port).

In your case, <VirtualHost *:80> means "any IP address, port 80", and ServerName livingmuaythai says this should be a good choice for URLs like http://livingmuaythai/foo/bar.

Your request http://localhost/livingmuaythai/index.html will look for, in order:

  1. <VirtualHost 127.0.0.1:80> with ServerName localhost or ServerAlias localhost
  2. The first <VirtualHost 127.0.0.1:80> block defined
  3. <VirtualHost *:80> with ServerName localhost or ServerAlias localhost
  4. The first <VirtualHost *:80> block defined