Build a virtual host using Apache HTTP Server

Source: Internet
Author: User
Tags fully qualified domain name

Directory:
Apache Overview
Install Apache HTTP Server Software
Configuration File Parsing
Application Cases

Apache Overview
The Apache HTTP Server Project is a cross-platform open-source HTTP Server software that can run on Windows and Unix platforms. The project aims to provide secure, efficient, and scalable HTTP Services. Apache httpd has become the world's No. 1 Web server software since its release in 1996. The latest stable version of Apache httpd is 2.4.4. You can select Source Code installation or binary package installation for installing Apache httpd software. However, since source code installation can be customized, this installation method is flexible, it can meet different requirements of enterprises for such environments. You can select RPM to install the Binary Package in CentOS 6.3. The biggest advantage of this installation is that it is simple and fast. This book uses the source code installation package. Because the source code installation requires a large number of dependent packages, you need to install these dependent packages first.
An important feature of Apache HTTP Sever is that it adopts a modular design model. Apache modules are divided into static modules and dynamic modules. The static module is the most basic module of Apache, it is a module that cannot be added or detached at any time. The static module is set during software compilation. Dynamic modules can be added or deleted at any time. This design allows enterprises to achieve maximum flexibility when deploying Apache. Each enterprise can meet their own platform and actual needs, install and use different modules. The Apache module will be compiled as a dynamic shared object DSOs). These dynamic shared objects are independent of httpd programs. The DSO module can be added when compiling Apache, or you can use Apache Extension Toolapxs at any time) compile and add modules. After the Apache HTTP Server software is installed, you can use httpd-M to view the module load list.
Install Apache Software
1. download the software package

 
 
  1. [root@centos6 ~]# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.4.tar.gz 
  2. [root@centos6 ~]# wget http://mirror.bjtu.edu.cn/apache/apr/apr-1.4.6.tar.gz 
  3. [root@centos6 ~]# wget http://mirror.bjtu.edu.cn/apache/apr/apr-util-1.5.2.tar.gz 

2. Install software

 
 
  1. [root@centos6 ~]# yum –y install gcc autoconf automake make \ 
  2. > pcre pcre-devel openssl openssl-devel 
  3. [root@centos6 ~]# tar –xzf httpd-2.4.4.tar.gz –C /usr/src/ 
  4. [root@centos6 ~]# tar –xzf apr-1.4.6.tar.gz –C /usr/src/ 
  5. [root@centos6 ~]# tar –xzf apr-util-1.5.2.tar.gz –C /usr/src/ 
  6. [root@centos6 ~]# cd /usr/src/apr-1.4.6/ 
  7. [root@centos6 apr-1.4.6]# ./configure  --with-apr=/usr/local/apr/ 
  8. [root@centos6 apr-1.4.6]# make && make install 
  9. [root@centos6 apr-1.4.6]# cd /usr/src/apr-util-1.5.2/ 
  10. [root@centos6 apr-util-1.5.2]# ./configure  --with-apr=/usr/local/apr/ 
  11. [root@centos6 apr-util-1.5.2]# make && make install 
  12. [root@centos6 apr-util-1.5.2]# cd /usr/src/httpd-2.4.4/ 
  13. [root@centos6 httpd-2.4.4]# ./configure –prefix=/usr/local/apache2 –enable-so \ 
  14. > --enable-ssl  --enable-rewrite –with-mpm=worker –with-suexec-bin \ 
  15. > --with-apr=/usr/local/apr/ 
  16. [root@centos6 httpd-2.4.4]# make && make install 

The configure script is used to check the system environment, find the dependent files, and set the installation path. configure has many parameters. You can use./configure -- help to view all the parameters supported by the script.
Common Parameters of configure are described as follows:
Parameter description
-- Prefix specifies the installation home directory of the Apache httpd Program
-- Enable-so: enables modularity and supports DSO Dynamic Object sharing)
-- Enable-ssl supports SSL encryption
-- Enable-rewrite supports address rewriting.
-- With-mpm: Set Apache httpd Working Mode
-- With-suexec-bin supports SUID and SGID
-- With-apr specifies the absolute path of the apr Program
3. Start the service

 
 
  1. [root@centos6 ~]# /usr/local/apache2/bin/apachectl start 
  2. [root@centos6 ~]# netstat -ntulp |grep http 
  3. [root@centos6 ~]# iptables -I INPUT -p tcp -dport 80 -j ACCEPT 

