Apache2 host two sites on the same server - apache

I am trying to host subversion and wiki on the same site.
I have created two files in /etc/apache2/sites-available
in "subversion"
I have
<VirtualHost -myserverIP-:80>
ServerAdmin webmaster#localhost
ServerName svn.example.com
DocumentRoot /var/svn/repos
<Location /svn/repos>
DAV svn
SVNPath /var/svn/repos
AuthType Basic
AuthName "Subversion Repository"
AuthUserFile /etc/apache2/passwords
Require valid-user
</Location>
</VirtualHost>
and in "wiki" I have
<VirtualHost -myserverIP-:80>
ServerAdmin webmaster#localhost
ServerName wiki
ServerAlias -myserverIP-
DocumentRoot /home/www/wiki/html
<Directory /home/www/wiki/html>
AllowOverride None
Options -Indexes -MultiViews -SymLinksIfOwnerMatch +FollowSymLinks
Order allow,deny
allow from all
</Directory>
<Location /wiki>
</Location>
<DirectoryMatch "^/home/www/wiki/html/(data|conf|bin|inc)/">
Order allow,deny
Deny from all
Satisfy All
</DirectoryMatch>
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
ErrorLog /home/www/wiki/logs/error.log
CustomLog /home/www/wiki/logs/access.log combined
</VirtualHost>
However now, when I browse to the IP address of the server, I get the contents of the /var/svn/repos directory, wheras when I just had the wiki page, I got the wiki index page. I can actually go to -IPaddress-/svn/repos and see the SVN repository, but -IPaddress-/wiki does not work.
Thanks for the tips

Both your virtual hosts are on port 80 on the same machine. Apache can only guess which one you want to use. It is guessing the subversion one and browsing to the DocumentRoot specified.
You need to differentiate the virtual hosts, or browse to the server using the server name rather than IP.
Instead you could setup the SVN location inside your other virtual host.
Also consider using /etc/apache2/mods-available/dav_svn.conf depending on your Operating System.

However now, when I browse to the IP address of the server, I get the contents of the /var/svn/repos directory, wheras when I just had the wiki page, I got the wiki index page. I can actually go to -IPaddress-/svn/repos and see the SVN repository, but -IPaddress-/wiki does not work.
When you make a request to apache and the hostname you are using isn't defined by any ServerName or ServerAlias in any of the vhosts, apache returns the default vhost, which is always the first vhost in the file (or in the file with the name that's the smallest lexiconical order, e.g. 00-default_vhosts.conf). In your case, your SVN vhost is first so it is the "default" vhost. If you swap the 2 around the other one would be the "default" vhost.

Related

Can't add subdomain on LAMP VM Instance on Google Compute Engine

I'm trying to add the blog.mydomain.com subdomain in Apache on a Google Cloud LAMP VM Instance.
In the /etc/apache2/sites-available folder there are three files: default, default-ssl, and lamp-server. I assumed lamp-server is the file being used, so I copied this file to blog.mydomain.com and edited the contents to be:
<VirtualHost *:80>
ServerName blog.mydomain.com
ServerAlias blog.mydomain.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/blog.mydomain.com/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/blog.mydomain.com/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
After saving this new file I did sudo a2ensite blog.mydomain.com and then service apache2 reload. After doing this my main website (www.mydomain.com) went down and only would show a page that says "Error establishing a database connection". This message did not come from my application.
What am I doing wrong?
This doesn't make any sense. If you add blog.mydomain.com by issuing a2ensite which creates virtualhost record in sites-available and in sites-enabled which shouldn't impact your original website unless you changes something.
If there is an entry for www.mydomain.com in the virtualhost and its goes to the right folder, you should not get "Error establishing a database connection" I'm guessing this is a wordpress instance.
Make sure your blog.mydomain.com points to the server ip address. Ping it before adding it to the servers web servers configuration.
Also get rid of these lines from the blog.mydomain.com configuration:
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
and restart the web server and load blog.mydomain.com and www.mydomain.com.
The message "Error establishing a database connection" is probably coming from the /var/www/blog.mydomain.com/ directory.

