VirtualHost setup always doesn't work - apache

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

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.

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.

Virtual hosts not working on MAMP

I am running into virtual hosts setup issues on MAMP. Here is my vhosts config
NameVirtualHost *
<VirtualHost *>
ServerName dev.local.com
DocumentRoot “/Applications/MAMP/htdocs/local”
</VirtualHost>
<VirtualHost *>
DocumentRoot /Applications/MAMP/htdocs
ServerName localhost
</VirtualHost>
Here is my hosts file
127.0.0.1 localhost
127.0.0.1 dev.local.com
now when visiting localhost and dev.local.com both are pointing to index of local directory. I tried various config setting in vhosts file but none is working. I am on MacBook Pro 10.10
When I first test virtual host (example.dev) I added two slightly different index.php files its directry and to localhost directory (/htdocs). My virtual host would redirect to locahost, I would know because the index.php would be the one in /htdocs. Similar to what user6166 said, I changed the code below to match the 8888 and it worked! I test example.dev:8888 and it works.
NameVirtualHost *:8888
<VirtualHost *:8888>
ServerName dev.local.com
DocumentRoot /Applications/MAMP/htdocs/local
ServerAlias dev.local.com
</VirtualHost>
<VirtualHost *:8888>
DocumentRoot /Applications/MAMP/htdocs
ServerName localhost
</VirtualHost>
Then to remove 8888 change above back to 80 and MAMP preferences to:
Apache Port:80
Nginx Port:8888
mySQL Port:3306
Then inside httpd.conf change below to 80:
Listen 8888
ServerName localhost:8888
Hope that helps someone, it definely fixed my problem!
I didn't find any of the answers useful, so if other suggestions don't work for you:
Consider that the virtual hosts need to be correctly enabled/included by uncommenting
Include conf/extra/httpd-vhosts.conf in C:\MAMP\conf\apache\httpd.conf and NOT in C:\MAMP\bin\apache\conf or C:\MAMP\bin\apache\conf\original .
There're multiple locations that have MAMP configuration, make sure to uncomment that line in the correctly located file.
Changed the hosts file like this -
NameVirtualHost *:80
<VirtualHost *:80>
ServerName dev.local.com
DocumentRoot /Applications/MAMP/htdocs/local
ServerAlias dev.local.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /Applications/MAMP/htdocs
ServerName localhost
</VirtualHost>
Enable virtual host in the right httpd.conf
Open MAMP > File > Edit template > Apache > httpd.conf
edit or add:
# Virtual hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
edit mamp httpd.conf
I had a similar issue with MAMP Pro 5 (after updating from version 4). One of my custom host with aliases was returning a 404 NOT FOUND error.
After a LONG troubleshoot, make sure to add the domain in the alias list.
MAMP 4
host:
test.co
alias:
sub.test.co
Becomes in MAMP 5:
host:
test.co
alias:
sub.test.co
test.co
Hope they'll get that fixed and that it helps someone!
Try to configure MAMP ports by this way:
MAMP
Preferences
Ports
Apache port: 80
Nginx port: 8888
I've had the same port values and after configurating it started working.
Open the main Apache configuration file, httpd.conf, which is located at
Applications/MAMP/conf/apache/httpd.conf
and Remove the hash (pound) sign from the beginning of the line that begins with Include:
Virtual Hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
I'm not an apache expert. But I had the same problem. I think my DNS doesn't resolve my URL.
When I ping exampl.dev everything looks fine, but when I enter example.dev in the browser I get an error. I always have to add http:// in front of the url.

Apache Virtualhost Redirect Not Working

I have two simple redirects set up in my virtualhost for a subdomain; one is working, one is not:
<VirtualHost *:80>
ServerName subdomain.site.com
Redirect / https://subdomain.site.com/subdirectory/login.php
</VirtualHost>
<VirtualHost x.x.x.x:443>
ServerName subdomain.site.com
Redirect / https://subdomain.site.com/subdirectory/login.php
SSLEngine on
SSLCertificateFile /etc/httpd/ssl/subdomain.site.com.crt
SSLCertificateKeyFile /etc/httpd/ssl/subdomain.site.com.key
ErrorLog logs/ssl_error_log
CustomLog logs/ssl_access_log common
</VirtualHost>
The first redirect is working. That is, if someone simply types in subdomain.site.com in their browser it redirects to https and to the correct subdirectory. The second redirect is not working. If someone types in https://subdomain.site.com it says "Firefox has detected that the server is redirecting the request for this address in a way that will never complete" and the browser URL becomes "subdomain.site.com/subdirectory/login.phpsubdirectory/login.phpsubdirectory/login.phpsubdirectory/login.php..." instead of redirecting to the correct https://subdomain.site.com/subdirectory/login.php page. Can anyone point me in the right direction?
Edit: I updated the above VirtualHosts file to the newer version and the problem has changed so I updated the problem description as well.
Alright, none of the answers above worked so I had to keep working on this. Ultimately I removed the redirect line from the :443 virtualhost section and added the following two lines to the same section to get this to work correctly:
RewriteEngine On
RewriteRule ^/$ https://subdomain.site.com/subdirectory/login.php [R=301,NC,L]
You have to add this line to top of file
NameVirtualHost x.x.x.x:443 or domaine name:443
check you apache version.
You have to add this line to top of file :
NameVirtualHost x.x.x.x
and
Listen 80
Listen 443
regards
if you are on ubuntu (I mean debian based linux distro)in your /etc/hosts you should define a line like below :
127.0.0.1 yourdomain
and then in make a new file for your new site configuration in :
/etc/apache2/sites-available/
and name it like your domain name .conf just to don't forget what is that conf file for.
then enable new conf with following command
a2ensite your_conf_name
then restart apache.
now your new site configuration is ready.
now look at following link :
http://httpd.apache.org/docs/2.2/bind.html
you have to mention that your apache should listen on multiple port
in your case 80 , 443
If you are using SSL you should change default-ssl.conf settings like
<VirtualHost *:443>
ServerName subdomain.site.com
ServerAlias subdomain.site.com
DocumentRoot /var/www/html/subdomain.site.com

add a server alias to the domain's VHOST?

I have a VPS. I hosted a domain ipointing to a sub directory of the www folder. The domain works fine till the home page. The moment I start going to other pages its shows my servers [orginalname]/[subdirectory name] . I think I need to add a server alias to the domain's VHOST. Can anyone tell me how to do that??
Are you using Apache?
Try with
<VirtualHost *:80>
DocumentRoot "/path/to/document/root"
ServerName name1
ServerAlias name2
...
I did it using the proxy Apache option, this is it:
My VirtualHost is http://dlx/ and I want to add an "alias" like http://dlx/drupal/
In the httpd.config file I added a proxy configuration:
<VirtualHost 127.0.0.1>
ServerName dlx
DocumentRoot "C:/deluxe/"
<LocationMatch /drupal/>
ProxyPass http://localhost/drupal/
ProxyPassReverse http://localhost/drupal/
</LocationMatch>
</VirtualHost>
Configure .htaccess on my dlx virtualhost (C:/deluxe/):
RewriteRule ^drupal/(.*)$ http://localhost/drupal/$1 [P,L]
That's it. It works for me, I hope it also works for you.