Apache virtual host - apache2

Hi does this virtual host configuration is ok?. I,m trying to test it in my local machine and it don,t show the page. I disable the firewall to have all the ports open, do i have to do anything more to open the ports? or the virtual host configuration is wrong.
<VirtualHost linux1:8890>
#General setup for the virtual host
DocumentRoot "/srv/www/htdocs/webpage1"
ServerName linux1:8890
</virtualHost>

Did you add Listen 8890 anywhere? Apache needs this command in order to listen to that port.

You should have Listen 8890 somewhere, also above the declaration of the Virtualhosts you need NameVirtualHost *:8890
The virtualhost you define is ok, but you can only access that virtualhost by the hostname.

Related

What is the correct way to set up a virtual host running on Apache?

I configured apache on ubuntu 16.04 and I created a virtual host with these instructions:
<Directory /var/www/html/example.com/public_html>
Require all granted
</Directory>
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/example.com/public_html
ErrorLog /var/www/html/example.com/logs/error.log
CustomLog /var/www/html/example.com/logs/access.log combined
</VirtualHost>
when I try to open the page www.example.com on my browser I see this message:
This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.
Should I use another name instead of "example" to solve the problem?
Ok any name? For example "myname.com"
For clarity, I only need this for local tests
www.example.com is actual website that somebody else owns, has it registered in DNS and has some content on it. When you open the address in browser, your computer first asks DNS server (presumably one setup by your ISP). DNS then responds, that www.example.com is ran on 93.184.216.34 - which is probably not your machine. This is where the message is from.
Now, what is the meaning of ServerName and ServerAlias then? It is actualy a directive for your webserver, running on your machine, to know which packets are meant for given virtual host. It does not affect how your browser will transalate them.
For the sake of local testing (both server and browser are on same OS), all you need to do is to create virtualhost for a 'localhost'. This is a reserved address for the machine currently being used. Simply put:
ServerName localhost
ServerAlias localhost
into your virtualhost and you will be able to access the webserver by entering 'localhost' into your browser.
In case you would need more than one host locally, you can override the dns by editing the host file https://en.wikipedia.org/wiki/Hosts_(file) - for example, in windows by adding hosts records like:
127.0.0.1 app.local
127.0.0.1 db.local
and then creating two different virtual hosts, one with alias app.local, second with db.local. Then, the OS will then route requests from your browser correctly to your machine per hosts file. Your websever will then sort your request according to the header to proper virtualhost.
You might want to check out some general information on how dns and internet protocol works:
https://en.wikipedia.org/wiki/Internet_protocol_suite
https://en.wikipedia.org/wiki/Internet_Protocol
https://en.wikipedia.org/wiki/Domain_Name_System

How to access Apache VirtualHost via both Name & IP

I am using Apache VirtualHosts with 2 local domains on my home computer for development & testing.
• d4damage.local (personal websites)
• company.local (work stuff on external drive)
It's working perfectly on my network. However, I also need to optionally access d4damage.local via IP 192.168.1.60 (my computer is fixed to this IP)
The reason is my PS3 cannot resolve d4damage.local, but it can via IP. (all my other wi-fi devices are fine using d4damage.local)
It seems I can set d4damage.local to work on IP or name, but not both. (403 forbidden) the commented lines show the alternatives.
httpd-vhosts.conf
#NameVirtualHost 192.168.1.60:80
#<VirtualHost 192.168.1.60:80>
<VirtualHost *:80>
ServerAdmin admin#d4damage.local
DocumentRoot "/Users/paul/Sites"
ServerName d4damage.local
ErrorLog "/private/var/log/apache2/d4damage.local-error_log"
CustomLog "/private/var/log/apache2/d4damage.local-access_log" common
</VirtualHost>
httpd.conf
ServerName d4damage.local
...
DocumentRoot "/Users/paul/Sites"
<Directory "/Users/paul/Sites">
hosts
127.0.0.1 localhost
192.168.1.60 company.local d4damage.local
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
The above files concern the main domain i wish to use on my computer, the company.local one is additional & i don't need to access that via IP.
Hope you can help! Thanks.
you will want to edit your hosts file, commonly found ´/etc/hosts´.
sudo nano /etc/hosts
and new line like this:
192.168.1.60 d4damage.local
Save the file and reboot.
ah ha! I have found the issue.
I had (carelessly) left the example vhosts dummy-host.example.com in the vhosts file & simply commenting that block out has fixed the problem. I assume that because the dummy name/directory is not defined in httpd.conf & therefore causes a misconfiguration.
So the answer is to make sure you remove or comment-out the dummy examples in httpd-vhosts.conf

Can't access my website through ip. Is something wrong with my httpd settings?

I just set up a fresh apache/mysql install on CentOS on remote server.
Right now I just want to be able to access my server through ip on my server
let's say the ip is 64.123.myip.whatever
Here is my /etc/httpd/conf/httpd.conf setting
### Section 2: 'Main' server configuration
ServerName 64.123.myip.whatever:80
### Section 3: Virtual Hosts
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot /var/www/html
ServerName 64.123.myip.whatever
ErrorLog logs/error_log
CustomLog logs/access_log common
</VirtualHost>
I restart my apache, try to access this server and I get this error in the browser
Oops! Google Chrome could not connect to 64.123.myip.whatever
Why doesn't this work?
A couple of things. First... do you literally mean IP address? If so then putting that into Chrome is going to actually go right to that address. The odds of that random IP address actually hosting a webserver is fairly remote, hence the error. ServerName cannot be an IP address. If you instead mean you're trying to use a ServerName of "64.123.myip.whatever", then you must add a host entry that directs all traffic for "64.123.myip.whatever" to the IP address of your web server (presumably 127.0.0.1).

