Home>Article>Backend Development> Teach you to create a virtual host and run php projects (phpstudy + wamp)

Teach you to create a virtual host and run php projects (phpstudy + wamp)

藏色散人
藏色散人 forward
2022-08-07 15:17:30 6000browse

Creation of PHP environment and virtual host (phpstudy wamp)

This article involves two PHP Integrated environment

  • phpstudy
  • wampserver

Both of these two include mysql apache php, phpstudy is more powerful than wamp, and is very Simple and easy to use.

phpstudy is easy to use because it helps us encapsulate many tedious and error-prone operations. However, if we use it directly, we do not understand the implementation behind it, so learning wamp first can familiarize us with the principles.

1. Create a website with wamp

1.1 Understand the default website program

After the installation is completed, the tray icon is normal green.
Teach you to create a virtual host and run php projects (phpstudy + wamp)

My program is installed inD:\wamp64. After finding it normal, directly access the pagelocalhost. The default port is 80. You can ignore

Teach you to create a virtual host and run php projects (phpstudy + wamp)

to get a normal response. This page comes fromD:\wamp64\www\index.php, which is under the wamp installation directory. The www folder stores the default website programs.

1.2 Create a new virtual host

At this point we need to understand a few files first.


  1. hosts, file location:C:\Windows\System32\drivers\etc
    Teach you to create a virtual host and run php projects (phpstudy + wamp)

Teach you to create a virtual host and run php projects (phpstudy + wamp)

The function is to establish an associated "database" between some commonly used URL domain names and their corresponding IP addresses. When the user enters a URL that needs to be logged in in the browser, the system will first automatically start fromHosts filelooks for the corresponding IP address. Once found, the system will immediately open the corresponding web page. If not found, the system will submit the URL to the DNS domain name resolution server for IP address resolution.

  1. httpd.conf, file location:D:\wamp64\bin\apache\apache2.4.17\conf

This file is the configuration file of apache and generally does not need to be changed.

  1. httpd-vhosts.conf, file location:D:\wamp64\bin\apache\apache2.4.17\conf\extra

This file is a virtual host file. When creating a new website, it needs to be configured in this file to take effect.


The following I have a requirement:

I think it is too wasteful to run only one program on one computer, and port 80 of my local machine is occupied. I want to open a website for ittest.com:81to access

1) According to the function of the hosts file, when I visit test.com:81, I need it to jump directly without going. Perform dns domain name resolution

So, add a line in the hosts file according to the fixed format

# 当访问www.test.com时 , 我们告诉电脑直接解析到本机127.0.0.1 不用去dns域名解析。# :81属于端口号 不需要添加到这里127.0.0.1 www.test.com

2). The previous step only tells the computer to resolve to the local, but we haven’t done that yet. The corresponding virtual host is

, so a new virtual host needs to be configured inhttpd-vhosts.conf.

 DocumentRoot "网站程序目录" ServerName 绑定的域名 ServerAlias 绑定的域名别名  Options FollowSymLinks ExecCGI AllowOverride All Order allow,deny Allow from all Require all granted  

Teach you to create a virtual host and run php projects (phpstudy + wamp)

In additionVirtuaHost *:Port number

After filling in here, it will not take effect because we have not allowed apache to be enabled. Virtual host, now enable it, in thehttpd.conffile

Teach you to create a virtual host and run php projects (phpstudy + wamp)

Finally set the default port of wamp's apache service:

Teach you to create a virtual host and run php projects (phpstudy + wamp)
Teach you to create a virtual host and run php projects (phpstudy + wamp)
Successfully obtained response.

2. Creating a website with Phpstudy

After reading wamp, it feels very cumbersome. Next, try using phpstudy.

The demand remains unchanged, let’s create a websitewww.test.com:81,

1.1 Effect demonstration

1), directly Start apache

Teach you to create a virtual host and run php projects (phpstudy + wamp)

2) Create website

Teach you to create a virtual host and run php projects (phpstudy + wamp)

根目录选择项目的根目录

三)、查看效果

Teach you to create a virtual host and run php projects (phpstudy + wamp)

这就完成了…

1.2 配置文件查看

使用过wamp后我们知道,配置一个虚拟主机需要改三个文件,下面看一下phpstudy的操作。

一)、hosts

由于做wamp的时候已经手动添加过了,所以这个文件看不出来两者的差异。

二)、httpd.conf和vhost.conf

Teach you to create a virtual host and run php projects (phpstudy + wamp)

