The basics of Apache httpd server

Source: Internet
Author: User

The HTTPD server is a Web server under the Apache product line and is often used in conjunction with CGI scripts such as PHP or Python to provide dynamic Web services to users. HTTPD, as can be seen from the name, it provides primarily HTTP protocol-based Web services. This article introduces the first article of HTTPD server, mainly introduces the installation, operation mode, container configuration, and CGI configuration of the httpd server, so that everyone can build a Web server that can run from zero by reading this article.
Where do I download the httpd? Please visit http://httpd.apache.org/download.cgi, here is the latest HTTPD server source code. If you want to deploy your server in the production environment, or do not want to use the process of some unexpected problems, or download stable release version is better. The current stable version is 2.4.10.
If our operating system is Linux, after downloading the good httpd-2.4.10.tar.gz, we can release all the source code through the tar zxvf httpd-2.4.10.tar.gz command. However, it is not possible to successfully compile the source code to install, because to install the HTTPD server, the first to install several other software, they are apr,apr-util,pcre,zlib. Make sure that Apr,apr-util,pcre and zlib are the latest versions and try not to use the Yum repository version, because if your operating system is not up-to-date, like your operating system is CentOS 5, then when you use Yum to install APR, will be installed to the old version of APR, resulting in 2.4.10 httpd server compilation failed (of course, it is recommended to use the new version of the operating system, otherwise there may be other problems, such as the C compiler version, autoconf version problems, etc., will cause some trouble). So in order to not differentiate with the platform, we are still in trouble, through the official release of the latest version of the source code to compile and install these pre-installed software. Apr and Apr-util on http://apr.apache.org, where you can download to Apr 1.5.1 and Apr-util 1.5.4. Pcre in Http://sourceforge.net/projects/pcre/files/pcre/. Download Pcre 8.36 here. Zlib in Http://zlib.net, after downloading, we have apr-1.5.1.tar.gz,apr-util-1.5.4.tar.gz,pcre-8.36.tar.gz,zlib-1.2.8.tar.gz these three files. I guarantee by personality that they match httpd 2.4.10 100%.
Before installing the httpd, we should install the Apr,apr-util,pcre and zlib first. The specific procedure for installing them is given below, and it is worth noting that APR should be installed before Apr-util because Apr-util relies on Apr. Now, if all your files are under/usr/local.
Apr installation process:

cd/usr/local/apr-1.5.1./CONFIGURE-PREFIX=/USR/LOCAL/APR make && make install

Apr-util installation process:

cd/usr/local/apr-util-1.5.4./configure-prefix=/usr/local/apr-util-with-apr=/usr/local/apr

PCRE installation process:

cd/usr/local/pcre-8.36./configure-prefix=/usr/local/pcre make && make install

Zlib installation process:

cd/usr/local/zlib-1.2.8./configure-prefix=/usr/local/zlib make && make install

After installing the above three pre-installed software, you can install Apache httpd server.
HTTPD installation process:

        cd /usr/local/ httpd-2.4.10        ./configure -prefix=/usr/local/httpd           -with-apr=/usr/local/apr           -with-apr-util=/usr/local/apr-util \           -with-pcre=/usr/local/pcre \           -with-z=/usr/local/zlib -with-mpm=worker           -enable-modules=all -enable-so        make & & make install 

Where-WITH-MPM is a multi-processing module setup, such as set to worker, the system will be run in a multi-process multithreaded model, which is suitable for the PHP-FPM to manage the PHP CGI process, but is not suitable for the PHP content through the Libphp.so module parsing. The-enable-modules represents the launch of all modules within the httpd.-enable-so represents the launch of the dynamic library loading function (this is on by default). The preceding-with-xxx is the installation path of the required predecessor software.
In this way, we have completed the installation of httpd. Before starting httpd, we should make the necessary configuration for httpd. The httpd configuration file is named Httpd.conf and is located in/usr/local/httpd/conf/httpd.conf. The following is a brief introduction to our httpd configuration file.
The httpd configuration file is divided into global and container two parts, the configuration instruction outside the container is the global instruction, which is valid throughout the entire configuration file, and the instruction inside the container is only valid within the container scope. So what is a container? To put it simply, most of them start with the angle brackets <xxx>, and the </xxx> end is the container. such as <directory></directory&gt, such as <Location></Location> and so on. First of all, the general global directives are discussed, and the basic common global directives are serverroot,listen,loadmodule,user,group,servername,documentroot and so on. Because this is the basis, so it is only a simple introduction of the comparative basis of the ^_^.
ServerRoot, the root directory of the server, typically the server's installation directory, the relative path in the configuration file is generally used as a reference.
Listen, the server's listening port, can be set in the form of IP: port, or only the port can be set. If IP is set, the server listens only to the network interface that corresponds to this IP.
LoadModule, load the dynamic library, such as you want to turn on proxy function, then should use LoadModule load proxy.so module.
User, the username used by the server runtime, which is a user of the Linux system.
Group, the set of user groups used by the server runtime, which is the Linux system user group.
ServerName, the server name can be used by the domain name or IP you requested to indicate, do not set this at the start of a warning, but will not error. setting does not matter.
DocumentRoot, the server retrieves the root directory of the content, such as the user in the browser input URL is http://ip/index.html, then where is this index.html? Just under the value set by the DocumentRoot.
These instructions, the general default profile has been set up for you, unless your 80 port is accounted for by a process, the server should be able to start normally. The TCP port can be viewed through the Linux command netstat-ant. If you don't see port 80 in the local address column, congratulations, start the server. Otherwise, change the value of the listen.
If you don't need to build a virtual host, maybe the <Directory> container will be your main set point. Directory is literally a directory that sets the contents of the server to retrieve content. If the value of your documentroot is/usr/local/httpd/htdocs. Then when the user's URI is index.html, the server will look for the index.html file under/usr/local/httpd/htdocs, and when the URI is test/index.php, the server will be in/usr/local/httpd/ Htdocs/test look for the index.php file. So if I want to customize some settings in/usr/local/httpd/htdocs, like to display a list of files in a directory, you should take advantage of the <Directory> container. Here is a section of the Directory Setup code:

