Apache: Virtual Host configuration - apache

As i tried to configure my virtual host in apache. I put something like this,
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /xampp/htdocs/gift
ServerName gift.loc
</VirtualHost>
And in my hosts file i put something like this,
127.0.0.1 localhost
127.0.0.1 gift.loc
And i run it on the browser,
http://gift.loc - is fine
But when i tried using this,
http://localhost/othersite - can't found
Do i missed somehting to configure? ANy ideas...
Thanks in advance,

You need a VirtualHost entry for every host you want apache to handle. The first one in the config file will be used as the default if no other VirtualHosts match the request.
For example if we have:
<VirtualHost *:80>
DocumentRoot /xampp/htdocs/gift
ServerName gift.loc
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /example/htdocs/gift
ServerName example.com
</VirtualHost>
A request for foobar.org will get handled by the gift.loc virtual host.

you need to put localhost in the vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /xampp/htdocs/
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /xampp/htdocs/gift
ServerName gift.loc
</VirtualHost>
This works fine (Make sure you restart apache). If you need to check your configuration you can (on linux at least) run httpd -S.

There are few steps you need to follow to setup the virtual host on ubuntu:
Let say that your project folder name is myProject
Step 1:Place your folder inside /var/www/html
sudo mv ~/myProject /var/www/html/
Step 2: Give ownership of project folder to www-data
sudo chown -R www-data:www-data /var/www/html/myProject
Step 3:Create new site inside Sites available:
cd /etc/apache2/sites-available/
ls
Here you will see existing 000-default.conf and default-ssl.conf .Copy the content of both file into one file and replace your folder name or copy this one into new file named myProject.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/myProject/
ServerName project.com
ServerAlias www.project.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/myProject/
ServerName project.com
ServerAlias www.project.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/ssl/certs/mobidev_cert.pem
SSLCertificateKeyFile /etc/ssl/certs/mobidev_key.pem
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
Include the path of self signed certificate also in this as shown ssl key and ssl certificate that can be downloaded easily.
Step 4:Add your project into apache configuration file.
sudo vi /etc/apache2/apache2.conf
put this lines in the file:
DocumentRoot "/var/www/html/myProject"
<Directory /var/www/html/myProject/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Step 5:Add your virtual server name (specified in myProject.conf) into host file.add that line:
sudo gedit /etc/hosts
127.0.1.1 project.com
Step 6:Now all set ,enable site,restart apache
sudo a2ensite /etc/apache2/sites-availabl/myProject.conf
sudo systemctl reload apache2
sudo update-rc.d apache2 defaults
sudo update-rc.d mysql defaults
sudo a2enmod ssl
sudo a2ensite default-ssl
Just hit project.com in your browser.

From the docs, it looks like we need to create a block for each different host that you would like to serve.
Further in the same doc, If you are adding virtual hosts to an existing web server, you must also create a block for the existing host.

Related

Apache2 ServerName and ServerAlias not working

On a nearly fresh Ubuntu 20.04 LTS computer, I would like to set up a virtual host on my local machine. So I created a index.html under /var/www/test/ with the following content:
you have entered a test page
I have set up a test.conf file under /etc/apache2/sites-available/
with the following content:
<VirtualHost *:80>
ServerAdmin webmaster#zhihu.com
DocumentRoot /var/www/test/
ServerName zhihu.com
ServerAlias www.zhihu.com
<Directory /var/www/test/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The localhost is working:
/etc/hosts config is probably working as indicated by ping
ping zhihu.com
PING zhihu.com (127.0.1.1) 56(84) bytes of data.
64 bytes from xxx (127.0.1.1): icmp_seq=1 ttl=64 time=0.045 ms
but browser cannot bring me to the domain which should now be hosted in /var/www/test/.
I have also a2ensite test.conf and a2dissite 000-default.conf and service apache2 reload
So I think the only possible place for error to occur is ServerName and ServerAlias. Why are they not working?
Could you please enable VirtualHost using a2ensite and access site in incognito mode.
Ensure that the ssl certificate for the site includes both example.com and www.example.com
and ServerAlias is set to www.example.com

