Name Based Virtual Hosting url not found - apache

I am trying to set up a website with name based virtual hosting. Now when I visit the website, lukej.us, I get a url not found error. Here is the conf file
<VirtualHost *:80>
ServerName www.lukej.us
ServerAlias lukej.us *.lukej.us
DocumentRoot /vars/www/html/lukej.html
</VirtualHost>
<Directory /vars/www/html/>
AllowOverride All
Order allow,deny
Allow from all
</directory>

DocumentRoot is supposed to be a directory. It is the root folder from which all files will be served. You attempted to specify a file, which then gets interpreted as a directory, since it expects a directory. So its trying to serve from the directory /vars/www/html/lukej.html/ which probably doesn't exist.
You probably wanted something like this:
<VirtualHost *:80>
ServerName www.lukej.us
ServerAlias lukej.us *.lukej.us
DocumentRoot /vars/www/html/
DirectoryIndex lukej.html
</VirtualHost>
This will serve files from the /vars/www/html/ directory, and will show the lukej.html as the index file (when you access the path / from the web).

Related

Configuring subdomain in Laragon

I'm using Laragon on Windows 10, and I want to create a subdomain for a new project,
The current structure is:
https://management.test
https://management.test/includes
https://management.test/client
I want to access the client folder using this URL:
https://client.management.test
But I don't want to have https://includes.management.test
I want the configuration to be only on the client folder.
I've tried playing with the Apache sites-enabled file but didn't get the result,
Can anyone share a working example with me or a simple solution to achieve this?
Thank you.
I just figured this out.
This "manual" solution worked for me
I edited drivers\etc\hosts and added my subdomain like
127.0.0.1 sub.project.test
Enable mod_vhost_alias.so in laragon\bin\apache[version]\conf\httpd.conf
Create a new file like {laragon folder}\etc\apache2\sites-enabled\sub.project.test.conf"
Add the following code in the file you just created
define ROOT "C:/laragon/htdocs/project/sub/"
define SITE "sub.project.test"
<VirtualHost *:80>
DocumentRoot ${ROOT}
ServerName ${SITE}
ServerAlias *.${SITE}
<Directory "${ROOT}">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "${ROOT}"
ServerName ${SITE}
ServerAlias *.${SITE}
<Directory "${ROOT}">
AllowOverride All
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile C:/laragon/etc/ssl/laragon.crt
SSLCertificateKeyFile C:/laragon/etc/ssl/laragon.key
</VirtualHost>
Replace ROOT to the path to the subdomain folder
Finally reload apache and visit the subdomain
References :
https://forum.laragon.org/topic/1705/help-subdomain-laragon/3
https://github.com/leokhoa/laragon/issues/90

Configure MAMP to treat each directory in Htdocs as Root in Apache mod_redirect

The issue is that when using MAMP for local development, the .htaccess file needs to differ in that the RewriteBase needs to specify the subdirectory for the specific site (as shown here on SO).
Is there a way to configure MAMP MAMP/conf/apachehttpd.conf so that each virtual site gets it's own "root"?
UPDATE
After getting a clue about Virtual Hosts:
Have updated /etc/hosts file to include:
127.0.0.1 ClientSite.localhost
Uncommented the line:
`#Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf`
in /Applications/MAMP/conf/apache/httpd.conf.
There is a directory called ClientSite in /Users/myname/Sites/.
This is the /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf content:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
ServerName localhost
ServerAlias *.localhost
VirtualDocumentRoot /Users/myname/Sites/%0
RewriteLogLevel 3
RewriteLog "/Applications/MAMP/logs/rewrite.log"
<Directory /Users/myname/Sites>
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Restarted the MAMP servers (and even ran dscacheutil -flushcache).
When browser is pointed to ClientSite.localhost it returns a 404: The requested URL / was not found on this server.
so that each virtual site gets it's own "root"?
You need to use VirtualDocumentRoot.
This is how I am using this on my MAMP in my /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf file:
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
ServerName localhost
ServerAlias *.localhost
VirtualDocumentRoot /Users/admin/htdocs/%0
RewriteLogLevel 3
RewriteLog "/Applications/MAMP/logs/rewrite.log"
<Directory /Users/admin/htdocs>
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Take note of VirtualDocumentRoot /Users/admin/htdocs/%0 directive. That makes each virtual site's root as:
VirtualDocumentRoot /Users/admin/htdocs/localhost
VirtualDocumentRoot /Users/admin/htdocs/dev.localhost
VirtualDocumentRoot /Users/admin/htdocs/client2.localhost
etc.
Then simply create a directory within /Users/admin/htdocs/ for each site named as above, like:
dev.localhost
client2.localhost
Remove (or rename) any .htaccess files during the process - and once websites confirmed to be accessible via url like: http://client2.localhost, .htaccess files should behave as expected.
Also be sure that in the /etc/hosts file, there's an entry like:
127.0.0.1 client2.localhost
for each URL in question.

