Home Backend Development PHP Tutorial PHP Beginner: Configure Apache Server and Set Up DNS_PHP Tutorial

PHP Beginner: Configure Apache Server and Set Up DNS_PHP Tutorial

Jul 13, 2016 pm 05:29 PM
apache dns php host beginner I technology yes server point of parse set up Configuration

当然主要有技术含量的是点DNS的解析,我正给我们公司负责,先查资料的说...‘

  我们通常所说的虚拟主机技术就是将一台(或者一组)服务器的资源(系统资源、网络带宽、存储空间等)按照一定的比例分割成若干台相对独立的“小主机”的技术。每一台这样的“小主机”在功能上都可以实现WWW、FTP、Mail等基本的Internet服务,就像使用独立的主机一样。
 
    目前网站服务器的虚拟主机平台使用以开放的Apache为最多,其次是微软的Windows IIS。Apache具有跨平台(FreeBSD/Linux/Windows/Solaris/Other UNIX)、易于维护与最佳安全性等优点。
    Apache是率先支持基于IP虚拟主机的服务器之一。 Apache 1.1及其更新版本同时支持基于IP和基于主机名的虚拟主机,不同的虚拟主机有时会被称为基于主机(host-based) 或非IP虚拟主机(non-IP virtual hosts)。

    用Apache设置虚拟主机服务通常可以采用两种方案:基于IP地址的虚拟主机和基于主机名字的虚拟主机,下面我们分别介绍一下它们的实现方法以及优缺点。以便大家在具体的应用中能够选择最合适的实现方法。

   一、Apache实现基于IP地址的虚拟主机(每个站点拥有一个独立IP地址)

    使用这种虚拟主机方式,首先要在服务器上为每个虚拟主机单独设置一个IP地址。这些IP地址可以通过增加多个网卡或者在一个网卡上设立多个IP地址来完成。有了多个IP地址后,可以采用以下两种方式之一来设置Apache。

1、为每个虚拟主机运行一份Apache
    采用这种方式,每一份Apache程序可以以单独的用户运行,因此各个虚拟主机之间互不影响。设置这种虚拟主机时,只要为每一份Apache设置一套配置文件就可以了,唯一需要注意的是:必须使用“Listen”语句,强制每一份Apache 仅仅在属于“自己”的IP地址上接收服务请求。

    优点:各个虚拟主机之间互不干扰,安全性高。
    缺点:占用系统资源较多。

2、多个虚拟主机共享同一份Apache
    采用这种方式,各个虚拟主机共享同一份Apache,因此各个虚拟主机之间有一定的影响,尤其是执行CGI程序时,可能会带来一些严重的安全问题。设置这种虚拟主机时,只要为每一个虚拟主机设置类似如下的信息即可:
   
    DocumentRoot /www/ghq1
    …
   

    优点:占用系统资源比上一种方式少。
    缺点:安全性低,每个虚拟主机仍然需要占用一个IP地址。

  
    例如服务器一个网卡上绑定有两个IP地址(172.16.3.40和 172.16.3.50)分别对应域名 www.ghq1.com和www.ghq2.org的服务,配置如下:

    服务器配置(apache的配置文件httpd.conf)
    Listen 80

   
    DocumentRoot /www/ghq1
    ServerName www.ghq1.com
   

   
    DocumentRoot /www/ghq2
    ServerName www.ghq2.org
   

    配置简单说明:“Listen”默认httpd服务会监控第80号通信端口, “Listen”选项让用户自行指定apache 服务器监控的IP地址或通信端口。

    “DocumentRoot”:指定apache 服务器存放网页的根目录;“ServerName”:允许用户自行设置主机名,这个名称将被送到远程连接程序,以取代安装apache 服务器主机的真实名称。构成虚拟主机的语法结构,其中的IP就是我们在服务器上绑定的不同的IP地址,也可以是IP地址加上通信端口号(见下面的例子)。

    如果服务器有两个IP地址(172.16.3.40和 172.16.3.50)分别对应域名 www.ghq1.com和www.ghq2.org。对每个域名,我们都希望在80端口和8080端口发布我们的网站。可以这样配置:

Server configuration (apache configuration file httpd.conf)
Listen 172.16.3.40:80
Listen 172.16.3.40:8080
Listen 172.16.3.50:80
Listen 172.16.3.50: 8080


DocumentRoot /www/ghq1-80
ServerName www.ghq1.com


DocumentRoot /www/ghq1-8080
ServerName www.ghq1.com


DocumentRoot /www/ghq2-80
ServerName www.ghq1.org


  DocumentRoot /www/ghq2-8080
  ServerName www.ghq2.org

Therefore, to establish a virtual host, we need to do domain name resolution work corresponding to different IPs, establish a corresponding directory (such as /www/ghq1), and store the corresponding homepage content in the corresponding directory.

2. Apache implements a virtual host service based on the host name (one IP address implements multiple websites)

The virtual host service based on the host name is a commonly used solution for virtual hosts at present. Because it does not require more IP addresses and no special software or hardware support. And most of today's browsers support this virtual host implementation method. Domain name-based virtual hosts are determined based on the hostname part of the HTTP header submitted by the client. Using this technology, many virtual hosts can share the same IP address.

Domain name-based virtual hosting is relatively simple, because we only need to configure the DNS server to map each host name (CNAMES) to the correct IP address, and then configure the Apache HTTP server to recognize different host names. . Domain name-based servers can also alleviate the problem of insufficient IP addresses (IPV4). In this way, each virtual host shares the same Apache, so when a CGI program is running, the security is not high.

Advantages: Only one IP address can provide a large number of virtual host services.
Disadvantages: Poor security. Maintaining these virtual hosts requires configuration file changes, and the Apache process needs to be restarted for it to work. Therefore, it is not suitable for large-scale virtual hosting services.

If the server has only one IP address and there are many mapped to this machine in DNS. We want to run two sites www.ghq1.com and www.ghq2.org on this machine. Creating a virtual host in the Apache server configuration does not automatically update the hostname in DNS accordingly. We have to add the domain name in DNS ourselves to point to our IP address. Otherwise others will not be able to see our web site.

Server configuration (apache configuration file httpd.conf)
# Ensure that Apache listens on port 80
Listen 80

# Listen for virtual host requests on all IP addresses
NameVirtualHost *


DocumentRoot /www/ghq1
ServerName www.ghq1.com

# Other directives here


DocumentRoot /www/ghq2
ServerName www.ghq2.org

# Other directives here

Because * (asterisk) matches all addresses, the main server does not accept any requests. Because www.ghq1.com appears first in the configuration file, it has the highest priority and can be considered the default or primary server. This means that if an accepted request does not match a ServerName directive, it will be served by the first VirtualHost.

When our IP address cannot be determined, it is very convenient to use * - for example, the ISP has configured us with a dynamic IP address (such as ADSL dial-up Internet access), and we have used some kind of dynamic domain name resolution system time. Because * matches any IP address, in this case, no matter how the IP address changes, we do not need to configure it separately. The above configuration is that we use domain name-based virtual hosts in most cases

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/509201.htmlTechArticleOf course, the main technical content is the resolution of DNS. I am responsible for our company, so I will check the information first. ...' What we usually call virtual host technology is to combine one (or a group of) services...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

How to set the cgi directory in apache How to set the cgi directory in apache Apr 13, 2025 pm 01:18 PM

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How to start apache How to start apache Apr 13, 2025 pm 01:06 PM

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

What to do if the apache80 port is occupied What to do if the apache80 port is occupied Apr 13, 2025 pm 01:24 PM

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

See all articles