Set DocumentRoot on local directory in apache

I got a local directory, which will need a DocumentRoot.
When I go to serverip/nivon-zuidholland I must have a DocumentRoot of public.
How would I manage this in apache2 config?
I inserted this in my apache2.conf. I tried to put this into my sites-available/nivon-zuidholland.conf but this doesn't work as well.
I can't restart apache2 because it returns DocumentRoot not allowed here.
<Directory "/var/www/html/nivon-zuidholland">
AllowOverride All
DocumentRoot /var/www/html/nivon-zuidholland/public
</Directory>
In your /etc/hosts add this:
127.0.0.1 nivon-zuidholland.local
Create a directory: /var/www/nivon-zuidholland (set group to www-data)
Create a Virtual host file: /etc/apache2/sites-available/nivon-zuidholland.conf
<VirtualHost *:80>
ServerName nivon-zuidholland.local
ServerAdmin webmaster#nivon-zuidholland.local
DocumentRoot /var/www/nivon-zuidholland
ErrorLog ${APACHE_LOG_DIR}/nivon-zuidholland_error.log
CustomLog ${APACHE_LOG_DIR}/nivon-zuidholland.log combined
</VirtualHost>
Place a symbolic link to this vhost conf file in sites-enabled directory
ln -s /etc/apache2/sites-available/nivon-zuidholland.conf /etc/apache2/sites-enabled/nivon-zuidholland.conf
Restart Apache server: sudo service apache2 restart
You should be able to access the site by going to: http://nivon-zuidholland.local/

Add Virtual Host for Localhost Ampps

<VirtualHost *:80>
DocumentRoot "C:/Program Files (x86)/Ampps/www"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/Program Files (x86)/Ampps/www/proj"
ServerName proj.local
</VirtualHost>
How do I bind a specific "domain" (virtual host domain) to one location? Above points proj.local to localhost as well.
As you said you are using AMPPS, I suppose you are adding this instructions to the folder:
AMPPS/apache/conf/extra/httpd-vhosts.conf
With that in mind I'll show an example of configuration.
<VirtualHost project.local:80>
<Directory "/Users/you/yourproject">
Options FollowSymLinks Indexes
AllowOverride All
Order deny,allow
allow from All
</Directory>
ServerName project.local
ServerAlias project.local 127.0.0.1
ScriptAlias /cgi-bin/ "/Users/you/yourproject/cgi-bin/"
DocumentRoot "/Users/you/yourproject"
ErrorLog "/Applications/AMPPS/apache/logs/project.error_log"
CustomLog "/Applications/AMPPS/apache/logs/project.access.log" combined
</VirtualHost>
All right. After doing that, you will have to add to your hosts file. In Mac they are located in:
/etc/hosts
If you are using a Windows environment you will find the hosts file in:
c:\windows\system32\drivers\etc\hosts
Attention: You may have to type your password again or allow the program you're using to edit the file to use administrator privileges.
Then you'll add your local IP and the hostname to it. Just like this:
127.0.0.1 project.local
If you don't add the URL to the hosts file that won't work.
Have you tried with <VirtualHOst proj.local:80>? You should be able to find more examples here: http://httpd.apache.org/docs/2.2/vhosts/examples.html
<VirtualHost 127.0.0.1:80>
DocumentRoot "C:\xampp\htdocs"
ServerName xampp.local
ServerAlias www.xampp.local
</VirtualHost>
after adding it at the end of your httpd-vhosts.conf file restart your xampp and also add following lines to your hosts file available at C:\Windows\System32\Drivers\etc\hosts
127.0.0.1 xampp.local

Setup vhost ubuntu

