How to configure multiple different sites and domains in Apache 2.4 server

user:visitors Date:2025-01-04 00:00:1522

In the Windows system environment, we want to run common websites will generally use Apache as a web server, if only one website is placed in the server, it is too waste of resources for the server with large storage space, so how to store and run multiple website projects in one server?

To configure a website with Apache, first we need to modify the configuration file in the installed apche.

Open the httpd.conf file in the conf folder.

Remove the # sign in front of Include conf/extra/httpd-vhosts.conf.

Then modify the httpd-vhosts.conf file in the conf/extra/ folder

Delete the default configuration and add the following code:   

<VirtualHost *:80>

    DocumentRoot “C:/wz1”

    ServerName 1.com

    ServerAlias www.1.com

# ErrorLog “logs/wz1-error.log”

# CustomLog "logs/wz2-access.log" generic

< directory "C:/wz1" >

All need to be granted

</Directory>

</VirtualHost>

<VirtualHost *:80>

    DocumentRoot “C:/wz2”

    ServerName 2.com

    ServerAlias www.2.com

# ErrorLog “logs/wz1-error.log”

# CustomLog "logs/wz2-access.log" generic

< directory "C:/wz2" >

All need to be granted

</Directory>

</VirtualHost>   

Among them, VirtualHost*:80 matches port 80, DocumentRoot sets the root directory of each site, ServerName sets the top-level domain name, and ServerAlias sets the website alias to add www in front of the domain name, so that you can bind two domain names with and without www for a site at the same time.

Set the root directory permissions and some rules for access in the Directory code snippet, here we set to allow all users to access the directory, if not set may the website cannot be opened or 403 error, of course we can also add some other settings above the code:

As:

The option followSymLinks #Options设定

The AllowOverride All #允许.htaccess rule overrides the Options setting

In the apache server, you can configure multiple sites by copying the VirtualHost code snippet into multiple copies as shown in the example wz1 and wz2 sites, and then modifying the bound site configuration.

The above is just the site configuration with 80 port http, if we need to set up multiple https configurations, how should we configure it?

In Apache, there are several ways to configure the sample file, we just need to add the httpd.conf code in the apache configuration file:

Module:

LoadModule ssl_module modules/mod_ssl.so

With Site Profiles:

Include conf/extra/httpd-ahssl.conf

The # in front of it is removed

Then modify the httpd-ahssl.conf file in the conf/extra folder

<VirtualHost _default_:443></virtualhost> removed

Add code:

<VirtualHost *:443>

  SSLEngine is enabled

  ServerName 1.com:443

  ServerAlias www.1.com:443

  SSLCertificateFile “C:/ssl/public.crt”

  SSLCertificateKeyFile “C:/ssl/key.key”

  SSLCertificateChainFile “C:/ssl/chain.crt”

  DocumentRoot “C:/wz1”

< directory "C:/wz1" >

    Options Indexes include FollowSymLinks

    All need to be granted

</Directory>

</virtualhost>

Among them, SSLCertificateFile sets the public key, SSLCertificateKeyFile sets the private key, SSLCertificateChainFile sets the general key, and there is a configuration as follows:

SSLCertificateFile “C:/ssl/public.crt”

SSLCertificateKeyFile “C:/ssl/key.key”

The SSLCertificateChainFile setting is missing because the SSLCertificateFile and SSLCertificateChainFile contents need to be combined.

In the same way as the setting method of http multiple sites, copy multiple copies of the code in the <VirtualHost *:443></virtualhost>, and then modify the ServerName, the domain name bound to ServerAlias, the SSLCertificateFile, SSLCertificateKeyFile, the SSLCertificateChainFile ssl certificate, DocumentRoot and Directory website directory addresses, and then save and restart apache to complete the configuration of multiple sites and domain names.

The above is the method of configuring multiple different site projects and bound domain names in Apache 2.4 in the win system, and setting HTTP and HTTPS protocols for multiple projects.

Popular articles
latest article