在wamp中,我们将多个虚拟主机的配置项都放在了一个文件中。

在phpstudy里,将每个网站的配置项单独抽离出个自己的文件并放到了一个文件夹中。

httpd-vhosts.conf详解

首先看下面的配置:

 ServerAdmin webmaster@dummy-host.example.com DocumentRoot "D:/xampp/htdocs/wherein" ServerName www.shop_dev.com ErrorLog "logs/wherein.com-error.log" CustomLog "logs/wherein.com-access.log" common  Options FollowSymLinks IncludesNOEXEC Indexes DirectoryIndex index.html index.htm index.php AllowOverride all Order Deny,Allow Allow from all Require all granted  

ServerAdmin指令:

语法: ServerAdmin email-address|URL

用来设置服务器返回给客户端的错误信息中包含的管理员邮件地址。便于用户在收到错误信息后能及时与管理员取得联系。

ServerName指令:

语法:ServerName [scheme://] FQDN [:port]

用来设置服务器用于辨识自己的主机名和端口号。主要用于创建重定向URL。

DocumentRoot指令:

语法:DocumentRoot directory-path

用来设置httpd提供服务的目录。即你所在项目入口处的文件夹。

ErrorLog指令:

语法: ErrorLog file-path

来设置当服务器遇到错误时记录错误日志的文件。如果file-path不是以/开头的绝对路径,那么将会被认为是一个相对于ServerRoot的相对路径。

CustomLog指令:

语法: ErrorLog file-path common

设置日志文件,并指明日志文件所用的格式(通常通过格式的名字)。

为主目录或虚拟目录设置权限。

特性:

Options FollowSymLinks IncludesNOEXEC Indexes

命 令 说 明
Indexes 允许目录浏览当客户仅指定要访问的目录,但没有指定要访问目录下的哪个文件,而且目录下不存在默认文档时,Apache以超文本形式返回目录中的文件和子目录列表(虚拟目录不会出现在目录列表中)
MultiViews 允许内容协商的多重视图MultiViews其实是Apache的一个智能特性。当客户访问目录 中一个不存在的对象时,如访问“http://192.168.66.6/data/a”,则Apache会查找这个目录下所有a.*文件。由于 data目录下存在a.gif文件,因此Apache会将a.gif文件返回给客户,而不是返回出错信息
All All包含了除MultiViews之外的所有特性,如果没有Options语句,默认为All
ExecCGI 允许在该目录下执行CGI脚本
FollowSymLinks 可以在该目录中使用符号连接
Includes 允许服务器端包含功能
IncludesNoExec 允许服务器端包含功能,但禁用执行CGI脚本

一旦定义允许目录浏览,就会将Web站点的文件夹和文件名结构暴露给黑客。目录浏览还会允许黑客浏览文件并掌握服务器配置信息,所以指定该权限往往带来安全性上的隐患。除非有充足的理由要使用目录浏览,否则应该禁用它。

DirectoryIndex index.html index.htm index.php

设置访问目录后进入的默认文件

AllowOverride all

定义位于每个目录下.htaccess(访问控制)文件中的指令类型。none为禁止使用.htaccess文件

Order Deny,Allow

Allow from all

设置缺省的访问权限与Allow和Deny语句的处理顺序

allow, deny:缺省禁止所有客户机的访问,且Allow语句在Deny语句之前被匹配。如果某条件既匹配Deny语句又匹配Allow语句,则Deny语句会起作用(因为Deny语句覆盖了Allow语句)。

deny, allow:缺省允许所有客户机的访问,且Deny语句在Allow语句之前被匹配。如果某条件既匹配Deny语句又匹配Allow语句,则Allow语句会起作用(因为Allow语句覆盖了Deny语句)。

eg.

Order deny ,allow Deny from baidu.com Deny from 192.168.66.6除了来自baidu.com域和ip地址为192.168.66.6的客户机外,允许所有客户机访问 Order deny ,allow Allow from 192.168.66.6 Deny from 192.168.66.1既匹配Deny语句又匹配Allow语句,由于allow语句覆盖了deny语句,所以是允许所有客户机访问 Order allow ,deny Allow from 192.168.66.6 Deny from 192.168.66.1既匹配Deny语句又匹配Allow语句,由于deny语句覆盖了allow语句,所以是禁止所有客户机访问

推荐学习:《PHP视频教程

The above is the detailed content of Teach you to create a virtual host and run php projects (phpstudy + wamp). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete