How to connect to name-based virtualhosts in vagrant guest? - apache

Inside the box, I have two virtual hosts:
<VirtualHost *:80>
HostName my.site1
...
</Virtualhost *:80>
<VirtualHost *:80>
HostName my.site2
...
</VirtualHost>
How can I connect to the vhosts inside the guest from the host? In my (host) /etc/hosts I have:
127.0.0.1 my.site1
127.0.0.1 my.site2
Because of the Vagrant's port mapping, the guest is accessible only as my.site1:port, e.g., my.site:3000. With that, the Apache inside the guest takes me just to the root (the Apache's welcome site). It is the same for both vhosts: my.site1:3000 and my.site2:3000.
apachectl -S logs:
VirtualHost configuration:
*:80 is a NameVirtualHost
default server stretch.localdomain (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost stretch.localdomain (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost my.site1 (/etc/apache2/sites-enabled/001-site1.conf:1)
port 80 namevhost my.site2 (/etc/apache2/sites-enabled/002-site2.conf:1)
Is that because of improper configuration of vhosts or am I missing some point in the name/port or host/guest configuration?
Am I right it should work this way?
my.site1:3000 -> contents of my.site1
my.site2:3000 -> contents of my.site2

Okay, so the problem here is the networking. I was able to reproduce this problem. First of all, please disable the port forwarding in Vagrant. Just comment config.vm.network "forwarded_port", guest: 80, host: 8070 and do a vagrant reload .To get this working, you need to check your host's IP address and then go to your Vagrant file and edit config.vm.network "private_network", ip: "X.X.X.X" so that the ip address here is actually on the same network as your host. What I did is just incremented the last octet by 1. e.g. My local IP address is 192.168.23.45 so I assigned 192.168.23.46 to the Vagrant guest.
Once this is done, perhaps, you can save yourself all the trouble by just using this shell script to create Virtual hosts for yourself. I have pasted the output below which you can go through to see that I have setup my two virtual hosts with mysite1 and mysite2 names.
Then just put the host file entries on your host like below:
192.168.23.46 mysite1
192.168.23.46 mysite2
And accessing the website using http://mysite1 and http://mysite2. You might want to change the content of the index.php placed by script under the respective document roots so that you can be sure that the requests are being handled by correct virtual hosts since this scripts just deals with the default index.php of apache which will be found under both your document roots.
The other option is to make the Vagrant box available on public network and then
access it using the public IP and for that, you will have to enable config.vm.network "public_network" in your Vagrant file and the rest of the process of creating the Virtual host is the same (Using this script).
[root#localhost vagrant]# bash test.sh
Enter the server name your want (without www) : mysite1
Enter a CNAME (e.g. :www or dev for dev.website.com) : mysite1
Enter the path of directory you wanna use (e.g. : /var/www/, dont forget the /): /var/www/mysite1/
Enter the user you wanna use (e.g. : apache) : apache
Enter the listened IP for the server (e.g. : *): *
Web directory created with success !
/etc/httpd/conf.d/mysite1.conf
Virtual host created !
Would you like me to create ssl virtual host [y/n]?
n
Testing configuration
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
Would you like me to restart the server [y/n]?
y
Redirecting to /bin/systemctl restart httpd.service
======================================
All works done! You should be able to see your website at http://mysite1
Share the love! <3
======================================
Wanna contribute to improve this script? Found a bug? https://gist.github.com/mattmezza/2e326ba2f1352a4b42b8
[root#localhost vagrant]# bash test.sh
Enter the server name your want (without www) : mysite2
Enter a CNAME (e.g. :www or dev for dev.website.com) : mysite2
Enter the path of directory you wanna use (e.g. : /var/www/, dont forget the /): /var/www/mysite2/
Enter the user you wanna use (e.g. : apache) : apache
Enter the listened IP for the server (e.g. : *): *
Web directory created with success !
/etc/httpd/conf.d/mysite2.conf
Virtual host created !
Would you like me to create ssl virtual host [y/n]?
n
Testing configuration
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
Would you like me to restart the server [y/n]?
y
Redirecting to /bin/systemctl restart httpd.service
======================================
All works done! You should be able to see your website at http://mysite2
Share the love! <3
======================================
Wanna contribute to improve this script? Found a bug? https://gist.github.com/mattmezza/2e326ba2f1352a4b42b8
Please let me know in case you need more clarification.

Related

Where to edit (which part of the document ) a LAMP servers host file? [and correct synax]

While trying to edit a host file on a ubuntu server, the docs recommend updating the system's host file saying
The hosts file creates static associations between IP addresses and
hostnames or domains which the system prioritizes before DNS for name
resolution. Open this file in a text editor and add a line for your
Linode’s public IP address. You can associate this address with your
Linode’s Fully Qualified Domain Name (FQDN) if you have one, and with
the local hostname you set in the steps above. In the example below,
203.0.113.10 is the public IP address, example-hostname is the local hostname, and example-hostname.example.com is the FQDN.
After opening a SSH connection and running
nano /etc/apache2/apache2.conf
The host file can now be edited. After scrolling to the bottom of the page to add the new lines I looked back at the example in the documentation:
127.0.0.1 localhost.localdomain localhost
203.0.113.10 example-hostname.example.com example-hostname
Is this the correct format?
127.0.0.1 localhost.localdomain localhost
104.123.155.24 newhostuser.104.123.155.24 newhostuser
You can add HostName entry in /etc/hosts file. If you want to run Apache on IP address then you can edit Listen entry in apache2.conf file.

Apache not serving pages

I have just done a clean install of apache on RedHat. In the httpd.conf file, I have changed the ServerName to the IP address of the computer. From the command line, when I use xdg-open I cannot view the test page from the apache server (when using the public IP in the address bar), however I can see it when I open 127.0.0.1.
I have a rule in my firewall to allow traffic on port 80.
Is there a way to debug this?
for your firewall, you can check the current rule with
/etc/init.d/iptables status command.
and you can disable/cleanup your iptables with
/etc/init.d/iptables stop
to check the route to your public ip ,do tracepath YOUR-PUBLIC-IP

I get an Apache Test page in a secondary domain after cloning a server with Plesk

I had a properly set Centos 6.6 / Apache server with some virtual hosts. I used Plesk to configure it mostly because I am a newby. The main domain was xxdomain.com and we had 2 test websites in test1.xxdomain.com and test2.xxdomain.com everything worked perfectly..
After the migration to the new server, the main website works fine, but now test1.xxdomain.com and the other will not work. I get an Apache Test Page saying "You may now add content to the directory /var/www/html/. Note that until you do so, people visiting your website will see this page and not your content. To prevent this page from ever being used, follow the instructions in the file /etc/httpd/conf.d/welcome.conf.", but the Document Root is setup some where else, plus I also tried copying the files there just in case.
I tried all the solutions here: http://kb.odin.com/en/135 and more, but I am unable to get any result...
-Even when pointing directly to a file, I get a 404 error..
-reconfiguring all domains via ssh worked, but when I try the process with the actual test1.xxdomain.com I get "syntax error near unexpected token 'newline'" error.
-I left just an index.html file with no .htaccess in the Documentroot folder
Check that
your domain test2.xxdomain.com is resolving to IP address of Plesk server
main apache config (httpd.conf) includes conf files generated by plesk. You can do it with command apachectl -S
configs of yuor site presents in apachectl -S output
Here the piece of my configs where you can see plesk's webmails entries and my domain "cos66.local":
# apachectl -S
VirtualHost configuration:
192.168.0.103:7080 is a NameVirtualHost
default server default (/etc/httpd/conf/plesk.conf.d/server.conf:70)
port 7080 namevhost default (/etc/httpd/conf/plesk.conf.d/server.conf:70)
port 7080 namevhost lists (/etc/httpd/conf/plesk.conf.d/server.conf:142)
wild alias lists.*
port 7080 namevhost horde.webmail (/etc/httpd/conf/plesk.conf.d/horde.conf:9)
wild alias horde.webmail.*
port 7080 namevhost roundcube.webmail (/etc/httpd/conf/plesk.conf.d/roundcube.conf:9)
alias webmail.cos66.local
wild alias roundcube.webmail.*
port 7080 namevhost cos66.local (/etc/httpd/conf/plesk.conf.d/vhosts/cos66.local.conf:130)
alias www.cos66.local
alias ipv4.cos66.local
this virtual hosts uses port 7080 because I have enabled nginx integration.

How to restrict apache virtual hosts?

I have one site enabled, for example foobar.com.
But when I open IP address 127.0.0.1 - it's opens my foobar.com.
Whatever maps to 127.0.0.1 in /etc/hosts I open - foobar.com is opened.
How can I configure virtualhost/apache to only allow access from foobar.com?
Apache 2.4, Ubuntu 13.10
Apache always uses the first VirtualHost as the "default" host. That is, whenever it can't map the requested hostname (127.0.0.1 in your case) to a specific VirtualHost, it will use that default one.
Just define another VirtualHost before the foobar.com one. If you have both in the same configuration, configure the default one first. If you have separate config files, name the "default" one so, that it gets loaded before the foobar.com one. Many Linux distributions use a name like 00-default.conf for the default VHost.

https(apache + ssl) is only available from locahost, how to configure to visit it by domain name?

apache + ssl is configured using xampp on windows server 2003. http content has no problem by domain name, but https content can only be visited from localhost. "netstat -a" shows
Proto Local Address Remote Address State
...
TCP hostname:https hostname:0 Listening
...
How to config to enable https via domain name?
Found the reason. Another program take the 443 port so apache https failed. use "netstat -a -o -n" can get the detail.
I'm assuming you can already access apache using this domain name.
Take a look in your ports.conf, usually found at
/etc/apache2/ports.conf
It should contain a line like:
NameVirtualHost *:443
and also
Listen 8443 https