Apache disable localhost redirection - apache

i got a problem with apache conf with Laragon.
so the case is :
i got 2 subdomain
subdom1.mysite.com (DNS Record to IP 1.2.3.4)
subdom2.mysite.com (DNS Record to IP 1.2.3.4)
IP 1.2.3.4 is my VPS
everything went well, but i got a problem.
when i access 'localhost' from my VPS, it keeps redirecting to subdom1.mysite.com
and localhost/phpMyAdmin also redirected to subdom1.mysite.com/phpMyAdmin
here's my httpd.conf
Define APACHE_LOG_DIR "C:/mydir/logs"
Define APACHE_ROOT_WEB_DIR "C:/mydir/wwwroot"
ServerName localhost
DocumentRoot "C:/mydir/wwwroot"
<Directory "C:/mydir/wwwroot">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
ServerName subdom1.mysite.com
ServerAlias subdom1.mysite.com
DocumentRoot ${APACHE_ROOT_WEB_DIR}\subdom1.mysite.com
ErrorLog ${APACHE_LOG_DIR}/subdom1.mysite.com-error.log
CustomLog ${APACHE_LOG_DIR}/subdom1.mysite.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName subdom2.mysite.com
ServerAlias subdom2.mysite.com
DocumentRoot ${APACHE_ROOT_WEB_DIR}\subdom2.mysite.com
ErrorLog ${APACHE_LOG_DIR}/subdom2.mysite.com-error.log
CustomLog ${APACHE_LOG_DIR}/subdom2.mysite.com-access.log combined
</VirtualHost>
my localhost keeps redirecting to first vHost. I've googled lot's of combination of directory, serverName etc. But no luck.

Your VirtualHosts are binding to all available interfaces. Because of how apache determines to which VHost should route a request, it's ending up in the first one defined.
You can either:
Bind your subdomains to the external ip only.
Create a VirtualHost for the main server using the loopback ip.
Create a VirtualHost with the _default_ address.

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

CentOS Apache VirtualHost default