What is the procedure to add domain aliases to an existing linux apache installation?

I have a personal VPS hosted in * and an ubuntu installation. The ubuntu runs apache,php,mysql and is currently being used for 5 websites mapped by virtualhosts. I am writing the whole procedure in case someone needs it.
When I want to add a new domain, I create an 127.0.0.1 test.com *.test.com row in /etc/hosts, add a new file in /etc/apache2/sites-available and run a2ensite test.com - then restart apache. Each website has its own folder in /var/www and the virtualhost entry looks like this :
<Virtualhost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin info#test.com
ServerName www.test.com
ServerAlias test.com *.test.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot "/var/www/test.com"
<Directory /var/www/test.com>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
</Virtualhost>
I am planning to add some aliases like aaa.test.com, bbb.test.com, ccc.test.com etc. which will point/forward to different folders. The aaa.test.com one will point to /var/www/aaa/index.php and the bbb.test.com to /var/www/bbb/index.php. To summarize, different aliases - same domain - different folders all in apache. How do I achieve that ?
There can only exist one DocumentRoot per VirtualHost container. Since you have specified different DocumentRoot for each aaa.test.com, bbb.test.com etc, you need to setup a separate VirtualHost for each:
<VirtualHost *:80>
ServerName aaa.test.com
DocumentRoot /var/www/aaa
DirectoryIndex index.php index.html
...
</VirtualHost>
and so on.
As aaa.test.com and bbb.test.com should point to different directories, You are required to create separate Virtualhost entries manually. Before that you have to remove the _*.test.com_ from the ServerAlias of test.com Virtualhost entry. Then create a file at /etc/apache2/sites-available , say aaa.test.com and add the following and then save
<Virtualhost *:80>
ServerName aaa.test.com
DirectoryIndex index.html index.php
DocumentRoot "/var/www/aaa/"
</Virtualhost>
make sure to restart/reload the apache service.
Do the same for bbb.test.com.. That is all you required to do... All d best :)

Apache: How to serve a path from a different directory without symlinking?

I have a one project being hosted in Apache under a specific subdomain:
<VirtualHost *:80>
ServerName dev.example.com
DocumentRoot /web/vhosts/dev.example.com
<Directory />
Allow from all
</Directory>
</VirtualHost>
I want to serve another project under a subdirectory of this domain:
dev.example.com/special-project
Which lives in /web/vhosts/special-project. I also can't simply create a symlink to this directory because of the way the original project is structured. It has to be configured using Apache only.
How do I configure Apache to be able to do this? Also, can this type of configuration be specified in a separate file, or does it have to be part of the original VirtualHost definition?
Use an Alias.
Alias /special-project/ /web/vhosts/special-project/
Just add another virtual host like:
<VirtualHost *:80>
ServerName special-project.example.com
DocumentRoot /web/vhosts/special-project
<Directory />
Allow from all
</Directory>
</VirtualHost>

I want to make a separate domain for images

I want to set up a domain called img.mydomain.com. It will be a virtual domain that just like my actual domain except for one difference: it will only serve files that end in .jpg .jpeg .gif .png etc. This way I can refer to img.mydomain.com/some_image.jpg. it will speed up the page speed by making the browser think that it's two separate domains (google page speed analyzer is always nagging me to do this).
I'm running apache on a linux server. Should I do this in httpd.conf? If so, what is my first step?
create 2 folder for each domains (for example):
/var/www/domain.com
/var/www/img.domain.com/
here's what you can put in your httpd.conf
<VirtualHost *:80>
DocumentRoot "/var/www/domain.com"
ServerName domain.com
ServerAlias domain.com www.domain.com
<Directory "/var/www/domain.com">
allow from all
Options +Indexes
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/img.domain.com/"
ServerName img.domain.com
ServerAlias img.domain.com
<Directory "/var/www/img.domain.com/">
allow from all
Options +Indexes
</Directory>
</VirtualHost>