After the installation is complete, Apache will provide the startup script named apachectl, which provides the startup, shutdown, and test functions of Apache httpd. If the configuration file is not modified, start the httpd program with start, the error message "cocould not reliably determine the server's fully qualified domain name" may be returned, indicating that httpd cannot determine the server domain name, you can modify the ServerName of the master configuration file. This prompt can also be ignored. Run the netstat command to check whether httpd has been started successfully.
Access the Web server using a browser on the client and see "IT works !" This indicates that the server can be accessed normally.
The specific apachectl parameters are as follows:
Parameter description
Start starts the httpd program. If the program has been started, an error is returned.
Stop to close the httpd Program
Restart the httpd Program
Graceful starts httpd without interrupting existing connections
Graceful-stop: Disable httpd without interrupting existing connections
Status to view the current status of the httpd Program
Configtest check the httpd main Configuration File Syntax
Configuration File Parsing
The Apache configuration file is located in the/usr/local/apache2/conf directory by default. The main configuration file under this directory is httpd. conf file, and some additional configuration files in the extra directory. In addition, the original directory contains copies of all these configuration files. The Apache HTTP Sever main configuration file consists of commands and containers. The Container starts with <container Name> and ends with </container Name>. The Container commands are generally only partially valid. The following is a specific description of the master configuration file.
SeverRoot command
The ServerRoot command sets the main directory for installing Apache Software. If the source code is used for installation, the default path is/usr/local/apache2.
Listen command
The Listen command sets the IP address and port number of the server listener. By default, port 80 of all IP addresses of the server is monitored. The syntax format is Listen [IP Address:] Port [Protocol]. The IP address and protocol are optional. By default, all IP addresses are monitored and the TCP protocol is used. You can use the Listen command multiple times to enable multiple ports in a configuration file.
LoadModule command
One of the features of Apache HTTP Server is that most of its functions are loaded in modules. However, if you want Apache to dynamically load modules, when compiling Apache, use -- enable-so to compile mod_so to the Apache core module statically. The role of the LoadModule command is to load the module. The syntax format is the LoadModule module File name. The module File is generally located in the modules directory under the directory specified by ServerRoot.
LoadFile command
The LoadFile command function is similar to LoadModule. The difference is that LoadFile can load module files under the modules directory through an absolute path.
ServerAdmin command
When a website fails, you need to provide the customer with an email address that can help solve the problem. The role of the ServerAdmin command is to provide such an email address.
ServerName command
The ServerName command sets the host name and port of the server, which is important for URL address redirection.
DocumentRoot command
This command sets the root directory of the document that the Web service opens to the client, that is, the root path of the client to access the website. The default value is/usr/local/apache2/htdocs.
ErrorLog command
ErrorLog locates server error logs. By default, the relative path is used to specify the "logs/error_log" file under the directory of ServerRoot.
ErrorLogFormat command
Set the format of the error log. Apache HTTP Server has defined many format strings that can be directly referenced.
CustomLog command
This command sets the client access log file name and log format. The default value is "logs/access_log", and the syntax format is CustomLog file name format.
LogFormat command
Description of the user log file format. You can directly use the pre-configured format string of Apache. Generally, we will create an alias for the log format set by the LogFormat command, then, you can use the CustomLog command to call the log format alias.
Include command
The Include command allows Apache to load other configuration files in the main configuration file. The command syntax is relatively simple. After the Inclde command, it can directly follow the path of other additional configuration files.
Options command
Set Options for a specific directory. The syntax format is Options [+ |-] Option [+ |-]. The option can be set to None, indicating that no additional features are enabled, or the following common options can be used.
All: All options except MultiViews are enabled.
ExecCGI: allows the execution of CGI scripts.
FollowSymlinks: allows you to link files in a directory to files or directories outside the directory.
Indexes: If there is no homepage document specified by DirectoryIndex under the directory corresponding to the URL, then Apache
All files in the current directory will be indexed.
Order command
Control the default access status and the Order of Allow and Deny. If Order deny and allow are used, check for rejection first. If Order Deny and Allow are not used, the default access status is allowed. If Order allow and deny are used, check the permission first. If Order allow and deny are not allowed, the request is denied by default.
The following are examples:
Order deny, allow
Deny from all
Check the deny rules first, and then check the permit rules. The default value is allow. Deny From all indicates that all is rejected, so the final result is rejected.
Order Allow, Deny
Allow from All
Check the permit rules first, and then check the deny rules. allow from all indicates that all are allowed, so the final result is allow all.
Order Allow, Deny
Allow from 192.168.0.1
When you check the permit rules, 192.168.0.1 is allowed, and the others are default values. By default, all are rejected, and all are rejected except 192.168.0.1.
Order Allow, Deny
Allow from 192.168.0.1
Deny from All
Check to Allow access to the rule 192.168.0.1 first, but Deny access to all when checking to Deny the rule, and 192.168.0.1 is also included in all. When Allow conflicts with Deny, this case uses the Deny rule to overwrite other rules with the final Order rule. The final result is that all users are denied access.
Order Deny, Allow
Deny from all
Allow from 192.168.0.1
Check the denial rule first, then check the Allow rule. The deny rule is deny all, And the Allow rule is Allow 192.168.0.1. Deny parts that conflict with the Allow rule. In this case, the Allow rule is used, in this case, only access to 192.168.0.1 is allowed, and no other host can access the Apache service.
IfDefine container
Commands encapsulated by the IfDefine container are processed only when the test condition is true when Apache is started. The test condition must be defined by httpd-D When Apache is started. The syntax format is <IfDefine> instruction </IfDefine>. The specific case is as follows:
<IfDefine MemCache>
LoadModule mem_cache_module modules/mod_mem_cache.so
</IfDefine>
<IfDefine UseCache>
LoadModule cache_module modules/mod _ cache. so
</IfDefine>
This configuration allows the Administrator to start Apache using multiple configuration methods. If httpd-DuseCache-DMemCache is used when Apache is started, Apache will load the mod_mem_cache and mod_cache modules, if you do not use-D to specify any parameters, Apache will not load these modules.
IfModule container
The IfModule container can encapsulate the commands that will be processed only when the conditions are met, and determine whether the conditions are met based on whether the specified module is loaded. Syntax format <IfModule [!] Module> command </IfModule>. The specific case is as follows:
<IfModule unixd_module>
User daemon
Group daemon
</IfModule>
The preceding configuration description indicates that User daemon and Group daemon will be processed by Apache only after Apache loads the unixd_module module.
Directory container
The commands in the container are only applicable to specific File System directories, subdirectories, and contents in the Directory. The syntax format is <directory Directory-path> command </Directory>. The path can be ~ Match the regular expression. The specific case is as follows:
<Directory "/usr/local/apche2/htdocs">
Options Indexs FollowSymLinks
</Directory>
<Directory ~ "^/Www/[0-9] {3}">
AllowOverride None
</Directory>
In the above case, the Opions Index followsymparts is only valid for the/usr/local/apache2/htdocs directory, and AllowOverride None is only valid for subdirectories containing three numbers under the/www directory.
<DirectoryMatch>
DirectoryMatch is similar to Directory, but it can be directly matched using a regular expression instead of using it like Directory ~ Symbol.
Files container
This container is similar to a Directory container, but the commands in the Files container are only applied to specific Files. The syntax format is as follows: <Files File Name> command </File>, similar to Directory, can be used ~ Match the regular expression.
FilesMatch container
Only regular expressions are used to match the desired file. The commands in the container are only used to match the specified file. FilesMatch is equivalent ~ The Files of the symbol.
Location container
The commands defined in the Location container are only valid for specific URLs. The syntax format is <Location URL-path | URL> command </Location>. If you need to use a regular expression to match a URL, you can use ~ Symbol.
LocationMathch container
LocationMatch only uses regular expressions to match URLs. It is equivalent ~ The Location where the symbol matches.
VirtualHost container
VM application cases
A vm runs multiple WebSite Services on one Server at the same time. Apache HTTP Server supports domain-based and IP-based VM types. In the Apache configuration file, virtual host commands must be encapsulated using the VirtualHost container. IP-based virtual hosts can locate different website requests based on different IP addresses and port numbers. However, IP-based virtual hosts need independent IP addresses to locate connected websites, currently, IP addresses are scarce resources on the Internet. Therefore, we often prefer domain name-based Virtual Hosts. servers can separate and resolve websites based on the header information of the HTTP client access, the client can use different domain names to access server resources in the same IP address.
When a client request arrives, the server matches the IP address and port number based on the <VirtualHost IP Address: [port number]> parameter. The IP address can use * to match all the local IP addresses of the server. Next we use a case to describe the implementation method based on the domain name virtual host, first we need to enable the main configuration file such as Include conf/extra/httpd-vhosts.conf function, the default behavior comment line, remove the # symbol. The Include directive treats the httpd-vhosts.conf file as part of the configuration file. Below we will only list the parts to be modified in this configuration file.

 
 
  1. [root@centos6 ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 
  2. <VirtualHost *:80> 
  3.     ServerAdmin Jacob_test@gmail.com 
  4.     DocumentRoot "/usr/local/apache2/htdocs/example" 
  5.     ServerName www.example.com 
  6.     ServerAlias web.example.com 
  7. ErrorLog "logs/www.example.com-error_log" 
  8. CustomLog "logs/www.example.com-access_log" common 
  9. </VirtualHost> 
  10. <VirtualHost *:80> 
  11.     ServerAdmin Jacob_test@gmail.com 
  12.     DocumentRoot "/usr/local/apache2/htdocs/test" 
  13.     ServerName www.test.com 
  14.     ErrorLog "logs/test. com-error_log" 
  15.     CustomLog "logs/test.com-access_log" common 
  16. </VirtualHost> 
 
 
  1. [root@centos6 ~]# mkdir -p /usr/local/apache2/htdocs/{example,test} 
  2. [root@centos6 ~]# echo “example.com” > /usr/local/apache2/htdocs/example/index.html
  3. [root@centos6 ~]# echo “test.com” > /usr/local/apache2/htdocs/test/index.html
  4. [root@centos6 ~]# iptables -I INPUT -p tcp -dport 80 -j ACCEPT 
  5. [root@centos6 ~]# /usr/local/apache2/bin/apachectl restart 

After completing the preceding configuration, Apache HTTP Server can implement www.example.com and www.test.com websites based on the same IP address. You can use the client to access these two domain names. If there is no valid DNS server, you can modify the hosts file for domain name resolution. To implement an IP address-based virtual host, you only need to change * after VirtualHost to a fixed IP address. Multiple virtual hosts need to be encapsulated using multiple VirtualHost.
Http://manual.blog.51cto.com/3300438/1186157 ding adventure

This article from the "ding Adventure" blog, please be sure to keep this source http://manual.blog.51cto.com/3300438/1186157

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.