Virtual host works on phone, but it loads the wrong thing - apache

I have MAMP running in my computer and the virtual host project.dev configured and working correctly (have been working on the project for years). Now I added project.dev to the hosts file on my Android phone, pointing to my local IP.
I access http://project.dev in mobile Chrome and it reaches my MAMP just fine, but it loads whatever is in /htdocs (basically as if I accessed "localhost" on my computer's browser), instead of loading the virtual host. This is how I've defined the virtual host in my httpd.conf file:
NameVirtualHost project.dev
<VirtualHost project.dev:80>
ServerName project.dev
ServerAlias *.project.dev
DocumentRoot "/project/public/root"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^(.+)\.(js|css|png|jpg|swf|ico|txt|html|mp3)$
RewriteRule ^/(.+) /index.php [QSA,L]
AddDefaultCharset utf-8
</VirtualHost>
Why isn't it executing that rule and loading the correct file and defaulting to MAMP's htdocs directory?

<VirtualHost :80> # Specify IP here, not hostname. You can leave it at :80 too (Listen on any)
ServerName project.dev # For HTTP Host header
ServerAlias *.project.dev # Same thing. I'm not sure if wildcards work.
DocumentRoot /project/public/root
yada yada yada
</VirtualHost>

Related

Apache not loading document root correctly

Apache shows default page and doesn't load site configuration. OS: Debian 10.
Site is enabled but somehow it doesn't show files from public_html folder. Any help is appreciated.
<VirtualHost *:443>
SSLEngine On
ServerAdmin admin#abc.com
ServerName abc.com
ServerAlias *.abc.com
DocumentRoot /home/xx/public_html
SSLEngine on
SSLCertificateFile /home/xx/ssl.cert
SSLCertificateKeyFile /home/xx/ssl.key
<Directory /home/xx/public_html>
Require all granted
</Directory>
ErrorLog /home/xx/logs/error.log
CustomLog /home/xx/logs/access.log common
LogLevel debug
</VirtualHost>
No enough reputation to comment, so I’m trying with an answer and will clean it up if useful.
No mention of what you’re finding, if anything, in your logs. I assume you’re accessing using HTTPS to be sure your requests are going to port 443, but if per chance you were not I would try that first by specifying the protocol when entering the URL in your browser - otherwise you are probably making your request to the server on port 80 and not 443 where your VirtualHost is listening.
http://example.com ====> browser sends request to port 80, default port for http
https://example.com ====> browser sends request to port 443, default port for https
Is there also a VirtualHost entry for port 80 to redirect those requests to 443? If your browser is trying to load it as http using port 80 first then perhaps that’s why you’re seeing the Apache default page as I believe the server will be attempting to serve from /var/www/html/ for requests on port 80 unless you have already pointed these elsewhere with another VirtualHost, etc.
An example of what I mean that I have in use; either the ReWriteEngine or the Redirect permanent may be redundant, but I can confirm it functions fine for me as follows:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
Redirect permanent / https://www.example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
For my setup, I preferred that the www subdomain be default and set up DNS intentionally to treat it as such, so the bare domain is ServerAlias in my instance.

Virtual host doesn't work on WAMP and virtual host url causes to download a file in browser

I have a strange problem in accessing to virtual host in WAMP.
I have defined my virtual host (it calls mvc.dev) by defining in server configuration page in wampserver. Steps are exact that I have explained in the following:
1- first, going to server configuration page and select Add a virtual host
2- As index.php in my project is in C:\wamp64\www\mvcdev\public path, so I fill out related fields for my virtual host and then start to creation of virtual host.
3- It is created correctly and wamp server wants to restart DNS manually. I restarted it from wamp server menu like this:
4- After restarting, I check localhost page. It shows that mvc.dev is created correctly without any problem
5- And the problem occurs here, when I click on mvc.dev or type it in address bar of browser to access my virtual host, it causes to download a file instead of loading index.php file. I'm so frustrated about this issue and I don't know how can I solve this. Why does it cause to download hex file instead of executing URL?
This file is a hex file and every time that I put mvc.dev in url, it will start to download this file
For your information, I have also tested it by defining virtual host manually. I put related codes in httpd-vhosts.conf and then define mvc.dev in host and then restart DNS, but the result is the same with thing that I have explained before and I can not access to my virtual host at all. You can see it in the following:
configuration in host file:
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 localhost
127.0.0.1 mvc.dev
::1 mvc.dev
configuration of httpd-vhosts.conf:
#
# Virtual Hosts
#
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName mvc.dev
DocumentRoot c:/wamp64/www/mvcdev/public
<Directory "c:/wamp64/www/mvcdev/public/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
How can I solve this problem?
Note: I also checked this one by one but I couldn't solve my problem:
project-links-do-not-work-on-wamp-server
The .htaccess file is
# Remove the question mark from the request but maintain the query string
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
The code in index.php is
echo 'Request URL is: '.$_SERVER['QUERY_STRING'];
//require '../core/Router.php';
require "../vendor/autoload.php";
$router = new Core\Router();
$router->add('/','HomeController#index');
I had the same issue and I found out that Chrome requires any *.dev to be access via HTTPS and any chromium base browser will negate localhost access to it. Check this link
Change the website extension to something else. That worked for me. For example instead of example.com use example.tst.

