How to configure multiple sites on the Apache server (based on the host name)

For our grass-roots webmasters, it is a very good method to place multiple sites on the same host. In order to make the limited resources meet our infinite challenges, let's talk about how to place multiple sites on one Apache server. Of course, these pediatrics are only for entry-level personnel. Don't beat the brick!

Apache服务器配置多个站点的方法(基于主机名)

How to configure multiple sites on the Apache server (based on the host name)

One IP address places multiple sites (virtual host based on host name, that is, virtual space sharing IP) In short, it is implemented through the virtual host of Apache. Let's take a look at the specific operation steps:

1. Find the Apache configuration file: httpd.conf

For the configuration file of Apache, different Linux hosts may be placed in different places. Especially for those compiling and installing environments, the location is more flexible. Find httpd.conf and open it with an editor.

 Open the Apache installation directory, find the httpd.conf file, and remove the # sign in front of the following two lines of text.    LoadModule vhost_alias_module modules/mod_vhost_alias.so Removing # means to enable the virtual host function of Apache.    Include conf/extra/httpd-vhosts.conf   Removing the # from this line means importing the virtual host configuration from the conf/xtra/httpd-vhosts.conf file. Open it to write the virtual machine configuration in the httpd-vhosts.conf file instead of the httpd.conf file. It is also possible to write the configuration directly in httpd.conf, but this is not recommended.

2. Modify the configuration file and add a virtual host

Find the following configuration information:

#<VirtualHost *:80>

# ServerAdmin webmaster@dummy-host.example.com

# DocumentRoot /www/docs/dummy-host.example.com

# ServerName dummy-host.example.com

# ErrorLog logs/dummy-host.example.com-error_log

# CustomLog logs/dummy-host.example.com-access_log common

#</VirtualHost>

The above configuration information is the template provided by Apache for us to add virtual hosts. You can either modify it on the original basis or add it yourself according to this template. Usually, the following information is added at the end of the entire file:

<VirtualHost *:80>

# ServerAdmin webmaster@dummy-host.example.com

DocumentRoot /var/www/web/wordpress

ServerName www.mr-c.cn

ErrorLog logs/www.mr-c.cn-error_log

CustomLog logs/www.mr-c.cn-access_log common

</VirtualHost>

<VirtualHost *:80>

# ServerAdmin webmaster@dummy-host.example.com

DocumentRoot /var/www/web/wosn

ServerName www.wosn.net

ErrorLog logs/www.wosn.net-error_log

CustomLog logs/www.wosn.net-access_log common

</VirtualHost>

Here we use Watson's two domain names, www.wosn.net and www.mr-c.cn, and replace them with their own domain names in practical applications. Of course, if you want to configure more sites, you can continue to add them in this way. We use two sites as instructions.

Taking the domain name www.wosn.net as an example, we need to clarify one question. What if you resolve both www.wosn.net and wosn.net when you resolve the domain name, and you may visit www.mr-c.cn instead of www.wosn.net when you visit wosn.net? We can add another virtual host in the configuration file, that is, add:

<VirtualHost *:80>

# ServerAdmin webmaster@dummy-host.example.com

DocumentRoot /var/www/web/wosn

ServerName wosn.net

ErrorLog logs/www.wosn.net-error_log

CustomLog logs/www.wosn.net-access_log common

</VirtualHost>

3. After configuration, restart Apache

After modifying the configuration file, remember to restart Apache, or the modification will not work. This is the most easily ignored problem for many beginners.

In this way, the configuration of one Apache server placing multiple sites is completed. I hope it will be helpful to everyone!


An HTTP error occurred 403 - Access Forbidden Solution

HTTP error 403 - Forbidden: You don't have permission to access/on this server

It may be caused by insufficient permissions.

resolvent:

Open the configuration file httpd.conf of Apache and check line by line. Found:

 <Directory /> Options FollowSymLinks AllowOverride None Order deny,allow Deny from all </Directory>

Here "Deny from all" means to refuse all connections.

Modify this line to "Allow from all" to solve the problem.

The modified code is:

 <Directory /> Options FollowSymLinks AllowOverride None Order deny,allow allow from all </Directory>

Open it again in the browser and display it works! Problem solving.

403 Forbidden Main Cause Analysis

1. Your IP is blacklisted

2. You visit this website too much in a certain period of time (usually using the collection program), and are denied access by the firewall;

3. The domain name of the website is resolved to the space, but the space is not bound to this domain name;

4. Your web script file has no permission to execute in the current directory;

5. Create/write a file in a directory that does not allow writing/creating files;

6. The server is busy, and the same IP address sends too many requests, which is intelligently blocked by the server;


For format setting, it should be noted that in addition to setting serverName and DocumentRoot, directories should also be set, that is,<Directory></Directory>. Otherwise, there will be problems such as unauthorized access. Other parameters such as error log, user access information, alias settings, etc. can be set or omitted.

The above methods can also be written in the virtual space configuration<VirtualHost *: 80></ VirtualHost>Middle, in the following format:

 <VirtualHost *:80> #ServerAdmin  admin@myxzy.com ServerName dummy-host.example.com? DocumentRoot "g:/www2" DirectoryIndex?index.html?index.php?? ServerAlias?www.dummy-host.example.com?? <Directory "g:/www2"> Options FollowSymLinks AllowOverride All Require all granted </Directory>  </VirtualHost>

 


Apache VirtualHost Configuration

Take lampp environment as an example. Other environments only have different paths of configuration files.

First of all, you need to? In lampp/etc/httpd.conf (this is the general configuration file of Apache), remove the comments on the virtual path.

#Include etc/extra/httpd-vhosts.conf

Make the httpd-vhosts.conf file work, or write the configuration directly in httpd.conf, but this is not recommended.

The related configurations are: Listen? NameVirtualHost ?<VirtualHost>

1. Listen ? The port to listen to, multiple ports, and multiple Listens to write; Otherwise, when Apache starts, the corresponding interface will not be started

For example:

Listen 80

Listen 8080

2. Without NameVirtualHost, the<VirtualHost>tag is useless. (The new version has been abolished)

(This refers specifically to domain name based access. For IP based access, the VirtualHost of the first specified IP shall prevail, and each IP can be specified separately.)

One NameVirtualHost can use multiple<VirtualHost>, and each<VirtualHost>must have its own NameVirtualHost (I guess);

NameVirutalHost *:80

Specify the IP and port of this host. If there are multiple IPs on the server, you can specify which host is a port of an IP.

(The new version of Apache has removed the NameVirtualHost configuration, because it is really useless, and the parameters have been specified in VirtualHost.)

3. The most critical VirtualHost

Important: When Apache receives a request, it will first default to the first VirtualHost, and then find a match. If there is no match, the first VirtualHost will work.

Therefore, in httpd.conf,<Decretory/>(this is the default configuration for all directories)

And<Directory/opt/lampp/htdocs>are deny from all.

So my first VirtualHost is

<VirtualHost *:80>

ServerName *

DocumentRoot </opt/lampp/htdocs/guest>

<Directory /opt/lampp/htdocs/guest>

Order deny,allow

Allow from all

</Direcotry>

<VirtualHost/>

Later, add them in turn

<VirtualHost *:80>

ServerName www.myweb1.com

DocumentRoot </opt/lampp/htdocs/myweb1>

<Directory /opt/lampp/htdocs/myweb1>

Order deny,allow

Allow from all

</Direcotry>

<VirtualHost/>

The nginx virtual host configuration is in the vhost.conf file;

Welcome to leave a message to communicate and make progress together!

 Watson Blog
  • This article is written by Published on October 22, 2017 00:15:08
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/707.html

Comment