How to build multiple Web sites using Apache's VirtualHost on a single server

  • 2020-06-15 10:58:31
  • OfStack

preface

This article will detail how to use apache's virtualhost(virtual host) to build several different web sites on a single server, and each site manages its own session.

The development environment

First, let's talk about my development environment parameters:

Operating System: RedHat6.7(CentOS) WEB server: apache2.2 php5.6.30

Modify the Apache configuration

apache2. 2 of the configuration file path in the/etc httpd/conf/httpd conf

We modify the apache configuration file with the following command:


$ vim /etc/httpd/conf/httpd.conf

Add listener ports

Find the following section,


#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, in addition to the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80
Listen 80

By default, you should only listen on port 80, where we'll add the port number for another site. For example, our A site is the default port 80, and the B site is planned to be built on port 8080. The final configuration file is changed to


...
#Listen 12.34.56.78:80
Listen 80
Listen 8080

Start and add VirtualHost

Then find the following sections in the configuration file:


### Section 3: Virtual Hosts
#
# VirtualHost: If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
 NameVirtualHost *:80
 NameVirtualHost *:8080

The above code is what I have changed. By default, the last two lines of NameVirtualHost should also be commented out. Since we are going to enable the virtual host, we have set up the two ports we were listening on.

In the meantime, to change the configuration file to look like this, let's first set the default port 80 site A


#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
 DocumentRoot /var/www/webA
 ServerName webA
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

Apache doesn't have VirtualHost on by default, so the code is commented out. We just need to comment out the DocumentRoot and ServerName lines and edit them.

DocumentRoot refers to the site root location of our A site

Then add the B site on port 8080.


<VirtualHost *:8080>
 DocumentRoot /var/www/webB
 ServerName webB
</VirtualHost>

Here, restart 1 Apache service (service httpd restart) and you can access two different sites.

Independent Session

If our A, B two sites login logic is using a set of code, then we will find that the A site and B site Session is Shared, that is, if the user in A site login, B site is not required, automatically logged in state; Users who exit the A site also exit the B site automatically.

This is obviously not the result we want, and the reason is that A, B and B share one Session system, which is why this is a problem.

The solution was to specify the location of session on the site.

We also modify the specified virtual host in the configuration file. Taking site B as an example, we modify the configuration file as follows:


<VirtualHost *:8080>
 DocumentRoot /var/www/webB
 ServerName webB
 <Directory "/var/www/webB">
  AllowOverride All
  php_value session.save_path "/var/lib/php/session_B"
 </Directory>
</VirtualHost>

session. save_path in php_value is session. save_path in php. ini file. The default php session file is stored in the/var/lib/php/session/folder.

B, try to visit the following site and 1 some session access operation, go back to/var/lib/php/session_B folder, you will find new session file.

conclusion


Related articles: