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

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 :)

Related

Apache VirtualHost configure

After I configured virtual host, my apache document root changed to the virtual host's document root, I just want to know why.
here is my httpd-vhosts.conf:
<VirtualHost *:80>
ServerName myapp.zend
DocumentRoot /opt/lampp/htdocs/php_zend_projects/myapp
<Directory /opt/lampp/htdocs/php_zend_projects/myapp/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
<IfModule mod_authz_core.c>
Require all granted
</IfModule>
</Directory>
</VirtualHost>
After I restart apache server, localhost page changed to index of /opt/lampp/htdocs/php_zend_projects/myapp,
http://gwjyhs.com/t6/702/1556725814x2728329017.png
but it is supposed to be xampp's default page like this:
http://gwjyhs.com/t6/702/1556726269x2728278877.png
If you enable vhosts you have to add an entry that looks like:
<VirtualHost *:80>
DocumentRoot "F:/Dev/xampp/htdocs"
ServerName localhost
</VirtualHost>
Note: change path to whatever is appropriate for you.
Restart webserver and it should work as before.
Reasoning behind this can be found in a comment on top of the httpd-vhosts.conf:
The first VirtualHost section is used for all requests that do not match a ##ServerName or ##ServerAlias in any block.
That means when you type in localhost it fallbacks to your myapp.zend vhost because it is (probably) the first virtualhost section.

apache 2.4 serverAlias with * wildcard overwrittes specificed vhost

I have two vhosts the default one and another one:
<VirtualHost *:80>
ServerName www.eve-stuff.com
ServerAlias *.eve-stuff.com
DocumentRoot /var/www/html/test
<Directory /var/www/html/test>
Options +FollowSymLinks
AllowOverride None
#Require all denied
</Directory>
</VirtualHost>
This one should just redirect all subdomains that a not specifically setup to the test directory.
This is another vhost i have:
<VirtualHost *:80>
ServerName dev.eve-stuff.com
DocumentRoot /var/www/html/dev
<Directory /var/www/html/dev>
Options +FollowSymLinks
AllowOverride None
</Directory>
</VirtualHost>
This one should show the dev folder for the dev subdomain.
From what I understood from the apache doc the ServerAlias *.eve-stuff.com in the first host will always catch UNLESS another vhost has specifically been created for the address.
However dev.eve-stuff.com still redirects to the test folder not to the dev folder, as it should.
All dns entries, also for the subdomains, point to the right IP.
I am running Apache/2.4.18 (Ubuntu).
Turns out the order in which it is setup matters.
Apache apparently looks for the matching host it can find.
Since the first config was in 000-default.conf and the second one was in the 020-dev.conf the dev-subdomain matched the first vhost and used that.
I changed default to 999-default.conf which means now dev will first be matched with the proper vhost but other undefined subdomains will still be matched with the default vhost.

Configure Apache to run website off of port-enabled IP address

To be perfectly honest, I'm not even sure if this is doable...
I've configured my vhosts file in /etc/apache2/sites-enabled which you can see here:
<VirtualHost 159.203.171.140:8080>
ServerAdmin webmaster#localhost
ServerName 159.203.171.140:8080
DocumentRoot "/home/wiki/public_html"
DirectoryIndex index.php index.html
<Directory "/home/wiki/public_html">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/wiki_error.log
CustomLog ${APACHE_LOG_DIR}/wiki_access.log combined
</VirtualHost>
for a digitalocean droplet given at the IP listed in the above hosts file. This droplet has absolutely nothing on it except for the wiki user in /home/ plus the required php, mysql/mariadb, apache stuff.
What I want to be able to do is to go to 159.203.171.140:8080 and see my site without having to purchase a useless domain name.
I'd really appreciate some help with this one.
If you have only one website on the droplet, then you don't need to set up a virtual host. You can use the 000-default.conf, no need for a2ensite.
You do not need the ServerName, which won't work with the IP as a name, you also don't need the IP address in the VirtualHost directive.
So, instead of this:
<VirtualHost 159.203.171.140:8080>
ServerAdmin webmaster#localhost
ServerName 159.203.171.140:8080
DocumentRoot "/home/wiki/public_html"
...
You can use this in your 000-default.conf file
<VirtualHost *:8080>
DocumentRoot "/home/wiki/public_html"
...
The rest of the directive stays as you have it.
Also, one note, if you are using port 8080, then you need go to /etc/apache2/ports.conf and set the Listen to 8080 (restart Apache after doing this).

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.

Name Based Virtual Hosting url not found

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).