Basic tutorial on Apache Httpd server

Source: Internet
Author: User

Basic tutorial on Apache Httpd server

Httpd server is a web server under the Apache product line. It is often used together with CGI scripts such as PHP or Python to provide dynamic Web services for users. Httpd provides http-based Web Services. This is the first article about the Httpd server. It mainly introduces the installation, running mode, container configuration, and CGI configuration of the Httpd server, the purpose is to enable you to build a running Web server from scratch by reading this article.

Where can I download Httpd? Visit http://httpd.apache.org/download.cgi. here, the source code of the latest httpdserver is available. If you want to deploy your server in the production environment or do not want unexpected problems during use, it is better to download Stable Release. The current stable version is 2.4.10.

If our operating system is linux, download httpd-2.4.10.tar.gz, we can use the tar zxvf httpd-2.4.10.tar.gz command to release all the source code. However, you may not be able to successfully compile and install the source code. to install the httpd server, you must first install several other software, such as APR, APR-Util, PCRE, and ZLIB. Make sure that APR, APR-Util, PCRE, and ZLIB are all the latest versions. Do not use the yum library version if your operating system is not the latest version, for example, if your operating system is CentOS 5, when you use yum to install APR, it will be installed to the old version of APR, as a result, the httpd server of 2.4.10 fails to compile (of course, the new version of the operating system is recommended; otherwise, other problems may occur, such as the C compiler version and autoconf version, ). Therefore, in order not to distinguish from the platform, we still have trouble compiling and installing these front-end software through the source code of the latest official version. 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 is available at http://sourceforge.net/projects/pcre/files/pcre /. Download PCRE 8.36 here. Zlib in the http://zlib.net for the three files. I personally guarantee that they match httpd 2.4.10 in 100%.

Before installing httpd, install apr, apr-util, pcre, and zlib. The following describes how to install them. It is worth noting that apr should be installed before apr-util, because apr-util depends on apr. Assume that 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 preceding three front-end software, you can install the 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

Here,-with-mpm is the setting of the multi-path processing module. For example, if it is set to worker, the system will run the multi-process and multi-thread model. This setting is suitable for managing php cgi processes with php-fpm, but it is not suitable for using libphp. the so module parses php content. -Enable-modules indicates that all modules in httpd are started.-enable-so indicates that the dynamic Library Loading Function is started (this function is enabled by default ). The preceding-with-xxx is the installation path of the required prefix software.
In this way, we have completed the installation of httpd. We should configure httpd before starting httpd. The configuration file name of httpd is httpd. conf, which is located at/usr/local/httpd/conf/httpd. conf. The following is a brief introduction to our httpd configuration file.
The httpd configuration file is divided into two parts: Global and container. The configuration commands outside the container are global commands, which are valid throughout the configuration file, it is valid only within the container range. So what is a container? To put it simply, most containers start with <xxx> and end with </xxx>. Such as <Directory> </Directory> and <Location> </Location>. First, we will discuss common global commands. basic common global Commands include ServerRoot, Listen, LoadModule, User, Group, ServerName, and DocumentRoot. Because this article is the foundation, we will briefly introduce the basic ^_^.
ServerRoot is the root directory of the server. It is generally the installation directory of the server. The relative path in the configuration file is generally used as a reference.
Listen: the listening port of the server. It can be set in the form of IP: port or only the port. If an IP address is set, the server only listens to the network interface corresponding to the IP address.
LoadModule: load the dynamic library. For example, if you want to enable the proxy function, you should use LoadModule to load the proxy. so module.
User: the User name used when the server is running. This User is a Linux User.
Group is the user Group used when the server is running. This User Group is a Linux user Group.
ServerName: the server name can be expressed by the domain name or IP address you have applied for. If this parameter is not set, a warning will be triggered at startup, but no error will be reported. It doesn't matter if it is set or not.
DocumentRoot: the root directory of the content retrieved by the server. For example, if the url entered by the user in the browser is http: // ip/index.html, where is this index.html? In the value set by DocumentRoot.
These commands are usually set in the default configuration file for you. The server should be able to start normally unless port 80 is occupied by a process. You can run the linux Command netstat-ant to view the tcp port status. If port 80 is occupied in the Local Address column, congratulations! Start the server. Otherwise, change the value of Listen.
If you do not need to create a virtual host, the <Directory> container may be your main setting point. The literal meaning of Directory is to set the Directory for the server to retrieve the content. If your DocumentRoot value is/usr/local/httpd/htdocs. When the user's uriis index.html, the server will find the index.html file under/usr/local/httpd/htdocs, when the uri is test/index. in php, the server will find the index under/usr/local/httpd/htdocs/test. PHP file. If you want to customize some settings in/usr/local/httpd/htdocs, for example, to display the file list in the Directory, you should use the <Directory> container. The following is a directory setting code:

