How to configure Apache virtual host [detailed steps]

When developing a project in a local environment, it is often necessary to deploy multiple websites, and each website can be accessed with a corresponding domain name. This effect can be achieved through the virtual host function of Apache. The specific configuration steps of Apache virtual host are as follows.

(1) Configure domain name. Since it is troublesome to apply for a real domain name, in order to facilitate learning and testing, you can change the hosts file of the operating system to resolve any domain name to a specified IP address. In the operating system, the hosts file is used to configure the resolution relationship between the domain name and the IP address. When the requested domain name has a resolution record in the hosts file, the record can be used directly. Only when there is no resolution record, the DNS domain name is passed. The parsing server performs parsing.

Run the command line tool as an administrator and enter the following command to open the hosts file.

C: WINDOWS \system32> notepad drivers\etc\hosts

The above command means using notepad to open the hosts file. After opening the file, add the following line at the bottom of the file.

127.0.0.1 laravel.test

After the above configuration, you can access the local web server through http:/laraveltest on the browser. This method is only valid for this machine. Since the virtual host has not been configured yet, http:/laravel.test is used to access Apache's default host at this time.

(2) Enable the virtual host auxiliary configuration file. There are some auxiliary configuration files in the confextra directory of Apache. These files are extension files of hitpd.conf, which are used to extract part of the configuration for easy modification. Open the hitpdleonf file and find a line of configuration as shown below. Delete the preceding "#" to enable the virtual host auxiliary configuration file.

#Include conf/extra/httpd-vhosta.conf

(3) Configure virtual host. Open the confextralhtpd-vhosts.conf auxiliary configuration file, and you can see the default configuration provided by Apache, as follows:

<VirtualHost *:80>
   ServerAdnin vebmasterêdunmy-host.exanpleacom
   DocunentRoot "c:/Apache24/docs/dungay-host.example.com"
   ServerName dummy-host.example,com
   ServerAlias snnw.dummy-host.exanple.com
   ErrorLog "1ogs/dummy-host.ekanple.com-error_log"
   customxlog "logs/dunay-hostresacple.con-access_log" comon
</VirtualHost>

In the above configuration, "*80" in line 1 indicates that the host is accessed through port 80; ServerAdmin is the email address of the administrator; DocumentRoot is the document directory of the virtual host; ServerName is the domain name of the virtual host; ServerAlias ​​is used to configure multiple domain names Alias ​​(separated by spaces); Erorlog is the error log; Customlog is the access log, and the following common indicates that the log format is a common format.

The above default configuration is not used in this book, so you can delete it directly, or you can comment it all with "#" for easy reference. Then write the reader's own virtual host configuration, as follows:

<VirtualHost ·:80>
   DocumentRoot "c:/web/apache2.4/htdocs"
   ServerNane localhost
</VirtualHost>
<VirtualHost*:80>
   DocumentRoot "c:/web/www/laravel/public"
   ServerNane laravel.test
</VirtualHoat>
<Directory "c:/web/www">
   Options -indexes
   AllowOverride All
   Require local
</Directory>

The above configuration implements two virtual hosts, namely localhost and laravel.test, and the site directories of these two virtual hosts are specified in different paths. Lines 9~13 are used to configure the access permissions of the c/web/www path. Among them, the 10th line is used to close the file list function; the 11th line is used to open the distributed configuration file, and will automatically read the configuration in the .htaccess file under the directory after opening; the 12th line is used to configure the directory access authority, set Require local means that only local access is allowed. If all access is allowed, it can be set to Require all granted; if all access is denied, it can be set to Require all denied.

(4) Write test files. Create the C:webwwwlaravelpublic directory, and write an index.html web page with the content of Laravel in the directory. Then restart the Apache service to make the configuration take effect, and use a browser to conduct an access test. The page effects of the two virtual hosts, localhost and laravel.test, are shown in Figure 1-8.

1693900547452_Configuration installation.png

Guess you like

Origin blog.csdn.net/zy1992As/article/details/132711472