I have set up some virtal host to distinguish between my subdomains (/etc/httpd/conf.d/*.conf).
This was working on my old server (Debian 7). But now if I browse the root page I get the first defined VirtualHost displayed.
Also if I am browsing an undefined subdomain I get the first VirtualHost config displayed.
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName sub1.example.com
ServerAlias sub1.example.com
DocumentRoot /home/sub/sub_sub1/www
ServerAdmin Admin
CustomLog /home/sub/sub_sub1/logs/access.log combined
ErrorLog /home/sub/sub_sub1/logs/error.log
LogLevel warn
<Directory /home/sub/sub_sub1/www>
Require all granted
</Directory>
</VirtualHost>
The first defined VirtualHost will serve all the requests that don't get handled by the other ones. You can create a default VirtualHost that handles those requests. To make sure this VirtualHost is the first one, you should name its config-file something like "000-default.conf".
This might also help you: VirtualHost Examples

SubDomain and VirtualHost

I have a domain on 1and1 with dns setup to point to a Amazon EC2 Linux instance..
I also have a subdomain with the same dns settings: pma.mydomain.com
I need to setup that subdomain to point to /usr/share/phpMyAdmin instead of /var/www/html (Document root of Apache)
I tried to create a VirtualHost inside httpd.conf with the following:
<VirtualHost *:80>
ServerAdmin webmaster#mydomain.com
DocumentRoot /usr/share/phpMyAdmin
ServerName pma.mydomain.com
ErrorLog logs/pma.mydomain.com-error-log
CustomLog logs/pma.mydomain.com-custom-log common
<Directory "/usr/share/phpMyAdmin/">
Allow From All
Options +Indexes
</Directory>
</VirtualHost>
The problem is that I see the content of /usr/share/phpMyAdmin also by visiting www.mydomain.com.
I think I'm missing something, it is the first time I try to setup a Virtual Host.

Apache IP Virtual Hosts

Server has two IPs, fresh centos min install. Apache is working, both ips load Apache test page. both www.domain.com and domain.com resolve to second IP.
I'd like for the first IP (192.168.0.1) to load Apache test page, this is working fine
I want the second IP (192.168.0.2) to load a website in /home/site/www
Currently when we goto domain.com or www.domain.com or 2nd IP it loads apache test page instead of the site, here's our config. Also I have the IPs listed as 192 instead of the real ips. What am I missing? Why isn't 192.168.0.2 loading /home/site/www instead of the Apache test page?
ServerRoot "/etc/httpd"
Listen 80
ServerName 192.168.0.1:80
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
NameVirtualHost 192.168.0.2:80
<VirtualHost 192.168.0.2:80>
DocumentRoot /home/site/www
ServerName mydomain.com
ServerAlias *.mydomain.com
ErrorLog logs/mydomain.com-error_log
CustomLog logs/mydomain.com-access_log common
</VirtualHost>
Update
The Fix
chcon -R --reference=/var/www /home/site/www
SELinux needed the correct permissions set on it, using the reference it copies the same permissions to my new folder
Try this:
ServerRoot "/etc/httpd"
Listen 80
ServerName 192.168.0.1:80
NameVirtualHost 192.168.0.1:80
NameVirtualHost 192.168.0.2:80
<VirtualHost 192.168.0.1:80>
DocumentRoot /var/www/html
ServerName mydomain.com #change accordingly
ServerAlias *.mydomain.com
ErrorLog logs/mydomain.com-error_log
CustomLog logs/mydomain.com-access_log common
</VirtualHost>
<VirtualHost 192.168.0.2:80>
DocumentRoot /home/site/www
ServerName mydomain2.com
ServerAlias *.mydomain2.com
ErrorLog logs/mydomain2.com-error_log
CustomLog logs/mydomain2.com-access_log common
</VirtualHost>
Don't forget to apply the changes on apache.
service httpd reload or similar command.
Also, make sure the directory /var/www/html has, at least, reading permissions for the apache user.
You are missing the NameVirtualHost directive.
NameVirtualHost 192.168.0.2:80
I would also highly suggest putting in Directory directives in as well.

Why does http://localhost redirect to my default virtual host once I setup virtual hosts in Apache?

This is probably an easy question, but I want to understand better how Apache works with virtual hosts. I am setting up virtual hosts because I work on multiple websites at once and I don't want to use subdirectories. I was pretty much using the default Apache httpd.conf file with the DocumentRoot pointing to something like "/www". I uncommented the virtual hosts include and added the following:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site1.dev
DocumentRoot /www/site1
</VirtualHost>
<VirtualHost *:80>
ServerName site2.dev
DocumentRoot /www/site2
</VirtualHost>
Now when I go to http://localhost I get the default page for site1.
I'm sure there is a reason why this makes sense, but I don't quite understand it. I would've thought that only requests that were explicitly to http://site1.test would get routed through that directive and it wouldn't just become the default. Can someone explain why it becomes the default.
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
(Should be true for 2.x also)
"If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a container and list it first in the configuration file."
answer 1 is correct
and i'd add with namevirtualhosts as the first entry
essentially matches any not-named elsewhere virtualhost
it should ONLY be used to catch unintentional mal-formed and broken traffic
ie a machene with one ip called john.domain.com running www.domain.com and www.domain2.com as valid webservers on ip www.xxx.yyy.zzz might have an optimal config like thus
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a directory readable by apache with only a robots.txt denying everything
ServerName bogus
ErrorDocument 404 "/errordocuments/unknown-name.html"
#custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine#
ErrorLog /var/log/httpd/unknown-error.log
CustomLog /var/log/httpd/unknown-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a possibly different directory readable by apache with only a robots.txt denying everything
ServerName www.xxx.yyy.zzz
ServerAlias john.domain.com
ErrorDocument 404 "/errordocuments/ip-name.html"
ErrorDocument 403 "/errordocuments/ip-name.html"
#custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/
ErrorLog /var/log/httpd/ip-error.log
CustomLog /var/log/httpd/ip-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
RedirectPermanent / http://www.domain.com/
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain
ServerName www.domain.com
ServerPath /domain
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
RedirectPermanent / http://www.domain2.com/
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain2
ServerName www.domain2.com
ServerPath /domain2
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
Confirming that for Apache 2.x, the first virtual host (with the same port number) will be used if a matching virtual host is not found.
http://httpd.apache.org/docs/2.2/vhosts/details.html
"If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected"
You can always add this code below, put it right below NameVirtualHost *:80 so that your default document root is served by default if no other virtual hosts found.
<VirtualHost *:80>
ServerName localhost
DocumentRoot /my/default/document/root
</VirtualHost>
Simply put this code at top in httpd-vhosts.conf
<VirtualHost localhost:80>
ServerName localhost
DocumentRoot d:/xampp/htdocs
<Directory "d:/xampp/htdocs/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
One way to do this is:
In your VirtualHosts configuration, enter the specific local site name you want to enable instead of using a wildcard:
<VirtualHost site1.dev:80> instead of <VirtualHost *:80>
Switch off NameVirtualHost *:80 which can be done by commenting it out in your vhosts.conf file
In your /etc/hosts file mention both aliases for the loopback IP:
127.0.0.1 localhost site1.dev
That's it. You should see that localhost goes to the default DocumentRoot as usual and the site1.dev goes to the site you've setup as virtual host.