Hosting multiple sites with Apache2

I have on a server machine a local domain called "localwiki.com" that points to /var/www/wiki and works fine.
The sites-available and sites-enable configuration:
<VirtualHost *:80>
DocumentRoot /var/www/wiki/
ServerName localwiki.com
ServerAlias *.localwiki.com
</VirtualHost>
I wanted to create a second local domain for /var/www/doxygen (which I can access using "serverIP"/doxygen).
Here is the localdoxygen.com config file created at /etc/apache2/sites-available/localdoxygen.com
<VirtualHost *:80>
DocumentRoot /var/www/doxygen/
ServerName localdoxygen.com
ServerAlias *localdoxygen.com
</VirtualHost>
I used "a2ensite" in order to create a symbolic link in sites-enable pointing to localdoxygen.com in sites-available:
sudo a2ensite localdoxygen.com
I also have reloaded the new configuration:
sudo /etc/init.d/apache2 reload
And restarted apache:
sudo service apache2 restart
But the domain (localdoxygen.com), still doesn't work.
Any suggestions on how to fix this?
To solve the problem I had to add an entry to /etc/bind in order to activate the DNS for the domain.
I think those conf files must end in .conf to be loaded: i.e. localdoxygen.com.conf
disable the default site - a2dissite default (!!!)
DNS correct
check in /etc/apache2/sites-enabled have your sites
reload apache2 /etc/init.d/apache2 reload
You need to define a NameVirtualHost in your apache2.conf to support multiple domains under one IP.
NameVirtualHost 123.456.789.123:80
or
NameVirtualHost *:80
see also Running several name-based web sites on a single IP address.

Multiple Domains on a VPS with Apache

I am planning on getting a VPS soon and have two sites I want to host. I have my local vhosts setup for my development environment using host file to port it correctly.
My question is hopefully simple: When setting up with two separate domains that point to the one VPS server is Apache smart enough, like on the local environment, to automatically filter any requests for domain.com to the correct VHOST like it does locally? I would just like to be sure :)
Thanks!
Example of what I am asking:
Say Domain1.com and Domain2.com are both on my VPS.
When someone requests www.Domain1.com apache sees this and passes it through to the domain1.com vhost file.
When someone requests www.Domain2.com apache sees this and passes it through to the domain2.com vhost file.
Repasting my answer from Hosting two domains using only one VPS? as here it is even more relevant.
As complete beginner, I have been trying to host multiple domains on one Apache VPS. Tutorials had too much information that lead me to confusion.
Below I describe, for complete beginners, how to host multiple domains on one VPS server with Ubuntu and Apache.
IMPORTANT! You need to use root account to execute most operations.
IMPORTANT! If you have been trying to make some changes to apache configuration before, undo them.
Creating VirtualHosts
Create folders for your domains on server.
For example:
/home/apache/domain1
/home/apache/domain2
Put index.html file in each folder with any text.
This is domain1
This is domain2
Go to /etc/apache2/sites-available folder.
Create file domain1.conf
sudo nano domain1.conf
<VirtualHost *:80>
DocumentRoot /home/apache/domain1
ServerName domain1.com
ServerAlias www.domain1.com
</VirtualHost>
Create file domain2.conf
sudo nano domain2.conf
<VirtualHost *:80>
DocumentRoot /home/apache/domain2
ServerName domain2.com
ServerAlias www.domain2.com
</VirtualHost>
You can create subdomains same way.
sudo nano blog.conf
<VirtualHost *:80>
DocumentRoot /home/apache/blog
ServerName blog.domain.com
ServerAlias www.blog.domain.com
</VirtualHost>
Enable created sites
sudo a2ensite domain1.conf
sudo a2ensite domain2.conf
Restart apache
sudo service apache2 reload
Redirecting domain to server
Created VirtualHosts will work only if you redirect your domain name to server IP. Domains are just names that can be translated to IP numbers.
Local computer
To test your configuration on local machine, you need to edit hosts file.
sudo nano /etc/hosts
It should look like this.
127.0.0.1 localhost domain1.com domain2.com
Hosts file tells your computer that domain needs to be redirected to local machine.
IMPORTANT! If you create entry in hosts file for existing domain, for example
127.0.0.1 stackoverflow.com
you will loose access to this website.
Server
In order to redirect domain to you web server, you need to create or modify "A"-type DNS record for given domain to IP address of your server. You can do it by panel control provided by your domain registrar.
If you do not know IP address of your server, log in to that server and type in command line:
ifconfig
The simple answer is 'yes', Apache is that clever. If you are used to using a local vhost file in conjunction with your hosts file to simulate local domains, the exact same technique can be applied on a VPS. The part you are doing with the hosts file is essentially creating a local name server, other than that Apache doesn't know the difference. Simply set the ServerName directive for each named vhost and you should find it working the same as it does locally.

Resources