Can't access symlinks within webdav folders [using apache 2.2.22] or map drive for explorer [windows]

I am using apache 2.2.22 with debian stable as the server.
I have created a virtual host for a webdav share. I can access all of the symbolic links contained in /webdav via a web browser, just not from a webdav client. If i put documents or proper folders within /webdav i can see them in a client.
Here is my config for the share:
<VirtualHost *:80>
ServerAdmin webmaster#mydomain.com
ServerName mydomain.com
ServerAlias *.mydoman.com
DirectoryIndex index.html
Options Indexes FollowSymLinks
DocumentRoot /var/www/html/www.mydomain.com/htdocs/
ScriptAlias /cgi-bin/ /var/www/html/www.mydomain.com/cgi-bin/
<Location /cgi-bin>
Options +ExecCGI
</Location>
ErrorLog /var/www/html/www.mydomain.com/logs/error.log
CustomLog /var/www/html/www.mydomain.com/logs/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#mydomain.com
ServerName mydomain.com
ServerAlias *.mydomain.com
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/mykey.crt
SSLCertificateKeyFile /etc/apache2/ssl/mykey.key
DirectoryIndex index.html
Options Indexes FollowSymLinks
DocumentRoot /var/www/html/www.mydomain.com/htdocs/
ScriptAlias /cgi-bin/ /var/www/html/www.mydomain.com/cgi-bin/
<Location /cgi-bin>
Options +ExecCGI
</Location>
ErrorLog /var/www/html/www.mydomain.com/logs/error.log
CustomLog /var/www/html/www.mydomain.com/logs/access.log combined
Alias /webdav /var/www/html/mydomain/htdocs
<Location /webdav-public>
Options Indexes FollowSymLinks
#AllowOverride None
DAV On
AuthType Basic
AuthName "webdav"
AuthUserFile /etc/apache2/webdav.password
Require valid-user
</Location>
</VirtualHost>
Permissions for root and webdav folder:
Owner: root, create/delete; Group www-data, access; Other, none
Permissions for the public folder which the symlinks point to:
Owner: myusername, create/delete; Group: www-data, access; Other, Access
Any ideas? Obviously i am trying to learn my way around apache so there is probably a few errors in the above, i welcome any off topic criticism.
So, it turns out:
<Location /webdav-public>
A location as above can be a symbolic link. However, symbolic links within the share location are not supported for whatever reason (at least for apache 2.2.22 in the debian stable repo). They do show when accessed by firefox or chrome for me, but not from a webdav client or while mapped in windows explorer (while on the home network, not tested elsewhere).
I had to rearrange my files to suit webdav, which sucks.

VirtualHosts configuration in one file or not?

I'm running a http server on Debian (Apache 2) with one IP address. I have few domains and SVN running on the server as well. At the moment I have configuration that points my domains to the correct folders on my server with VirtualHosts.
I have done all my VirtualHosts configurations only in the file called "/etc/apache2/sites-available/default". Is this the correct way to do it, or should I make a new file for every website I'm running on my server?
At this moment, my VirtuaHosts file (/etc/apache2/sites-available/default) looks like this:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.domain1.com
ServerAlias domain1.com
DocumentRoot /var/www/domain1
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain2.com
ServerAlias domain2.com
DocumentRoot /var/www/domain2
</VirtualHost>
<VirtualHost *:80>
ServerName svn.myhostname.com
DocumentRoot /var/svn
<Directory /var/svn/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
<Location />
DAV svn
SVNParentPath /var/svn
AuthType Basic
AuthName "Subversion"
AuthUserFile /etc/subversion/svn-auth
Require valid-user
</Location>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/svn.error.log
CustomLog ${APACHE_LOG_DIR}/svn.access.log combined
</VirtualHost>
There is no 'correct' way. You could also make an extra file for every Vhost. I for example have all my VHosts in /etc/apache2/httpd.conf and included it via Include httpd.conf in /etc/apache2/apache2.conf.
But if you want it to be a bit structured you CAN use the Apache2 built in VHosts System in /etc/apache/sites-availible/ with multiple files for every site (VHost).
If you want you could use nano /etc/apache2/sites-availible/mypage1 and then activate or deactivete it via the a2ensite command. Like a2ensite mypage1.