<Directory "/usr/local/httpd/htdocs">
Options Indexes
AllowOverride None
Require all granted
</Directory>


This code is very simple and mainly serves three purposes. One is to allow the display of the directory list, and the other is prohibited. the htaccess file overwrites the configuration. The third is to allow all users to access the directory. That is to say, as long as the uri is mapped to this directory, the content can be returned to the user. Options is some of the Directory settings Options, such as allowing the list to be displayed and allowing soft links in the directory. AllowOverride indicates whether other configuration files are allowed to overwrite the configuration. Require is authorization.

Run the/usr/local/httpd/bin/httpd-k start command to start the server. If no echo is displayed, the server is successfully started. You can use ps aux | grep httpd to determine the following. If there are many/usr/local/httpd/bin/httpd-k start processes, the server is successfully started. Use a browser to access the server. If the IP address of the server is 192.168.1.6, enter http: // 192.168.1.6/in the browser. If "It works!" is displayed on the page !", Indicates that everything on the server is OK. So far, the installation of the server has ended. The following describes the running mode of the following servers.
The running Modes of Httpd servers are mainly divided into prefork and worker, which belong to the multi-channel processing module MPM and are set by the-with-mpm parameter in./configure. Prefork is a non-thread-type, pre-derived multi-channel processing module, while worker is a thread-type. That is to say, prefork uses a process to process requests, while worker can use a thread to process requests. They have their own advantages and disadvantages. For non-thread-safe script processing systems, prefork modules, such as php-cli, are suitable for use. For a thread-safe script processing system, if your machine is multi-core and configured high enough, selecting the worker module may be a better choice.
Prefork uses a separate control process to generate sub-processes, which are used to listen for requests and give responses. Apache always tries to keep some standby sub-processes to meet the upcoming requests, so that the client does not have to wait for the sub-process to generate before getting the service. You can configure the mpm module in the global part of the configuration file. Of course, we usually add <IfModule> </IfModule> when setting mpm to determine whether the mode is used on the server. The following is a prefork configuration code:
<IfModule mpm_prefork_module> # This judgment can be left blank unless you are sure that you have started the prefork mode.
StartsServers 5 # Number of processes created at server startup
MinSpareServers 5 # minimum number of idle Processes
MaxSpareServers 10 # maximum number of idle Processes
MaxRequestWorkers 250 # maximum concurrency
MaxConnectionsPerChild 0 # Number of connections that can be processed by a single process. If it is set to a positive integer, after the number of connections is exceeded,
The sub-process will be killed. If it is set to 0, it indicates no limit.
</IfModule>

Worker is a multi-threaded, multi-process server that can process massive requests, provided that your server can resist. The following is a worker configuration code:

<IfModule mpm_worker_module>
StartServers 3 # StartServers commands under the same prefork
MinSpareThreads 75 # minimum number of Idle threads
MaxSpareThreads 250 # maximum number of Idle threads
ThreadsPerChild 25 # Number of threads for each sub-process
MaxRequestWorkers 400 # MaxRequestWorkers commands under the same prefork
MaxConnectionsPerChild 0 # MaxConnectionsPerChild command under the same prefork
</IfModule>


Finally, we will discuss how httpd interacts with CGI scripts to implement dynamic content. Take a common bash script as an example. 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 in the browser. cgi to display "Hello, World. ". So how should we set it? Open the httpd. conf file and 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 following explains the meaning of the preceding commands in sequence. You can see that the commands are included in the judgment of the alias and mime modules. Therefore, find the alias and mime modules in the LoadModule and uncomment them, at the same time, make sure that your/usr/local/httpd/modules has these two modules (both modules should be available, which are the core modules of httpd ). ScriptAlias maps the uri as/cgi-bin/to the physical directory/usr/local/httpd/cgi-bin, that is, the directory where you put the cgi script. The AddHandler command adds a cgi script processor so that httpd can process cgi scripts. The cgi script processor is named cgi-script, which processes files ending with the. cgi suffix. ExecCGI option in Options indicates that the CGI script can be executed in this directory. Restart the server after the configuration, and then you can happily access the dynamic content.
This article only introduces the httpd server's 9th ox, which gives you a basic understanding of the httpd server. Of course, the httpd server also has many functions, such as logging, rewriting, authentication, authorization, combined use with the php module, and combined use with the php-fpm manager. It is an important part of the web service architecture and contributes a lot to the web service infrastructure.

CentOS 6.5 compile and install httpd-2.4.7

Comparison of working models in httpd

Source code compilation and installation of httpd2.4 and virtual host

Compile and install the latest httpd-2.4

Implementation of httpd2.4 basic functions...

This article permanently updates the link address:

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.