قالب وردپرس درنا توس
Home / Tips and Tricks / Host multiple websites with one Apache server – CloudSavvy IT

Host multiple websites with one Apache server – CloudSavvy IT



Apache Virtual Hosts are a feature that allows you to host multiple independent websites from a single Apache installation. Each site has its own file system directory and domain name. You can offer different sites to different visitors based on the domain they are using.

Virtual hosts are created using the directive in Apache configuration files. They are normally stored in it sites-available directory within the Apache installation location. On Debian systems this is usually /etc/apache2Can use other distributions /etc/httpd

We assume that Apache is already running. We will create two separate virtual hosts, a.example.com and b.example.comThere is no limit to the number of virtual hosts you can use ̵

1; if you need a dozen sites on a single server, Apache will require it.

Configure the sites

Each site needs its own file system folder. You place the website files, such as HTML, CSS and JavaScript, in this folder. It will be the DocumentRoot by Apache, as this is the root from which documents are served.

You can locate your sites anywhere on your system. You will often see virtual hosts stored in it /var/www, where each site gets its own directory:

sudo mkdir -p /var/www/a.example.com
sudo mkdir -p /var/www/b.example.com

/var/www is normally owned by root, so we use sudo to create the subfolders. Then change the ownership of the document root to your own user and set the appropriate permissions. This allows Apache to read files while giving you write access so that you can add your content.

sudo chown -R $USER:$USER /var/www/a.example.com
sudo chown -R $USER:$USER /var/www/b.example.com
sudo chmod -R 755 /var/www

Copy your website files to the folders. We use two simple ones index.html files:

a.example.com/index.html


    
        

a.example.com

b.example.com/index.html


    
        

b.example.com

Configure Apache

Now you are ready to configure your virtual hosts. Create a new Virtual Host file for each of the sites. It doesn’t matter what you name the file; by convention, it usually matches the host name of your site.

Each virtual host must declare at least two properties:

  • ServerName – The host name (domain) from which the site is served.
  • DocumentRoot – The location of the file system to serve for this virtual host.

Here’s an example configuration for our two sites:

/etc/apache2/sites-available/a.example.com.conf


    ServerName a.example.com
    DocumentRoot /var/www/a.example.com

/etc/apache2/sites-available/b.example.com.conf


    ServerName b.example.com
    DocumentRoot /var/www/b.example.com

This basic setup is enough to get the two sites live! The *:80 means that Apache will consider using this virtual host for any request arriving on port 80. They will later pass through the ServerName explanations.

Enable virtual hosts

Add a virtual host to sites-available makes it available but not active. You must manually enable each site you want to use. Virtual host configurations to be enabled must have a symlink to /etc/apache2/sites-enabled (or /etc/httpd/sites-enabled

On Debian systems you can use the a2ensite command to simplify this step:

a2ensite a.example.com
a2ensite b.example.com

On other distributions, use the ln command to manually create a symlink.

sudo ln -s /etc/httpd/sites-available/a.example.com.conf /etc/httpd/sites-enabled/a.example.com.conf
sudo ln -s /etc/httpd/sites-available/a.example.com.conf /etc/httpd/sites-enabled/b.example.com.conf

Finally, restart Apache to apply your new configuration. Virtual host changes require a server restart after adding them to sites-enabled

sudo apache2ctl restart

You should now be able to see Apache serving your two different sites. If you don’t have DNS resolving to your server, you can /etc/hosts file to test your setup.

Add the following lines to /etc/hosts

127.0.0.1 a.example.com
127.0.0.1 b.example.com

This will force both domains to revert to your own system. You can now visit them in your browser to see the two different Virtual Host pages.

Site Aliases

If you the the same site on multiple domains, use the ServerAlias directive in your virtual host. Each alias is considered when Apache compares your virtual host against incoming requests. The virtual host is used as the request Host header matches a ServerAlias or the ServerName


    ServerName a.example.com
    ServerAlias example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/a.example.com

This example would be the a site on three domains, without having to repeat configuration information. YOU must use ServerAlias to define multiple domains – repeat ServerName is possible, but it takes precedence over previous use.

Other configuration options

Many of Apache’s server configuration options can be used with virtual hosts. They override global server settings when Apache handles a request using the virtual host.


    ServerName a.example.com
    DocumentRoot /var/www/a.example.com
    DirectoryIndex my-index.html

    
        AllowOverride All
        Options -Indexes
        Require all granted
    

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

This virtual host applies many more settings. my-index.html is served by default, instead of index.html, and custom log locations are used. Additionally, additional settings are applied to the document root itself, using the Directory block. .htaccess transfers are allowed (AllowOverride All) and Apache’s default directory listing pages are disabled (Options -Indexes

IP address hosts

Apache also supports IP-based hosts in addition to the name-based hosts we’ve seen so far. IP-based hosts are ideal when your server has multiple network interfaces, such as an internal corporate network and the public Internet. You can operate a different site depending on the network interface used.


    ServerName a.example.com
    DocumentRoot /var/www/a



    ServerName b.example.com
    DocumentRoot /var/www/b



    ServerName a.example.com
    DocumentRoot /var/www/a2

In this example, requests are on a.example.com through the interface with IP 192.168.0.1 would receive content other than that sent via 172.17.0.1In addition, users of the last IP address could gain access b.example.comThis site is not visible to users connecting via 192.168.0.1

This approach allows you to use one server for your corporate intranet and public website. Bind the internal sites to the LAN IP of your server; run the public hosts against the WAN address.

Virtual host matching

Apache's Virtual Host solution routine is well-documented and quite simple.

Generally, Apache tries the Host request header against a ServerName or ServerAlias field in a virtual host configuration. If there are multiple matches, Apache uses the first matching virtual host it found.

Virtual hosts are sorted by file name. like you need one site that previously matches another, rename the configuration file so that it is sorted before the target site. You can add a numeric prefix, such as 000-a.example.com.confUsually this is not necessary - if all of your sites have unique domains, you should never run into a conflict.

Conclusion

Apache Virtual Hosts allow you to split the configuration of multiple sites into standalone definition files. You can enable and disable each site independently by linking it to sites-enabled

Many configuration options are available for virtual hosts. You can override some Apache server settings per host, giving you full control over any virtualized site setting.


Source link