PHP virtual host configuration

The steps are very simple, so let's talk about the pits that have been stepped on.

(1) Detailed explanation of the directory for configuring the virtual host, that is, the detailed configuration precautions for the directory. Just started writing in the following format

<Directory>
        #Allow all access
        #allow from all
        #Allow overriding
        #AllowOverride all
        # Indicates that the site is allowed to display the file directory structure
        Options +indexes
</Directory>

   After saving and restarting Apache, it was found that the startup failed. After searching, it is found that the directory file needs to be added to the site document root directory, otherwise the matching will fail

   Here, I changed to

<Directory "C:\site">
...
</Directory>

 

 

 

 

①Create a site directory

②Enable the httpd-vhosts.conf configuration function

    Find the httpd.conf file in the \conf directory of Apache, the location: because the integration package I use is in Apache in the first-level directory


Specific path:


 

 

   Open it and find the following two lines:

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
Change it to the following:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

    It means to enable the virtual host setting file httpd-vhosts.conf, the virtual host settings are all set in this file

③ Configure the configuration file of the Apache virtual host to create a virtual host;

    The file is located in the Apache directory, conf/extra/httpd-vhosts, after opening the configuration file, copy a section of the previous site configuration code---virtual host configuration, and paste it at the end

#custom virtual host
<VirtualHost *:80>
    #Configure the site administrator mailbox, when the site reports 500 server errors, an error message will be prompted on the page,
      #And list the administrator mailbox
    ServerAdmin [email protected]
    #site document root directory
    DocumentRoot "C:\site"
    #Site binding domain name
    ServerName www.1006.com
    #Site alias ServerAlias ​​1006.com (usually a domain name without www)
    #The storage location of the error log, the logs directory is in the Apache directory, not in the site root directory
    ErrorLog "logs/1006-error.log"
    #The storage location of normal access logs is also in the Apache directory. The last common is the record rule name of the log,
    #Define logging rules in Apache's group configuration file httpd.conf
    CustomLog "logs/1006-access.log" common
    #Detailed configuration for the directory
    <Directory>
        #Allow all access
        allow from all
        #Allow overriding
        AllowOverride all
        # Indicates that the site is allowed to display the file directory structure
        Options +indexes
    </Directory>
</VirtualHost>

 ④Restart Apache

⑤ Modify the hosts file to bind and resolve the two domain names declared in the configuration file

    The location of the hosts file: C:\Windows\System32\drivers\etc\hosts, the shortcut operation win+R pops up the run dialog box, enter the drivers to find the etc directory

    Edit hosts: enter 127.0.0.1 www.1006.com 1006.com (two domain names can be written on one line, the site domain name is written in the front, and the site alias is written in the back)

⑥ Inspection

   Use the ping command in cmd to test whether the domain name is successfully bound

   Check whether the IP points to the local cdm input: ping ip

   ping is an executable command that comes with the Windows series. It can be used to check whether the network can be connected, and it can be used to help us analyze and determine network failures. Application format: Ping IP address

Now the browser enters the custom ip, and you will see the index of /, which means that the site directory is listed.

At this point, the virtual host configuration is complete.

⑦Copy the code file to the site directory

   Copy the ThinkPHP directory and the index.php entry file to the site root directory. Access the new domain name through a browser, and you will see Welcome to ThinkPHP! interface

   And the Application directory will be automatically created in the site directory. Because the application directory is defined in the entry file index.php, the Application directory does not need to be introduced when referencing files.

// Define the application directory, and the files automatically generated later will be placed in it
define('APP_PATH','./Application/');



 

 

 Share other articles:

Let's talk about the process of configuring virtual hosts:

1. First open the httpd.conf file and open httpd-vhosts.conf

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

2. Then open the httpd-vhosts.conf file and configure a virtual host

<VirtualHost *:80>
    DocumentRoot "D:/amp/www/wechat"
    ServerName www.wechat.com
</VirtualHost>

3. Open the hosts file and add a domain name

127.0.0.1        www.wechat.com

4. Restart Apache. At this time, the domain name of the virtual host www.wechat.com can be accessed normally.

 But my localhost cannot be accessed. The error is as follows:

Resolution process:

My idea is, can't add a virtual host named localhost?

1. Open the httpd-vhosts.conf file and configure a virtual host named localhost:

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "D:/amp/www"
</VirtualHost>

2. Open the hosts file and add a domain name

Note: This step, whether you do it or not, has no effect, and the test is passed.

#127.0.0.1        localhost

3. Restart Apache, the expected result does not appear, hey, I am exhausted, still can't access localhost, although other virtual domain names can be accessed normally,

 But I just want to have both, keep looking for the problem

 

problem solved:

Finally, I compared my original configuration code and found that there is a line of code in the httpd.conf file that needs to be commented out

#ServerName localhost:80

It's this line of code, I've been tossing it hard enough, just comment out the line of ServerName.

Maybe the value of localhost:80 is not in your httpd.conf file, it may be ServerName a.com:80 ; all the same, just comment it out

In the future, no matter whether the virtual host is turned on or off, localhost can be accessed normally.

 

 

Other articles 2:

First of all, we need to understand why we can access our apache homepage by visiting localhost .

When parsing a domain name, it starts with the local hosts file .

如果查不到,才会去DNS服务器查询。

解析:

在Windows操作系统C:\WINDOWS\system32\drivers\etc目录下的hosts文件,存放着一些主机名和IP地址的映射表。通常,客户机需要进行域名解析时,系统会先读取该文件,在其中查找对应域名的IP地址。若查找失败,则将域名解析任务提交给该主机所配置的首选DNS服务器进行查询。默认情况下,该文件必须包含的一条“127.0.0.1 localhost”记录。

如果你在这里面写一行:127.0.0.1 www.baidu.com

百度你是肯定上不了。因为访问的是你自己的机器。

hosts 就是一个映射(域名和IP的对应,localhost也可以说是一个域名) localhost -> 127.0.0.1 

找到C:\WINDOWS\system32\drivers\etc\hosts ,修改如下

不多解释,这就是为什么计算机认识localhost的原因。

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326121852&siteId=291194637