apache multiple sites | show configured sites
ubuntu@ubuntu:/var/www$ sudo cp -R example.com example1.com
ubuntu@ubuntu:/var/www$ sudo chown -R ubuntu:www-data example1.com/
ubuntu@ubuntu:/etc/apache2/sites-available$ sudo cp example.com.conf example1.com.conf
ubuntu@ubuntu:/etc/apache2/sites-available$ sudo a2ensite example1.com.confubuntu@ubuntu:/etc/apache2/sites-available$ sudo service apache2 restart
example.com (enabled by site administrator)
example1.com (enabled by site administrator)
ubuntu@ubuntu:/etc/apache2/sites-available$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 ubuntu
192.168.40.128 example.com
192.168.40.128 example1.com
How it works…
Multiple domain hosting works with the concept of NamedVirtualHost. We have configured virtual hosts with ServerName and ServerAlias. When a client sends a request with a domain name, it sends a host name in the request headers. This host name is used by Apache to determine the actual virtual host to serve this request. If none of the available virtual hosts match the requested host header, then the default virtual host or the first virtual host will be used to serve the request.
In this example, we have used hosts file to map test domain names with local IP. With the actual domain name, you need to point DNS servers to the IP address of your web server. Generally, all popular hosting providers host their own DNS servers. You need to add these DNS servers to your domain setting with domain registrar. Then, on your hosting side, you need to set respective A recordsand CNAME records. An A record points to an IP address and the CNAME record is an alias for the A record used for pointing a subdomain to an A record. Your hosting provider should give you details on how to configure domains and subdomains.
In previous versions of Apache server, you might need to enable
NameVirtualHost
under the configuration file. Find a line similar to #NameVirtualHost 172.20.30.40
and uncomment it by removing the #
symbol at the start.
You can also set up IP-based virtual hosts. If you have multiple IP addresses available on your server, you can set the virtual host to listen on a particular IP address. Use the following steps to set up an IP-based virtual host:
- Get a list of the available IP addresses:
$ ifconfig | grep "inet addr" ubuntu@ubuntu:~$ ifconfig | grep "inet addr" inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 inet addr:192.168.56.102 Bcast:192.168.56.255 Mask:255.255.255.0 inet addr:127.0.0.1 Mask:255.0.0.0
- Edit the virtual host configuration and set it to match the following:
Listen 80 <VirtualHost 192.168.56.102> DocumentRoot /var/www/example1.com/public_html ServerName example1.com </VirtualHost>
- Apache documentation at https://httpd.apache.org/docs/2.2/vhosts/examples.html
- Refer to the Installing and configuring the Apache web server recipe for the installation and configuration of the Apache web server.
Comments
Post a Comment