My server IP + all subdirectories are pointing to Subversion (SVN)

I installed a subversion on my server and it's working properly. All my repositories are located at /var/svn/repo1 /var/svn/repo2 etc... My /etc/apache2/sites-available/svn looks this:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName svn.myhostname.com
DocumentRoot /var/svn
<Directory /var/svn/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
<Location />
DAV svn
SVNParentPath /var/svn
AuthType Basic
AuthName "Subversion"
AuthUserFile /etc/subversion/svn-auth
Require valid-user
</Location>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/svn.error.log
CustomLog ${APACHE_LOG_DIR}/svn.access.log combined
</VirtualHost>
The problem is that when I open a browser and type my IP address or hostname they both point to SVN for some reason. For example, when I type only my IP the browser says "A username and password are being requested by MYIPADDRESS. The site says: "Subversion"". I think the only address that should be pointed to SVN would be svn.myipaddress.com, right??
When I type svn.myipaddress/repo1 it shows the repository normally though.
This causes me hard times as I cannot access my IP and open html/php-files there because it asks for SVN password every time.
Any thoughts on this?
Your initial <VirtualHost *:80> is redirecting all requests on port 80 (the standard http port) to the subversion directories. If you change that to something like <VirtualHost *:3690> then standard web requests should work as normal, though you'll need to add the port when accessing the repositories from the client, i.e. svn co http://repository.url:3690/repo1 in order for subversion commands to work.

example.com VirtualHost on local machine

What I am trying to do : To add a site (example.com) to apache2/sites-available (I have specified its document root) and then enable it and after that access it on my local machine (as example.com in browser). I have some following queries :
Is it possible to create a website and give a specific domain (example.com) to it?
What are the steps I need to follow in order to create a site, give a domain and access it on my local machine ONLY?
NOTE: I have the content of site. I want to create a new site and I do not want to change the default site(i.e. apache2/sites-available/default) of Apache.
UPDATE:
I faced a error while restarting Apache (I have added a VirtualHost as described)
ERROR
CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional "env=" clause (see docs)
Action 'configtest' failed.
The Apache error log may have more information.
...fail!
My site's file
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName dev.subhransu.com
ScriptAlias /private /home/hg/repositories/private/hgweb.cgi
<Directory /home/hg/repositories/private/>
Options ExecCGI FollowSymlinks
AddHandler cgi-script .cgi
DirectoryIndex hgweb.cgi
AuthType Basic
AuthName "Mercurial repositories"
AuthUserFile /home/hg/tools/hgusers
Require valid-user
</Directory>
ErrorLog ${APACHE_LOG_DIR}/dev.subhransu.com_error.log
# Possible values include: debug, info, notice, warn, error, cr$
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/dev.subhransu.com_ssl_access.lo$
SSLEngine on
SSLCertificateFile "/etc/apache2/ssl/dev.subhransu.com.crt"
SSLCertificateKeyFile "/etc/apache2/ssl/dev.subhransu.com.k$
</VirtualHost>
httpd.conf file
<VirtualHost *:80>
ServerName dev.subhransu.com
ServerAlias www.dev.subhransu.com
</VirtualHost>
I think the feature you may be looking for is vhosts. With vhosts you can create any number of sites, each setup independently of the other. For each vhost you can specify the 'ServerName' directive which is your domain, which can be anything you want it to be. And then in your computers hosts file you can route all your calls for that domain to localhost.
If you only have one site that you need to setup, just edit the default apache configuration, leaving vhosts alone, and then update the hosts file on your machine as I mentioned earlier.
References:
vhost examples
How to edit your hosts file
In httpd.conf (or vhosts), the ServerName and ServerAlias settings are what domain names Apache will respond to
ServerName example.com
ServerAlias www.example.com
then just edit your hosts file to point example.com to 127.0.0.1