Can't bind virtual host to different port with httpd (CentOS)

I have a VDS with CentOS and Apache. I'm using virtual-based hosts so I have domain 'my.domain' and few subdomains like 'sub1.my.domain', 'sub2.my.domain' etc. Each of them have it own config file in /etc/httpd/conf/vhosts/.
I need to redirect all connections from sub1.my.domain:8080 to another.web.site:8080. So I've added this lines in .htaccess file at sub1.my.domain's root directory:
RewriteEngine On
RewriteCond %{SERVER_PORT} 8080
RewriteCond %{REQUEST_URI} ^/
RewriteRule (.*) http://another.web.site:8080
But it wouldn't work because web-server listen only to port 80. So I did modified /etc/httpd/httpd.conf this way:
Listen 80
Listen 8080
And did modified sub1.my.domain's vhost file. Instead of...
<VirtualHost myserverip:80>
ServerName sub1.my.domain
AddDefaultCharset off
AssignUserID www-root www-root
DirectoryIndex index.html
DocumentRoot /var/www/www-root/data/www/sub1.my.domain
...
</VirtualHost>
...it now contents
<VirtualHost myserverip:8080>
ServerName sub1.my.domain
AddDefaultCharset off
AssignUserID www-root www-root
DirectoryIndex index.html
DocumentRoot /var/www/www-root/data/www/sub1.my.domain
...
</VirtualHost>
So as described in official documentation I've added new port to listen in httpd configuration and setup new port to resolve in domain's virtual host configuration. But when I'm trying to open sub1.my.domain:8080 I get an error - browser can't resolve that address. I've even forward 8080 port through iptables, reboot whole server but nothing helped.
What I did wrong?
Whole day spent to understand that fail2ban blocks all connections to 8080. I've added rule to fail2ban table through iptables and everything worked like a charm.

How to correctly set Documentroot in Apache serving Plone

I have a Plone site called example.com located at /var/www/Plone (I think). I have the following settings for the site located in sites-available for vhosts (excerpt):
<VirtualHost 10.0.1.4:8082>
ServerAdmin webmaster#localhost
ServerName wiedhas.noip.me
DocumentRoot /var/www/Plone
When I try to reach my site wiedhas.noip.me, apache loads the Plone directory tree and not my Plone site. I can browse through the file system of /var/www/Plone but it is not loading the site. I must not have set the documentroot to the correct directory of my site? Any help much appreciated.
This an excellent docu about running plone behind apache and more.
http://docs.plone.org/manage/deploying/front-end/apache.html
A simple example with ssl, how a vhost could look like:
<VirtualHost $IP:80>
ServerName my.domain.com
Redirect / https://my.domain.com
</VirtualHost>
<VirtualHost $IP:443>
ServerName my.domain.com
ErrorLog logs/my.domain.com-http-error.log
CustomLog logs/my.domain.com-http-access.log combined
Include vhosts.d/....ssl.inc
RewriteEngine On
RewriteRule ^/(.*) http://127.0.0.1:$PORT_OF_PLONE/VirtualHostBase/https/%{SERVER_NAME}:%{SERVER_PORT}/zodb/path/top/plone/VirtualHostRoot/$1 [P,L]
</VirtualHost>
The most important part is the rewrite rule:
RewriteRule ^/(.*) http://127.0.0.1:$PORT_OF_PLONE/VirtualHostBase/https/%{SERVER_NAME}:%{SERVER_PORT}/zodb/path/top/plone/VirtualHostRoot/$1 [P,L]
$PORT_OF_PLONE = Port of your running plone instance
/zodb/path/top/plone = That's where you added the plone site in zope.
Took me a while to get mine going so maybe this helps:
My vhosts looks like this (where my plone site is called 'mywebsite'):
#---------------------------------
# www.mywebsite.com
#---------------------------------
<VirtualHost *:80>
ServerName www.mywebsite.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/(.*)$ http://127.0.0.1:8081/VirtualHostBase/http/%{SERVER_NAME}:80/mywebsite/VirtualHostRoot/$1 [L,P]
</IfModule>
</VirtualHost>
Hope that helps :)
As you see in the first and correct answer you do not need DocumentRoot. DocumentRoot points to a directory with files to render by Apache. But Plone brings it's own server, the Zope application server, which runs on a different port than Apache. The RewriteRule redirects the incoming request to the application server and modifies the response in a way that the redirection is hidden for the client.

VirtualHost setup always doesn't work

I'm trying to set up a virtualHost for mampstack (NOT MAMP). This is what I've done so far:
In my httpd.conf file I've checked
Listen 8080
This is correct (I'm listening to the port 8080, NOT 80).
Then I've uncommented: Include conf/extra/httpd-vhosts.conf in my httpd.conf file
In my hosts file I have added the following: 127.0.0.1 mext-pst.local.
In httpd-vhosts.conf I've added:
NameVirtualHost *:8080
<VirtualHost *:8080>
DocumentRoot "/Applications/mampstack-5.4.20-0/apache2/htdocs"
ServerName 127.0.0.1
ServerAlias localhost
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN localhost
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot "/Applications/mampstack-5.4.20-0/apache2/htdocs/mext-pst-dashboard/web"
ServerName mext-pst.local
ServerAlias mext-pst.local
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN mext-pst.local
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^ http://%{HTTP_HOST}:8080%{REQUEST_URI} [P]
</VirtualHost>
Now when I go to http://mext-pst.local/ I just get an error of my browser that he can't connect with the page ... .
When I go to http://mext-pst.local:8080/ I get the following error:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /index.php.
Reason: DNS lookup failure for: mext-pst.local:8080
When I go to http://mext-pst.local:8080/index.php it works ...
Change 8080 to 80 its the default. But if you want your site to run on 8080, then you have to use it. Another solution might be to rewrite the url, that is when your server gets the url, it rewrites it with port number (8080).
First of all change Listen 8080 to Listen 80, as you want your application to be accessible only with http.
In your http-vhost.conf file put following lines (of course after removing previous changes). In the following configuration yourDefaultHttpFolder means the default http folder. You might have changed it. So correct it depending on your system.
<VirtualHost *:80>
DocumentRoot "/Applications/mampstack-5.4.20-0/apache2/htdocs/yourDefaultHttpFolder"
ServerName 127.0.0.1
ServerAlias localhost
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN localhost
<Directory /Applications/mampstack-5.4.20-0/apache2/htdocs/yourDefaultHttpFolder>
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^ http://%{HTTP_HOST}:8080%{REQUEST_URI} [P]
</Directory>
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot "/Applications/mampstack-5.4.20-0/apache2/htdocs/mext-pst-dashboard/web"
ServerName mext-pst.local
ServerAlias mext-pst.local
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN mext-pst.local
</VirtualHost>
This configuration is working on my server, when ever I try to access using 80 it rewrites the URL to my 8080 port and I see the content of that folder, not the the default index page.
You've got to change the port to *:80 and also if you're going to use a different name then the servername make sure to take up NameVirtualHost *:80 in your httpd.conf.
Since you are not listening on port 80, getting an error when you go to the URL without a port seems to be the correct result, right?
And when you go to port 8080 you are getting a Proxy Error. Are you sure there is no other software running on port 8080 or your browser doesn't have a proxy entered? Apache would not be giving a Proxy Error. I suspect this error is coming from somewhere else.
About your configuration, I am not exactly sure what it is that you are trying to achieve but, if you are trying to get some documents served when you go to localhost:8080 and another set of documents served when you go to mext-pst.local:8080 than you are almost there, NameVirtualHost *:8080 is correct and needs to be there, remove redirect lines as you don't need them (unless my assumption is wrong).