<directory "/usr/local/httpd/htdocs" > Options Indexes allowoverride None Require all GR Anted </Directory>


This code is very simple, mainly to achieve three purposes. One is to allow the directory list to be displayed, and the second is to prohibit the. htaccess file overwrite configuration, which allows all users to access the directory, which means that the content can be returned to the user as long as the URI is mapped to this directory. Options are some of the settings for a directory, such as allowing display lists, allowing soft links within the directory, and so on. AllowOverride is whether to allow other profiles to overwrite this configuration. Require is authorized.

We use the/usr/local/httpd/bin/httpd-k Start command to start the server. If there is no echo, the server starts successfully. Further can be used PS aux | grep httpd to determine the following, if there are many/usr/local/httpd/bin/httpd-k start processes, it indicates that the server started successfully. Using a browser to access the server, such as server IP 192.168.1.6, enter http://192.168.1.6/in the browser, if the page displays "It works! "Indicates that the server is OK. At this point, the server is installed to this end, the following describes the operating mode of the server.
The operating mode of the HTTPD server is mainly divided into prefork and worker, which belong to the multi-processing module MPM, which is set by the-WITH-MPM parameter at./configure. The prefork is a non-threaded, pre-derived, multi-processing module, and the worker is thread-type. This means that Prefork uses processes to process requests, and workers can use threads to process requests. They each have advantages and disadvantages, for non-thread-safe scripting systems, suitable for use with prefork modules such as PHP-CLI. For a thread-safe scripting system, choosing a worker module may be a better choice if your machine is multicore and configured high enough.
    prefork uses a separate control process that is responsible for generating child processes that are used to listen for requests and give answers. Apache always tries to keep some spare sub-processes available to meet the incoming requests so that the client does not have to wait for the child process to be generated before the service is available. The configuration of the MPM module can be set in the global section of the configuration file. Of course, in general, when setting up MPM, we will add <IfModule></IfModule> to determine if the mode is being applied to the server. The following is a prefork configuration code:

    <IfModule mpm_prefork_module>  #这个判断可以不加 Unless you're pretty sure you've started the Prefork mode          startsservers    5    # Number of processes created at server startup         MinSpareServers    5      #最小空闲进程数         MaxSpareServers     10     #最大空闲进程数         maxrequestworkers     250     #最高并发量          maxconnectionsperchild    0     #单个进程能处理的连接数, if set to a positive integer value, after the number of connections is exceeded,                                            the child process will be killed. When set to 0 o'clock, the tableShow Unlimited                                    </ Ifmodule>

    worker is a multithreaded multi-process server that can handle massive requests if your server is resistant to live. The following is the configuration code for a worker:

    <IfModule mpm_worker_module>         StartServers    3                  #同prefork下的StartServers指令          minsparethreads    75              #最小空闲线程数         MaxSpareThreads    250              #最大空闲线程数          ThreadsPerChild    25              #每个子进程的线程数         MaxRequestWorkers     400         #同prefork下的MaxRequestWorkers指令          maxconnectionsperchild    0     #同prefork下的MaxConnectionsPerChild指令      </IfModule>


Finally, let's explore how the next httpd interacts with CGI scripts to implement dynamic content. Take the normal bash script as an example. We create a bash script as follows:

#!/bin/bash Echo content-type:text/html Echo Hello, world.

The script file is named hello.cgi.

We put this script in the/usr/local/httpd/cgi-bin directory to allow users to access http://192.168.1.6/cgi-bin/hello.cgi on the browser, and to display "Hello, world." On the Web page. So how do you set it up? To open the httpd.conf file, refer to the following configuration code:

    <ifmodule alias_module>         ScriptAlias /cgi-bin/  "/usr/local/httpd/cgi-bin/"      </IfModule>    <IfModule mime_module>         AddHandler cgi-script .cgi    </IfModule>     <Directory  "/usr/local/httpd/cgi-bin" >         Options ExecCGI        Require all granted     </directory> 


The meanings of the above instructions are explained in turn below. You can see that the commands are included in Alias and MIME module judgments, so first find the alias and mime two modules in LoadModule and uncomment them, and confirm that there are two modules in your/usr/local/httpd/modules. , which belongs to the core module of httpd). The meaning of Scriptalias is to map the path of Uri/cgi-bin/to the physical directory/usr/local/httpd/cgi-bin, which is the directory where you put the CGI script. The AddHandler command represents the addition of a CGI script processor that enables HTTPD to handle CGI scripts, a CGI script handler named Cgi-script that handles files ending with a. cgi suffix. The EXECCGI option under options indicates that CGI scripts can be executed in this directory. After the configuration, restart the server, and then you can enjoy dynamic content access.
This article only introduced the HTTPD Server 9 Cattle 1 Mao, so that everyone has a basic understanding of the HTTPD server. Of course, the HTTPD server also has many features, such as logging, rewriting, authentication, authorization, combined with the PHP module, and the use of the PHP-FPM manager. It is an important part of the Web services architecture and contributes a lot to the Web services infrastructure.

This article is from the architect's path blog, so be sure to keep this source http://wangweiak47.blog.51cto.com/2337362/1606578

The basics of Apache httpd server

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.