I am trying to setup a vhost in ubuntu, my file is like
/var/www/vhost/domain1.com/app
/var/www/vhost/domain1.com/public
/var/www/vhost/domain2.com/app
/var/www/vhost/domain2.com/public
the vhost in /etc/apache2/site-avalable/domain1.com.conf is :
<virtualhost *:80>
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
ServerAdmin webmaster#domain1.com
ServerName domain1.com
ServerAlias *.domain1.com
DirectoryIndex index.php
DocumentRoot /var/www/vhosts/domain1.com/public
LogLevel warn
ErrorLog /var/www/vhosts/domain1.com/log/error.log
CustomLog /var/www/vhosts/domain1.com/log/access.log combined
</virtualhost>
problem
the vhost folder and all the files after that (the children) are accessible in the web browser. I know I have to put some sort of htaccess but i don't know how and where I have to put it.
If you use apache 2.4 and you develop on a local machine:
Create a new configuration file for your website:
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain1.com.conf
Edit the file like so:
<VirtualHost *:80>
ServerName domain1.com
ServerAlias *.domain1.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/vhost/domain1.com/public
ErrorLog /var/www/vhosts/domain1.com/log/error.log
CustomLog /var/www/vhosts/domain1.com/log/access.log combined
</VirtualHost>
Enable the website
$ sudo a2ensite domain1.com
Restart apache
$ sudo service apache2 restart
Add the following line to your /etc/hosts file
127.0.1.1 domain1.com
Now it should work, go to http://domain1.com and check it out.

Strange problem with Apache Virtual Hosts

I have recently installed Apache 1.3.41 on a Windows Vista machine. I have not changed the default settings in httpd.conf aprart from trying to setup virtual hosts, as follows:
Added some host names in the hosts file:
127.0.0.1 localhost
#::1 localhost
127.0.0.1 mysite
127.0.0.1 mydomain
I made the following folders in C:/Users/Moukasp/
C:/Users/Moukasp/Apache
C:/Users/Moukasp/django/mysite
Then added some simple html pages in each of those folder and the c:/Users/Moukasp/pictures folder and, finally, I added the following settings at the end of httpd.conf:
NameVirtualHost 127.0.0.1
<VirtualHost localhost>
# ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/Users/Moukasp/Apache"
ServerName localhost
Alias "/pics" "c:/users/moukasp/pictures"
Alias "/ap" "C:/Program Files/Apache Group/Apache/htdocs"
Alias "/dj" "C:/Users/Moukasp/django/mysite"
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost mysite>
# ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/Users/Moukasp/django/mysite"
ServerName mysite
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost mydomain>
# ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/Users/Moukasp/django/mysite"
ServerName mydomain
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
So, urls
http://localhost
http://localhost/pics
http://localhost/ap
http://localhost/dj
http://mydomain
work fine. But I there's no response from http://mysite which, as can be seen from the settings, serves from the same folder as http://mydomain. I have tried various names but no response. The hosts file is being read every time I start the Apache server. I even removed the mydomain server from the hosts file and the httpd.conf lest there's a limitation in the number of virtual hosts, but no luck.
Any help would be greatly appreciated, els I go mad! It is true, I have being trying this after complete failure to make Apache work with mod_python for django! I just hoped that a step at a time would lead to some success!
Try:
NameVirtualHost 127.0.0.1:80
In your httpd.conf. My config file includes a comment stating a port-specifier should be used.
The VirtualHost and NameVirtualHost directives should have IP addresses and ports:
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
# ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/Users/Moukasp/django/mysite"
ServerName mysite
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
Tried NameVirtualHost 127.0.0.1:80 but it doesn't make any difference! The only servers that work are localhost and mydomain.
I wonder whether I need to do something more than just declare the server name of each virtual host in the hosts file.
Try adding a port declaration to the hosts. For example:
<VirtualHost mydomain:80>
DocumentRoot "C:/Users/Moukasp/django/mysite"
ServerName mydomain
</VirtualHost>