example.com VirtualHost on local machine - apache

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

Related

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

404 Not Found in incorrect Apache configuration

I have a problem that apache configuration. Here my virtual host setting.
<VirtualHost *:80>
ServerName orocrm.75cl.sg
DirectoryIndex app.php
DocumentRoot /var/www/html/oro/web
<Directory /var/www/html/oro/web>
# enable the .htaccess rewrites
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
But, when I access to other menus, http://orocrm.75cl.sg/oro/web/app.php/ is always shown in front of the my targeted menu. So, it was shown 404 page. e.g http://orocrm.75cl.sg/oro/web/app.php/magento/cart/. It should be like that http://orocrm.75cl.sg/app.php/magento/cart/
May I know how do I fix this issue.
I recall that some versions of Apache come with a pre-configured vhost to point to the welcome.conf and such.
So I would suggest commenting the welcome.conf which is at /etc/httpd or on some distros /etc/apache2 and in those same directories find and remove any installation virtual hosts.
After that you should be free of conflicts as the pre defined virtualhost is like follows:
<VirtualHost *:80>
DocumentRoot "/var/www/welcome"
And that is all I can remember from last time looking at the file.
Enjoy

Apache won't start with added VirtualHost, but gives no error in error log

I am using EasyPHP (version 16.1.1) for Apache server (installed version 2.4.18 x86). I need to add a new VirtualHost, so now the whole VirtualHost section of httpd.conf looks like this:
# VIRTUAL HOSTS
## Virtualhost localweb
<VirtualHost 127.0.0.1>
DocumentRoot "C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www"
ServerName 127.0.0.1
<Directory "C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www">
Options FollowSymLinks Indexes ExecCGI
AllowOverride All
Order deny,allow
Allow from 127.0.0.1
Deny from all
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8080>
UseCanonicalName Off
ServerAdmin marekbuchtela#gmail.com
ServerName posis
ServerAlias posis.tovarna
VirtualDocumentRoot "C:\Users\marek\is\tovarna\public"
</VirtualHost>
The first one is the default one made by EasyPHP, the second one is added by me. However in this setup, the Apache won't start. When I remove the second VirtualHost, everything works fine, but with it, it just won't start at all. I have checked the error log and there is no output at all.
Any ideas why is this happening/how to fix it?
Thanks
Switch VirtualDocumentRoot "C:\Users\marek\is\tovarna\public"for DocumentRoot "C:\Users\marek\is\tovarna\public"
If by any chance you keep getting the error, show error_log output
There is another instance other than the answer given by ezra-s as to why EasyPHP server won't start and the Apache error log is empty.
When declaring paths in the vhosts file, it is important that the directories referenced in those paths exist. If you provide the following VirtualHost information, for example:
<VirtualHost *:80>
DocumentRoot "D:/MyApp/public_html"
ServerName myapp.local
ErrorLog "D:/MyApp/logs/error.log"
CustomLog "D:/MyApp/logs/apache.log" common
</VirtualHost>
...but you don't have a D: drive, or the directories D:/MyApp/ and D:/MyApp/logs/ don't exist, then you'll get a "silent fail" in Apache when starting the HTTP server from the EasyPHP console.

Wampserver shows 404 errors after swapping to using Vhosts

So I was using Wampserver for developing purposes, and I managed to set up the server for developing purposes (it worked with Dreamweaver, PHP worked etc.) but when accessing localhost, I get a 404 error.
I've hardly changed the settings, other than setting it to use that vhosts file, then I made a few new hosts in the hosts file in system 32, which all works fine.
localhost/phpmyadmin does work, however, so I am really stumped as to why it won't load anything. I also tried loading one of the site folders (localhost/Rufus) which also returned a 404. I have all the directories I need pointing to www folder, where all my files are saved.
And before anyone says, no it's not Skype or IIS, as they are both off and Wampserver is running on port 80 (I checked)
My vhosts file:
# Virtual Hosts
#
# Required modules: mod_log_config
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "c:/Apache24/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host2.example.com
DocumentRoot "c:/Apache24/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>
<Directory C:\Users\Tom\sites\wamp\www>
Order Deny,Allow
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot "C:\Users\Tom\sites\wamp\www\Rufus"
ServerName rufus.stone
</VirtualHost>
Remove the sample virtual hosts, assuming you aren't using them. You also need a virtual host for 'localhost' and it must be the first virtual host. After that you can include your other, custom vhosts.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:\Users\Tom\sites\wamp\www"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:\Users\Tom\sites\wamp\www\Rufus"
ServerName rufus.stone
</VirtualHost>
If you get a permission denied error try putting the directive I took out into your httpd.conf:
<Directory "C:\Users\Tom\sites\wamp\www">
Order Deny,Allow
Allow from all
</Directory>
It would go right under a similar directive that includes a Deny from all on the root.
Make sure you always restart Apache whenever you make changes to these files.
The error 404 is saying that you have not granted access to those Virtual Hosts, so Apache is kicking you out.
You need to include with your Virtual Host some information to tell Apache which locations are allowed access to each VHOST
You also need to make localhost a Virtual Host and because the first VHOST is the default VHOST it should really be the first you define, and it should always be limited to allow access only from the local machine.
So see if this works any better.
# Virtual Hosts
#
<VirtualHost *:80>
DocumentRoot "C:\Users\Tom\sites\wamp\www"
ServerName localhost
<Directory "C:\Users\Tom\sites\wamp\www">
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:\Users\Tom\sites\wamp\www\Rufus"
ServerName rufus.stone
<Directory "C:\Users\Tom\sites\wamp\www\Rufus>
AllowOverride All
Require local
</Directory>
</VirtualHost>
Require local says allow access from the PC running WAMPServer only.
If later you want to allow other PC's on your internal network to see the Apache server you can add to that like this, for example.
Require local
Require ip 192.168.1
This, assuming you are on the subnet 192.168.1.? will allow any PC connected to your router (wired or wifi) access to the server.
You should avoid these, unless you actually want the world to access your site
Require all granted
OR the Apache 2.2 syntax, which still works on Apache 2.4
Order Deny,Allow
Allow from all

Apache2 host two sites on the same server

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.