Apache learning notes

Source: Internet
Author: User
Tags server memory

Copyright Disclaimer: You can reprint the document at will. During reprinting, you must mark the original source and author information of the article as hyperlinks and this statement.
Http://www.chedong.com/tech/apache_install.html

Keywords: apache install php resin mod_gzip mod_expire webalizer cronolog

Summary:

Apache is a WEB server with a long history and powerful functions, but its rich functions are often unknown to new users. I personally feel that the Apache design fully embodies the advantages of modular design by installing in the dynamic module loading (DSO) mode, any sub-application module can be flexibly configured by simple modifications in the configuration file. The installation process can start with a simple static html service and be learned and used by a module or module. From pure HTML static services (core) to complex dynamic page services (core + php, core + resin, core + php + mod_gzip, core + resin + mod_expire ).

This article describes the WEB service planning, HTTPD installation/application module configuration, upgrade/maintenance, and other processes from the perspective of simplified installation ==> Performance Tuning ==> convenient maintenance. Independent upgrades of application modules such as Apache, PHP, and Resin are completely independent of each other.

WEB application capacity planning: WEB service planning and some simple estimation formulas based on hardware configuration and WEB application characteristics;
Apache installation process: common and simplified Installation Options of apache facilitate modular configuration of future applications;
Modify HARD_SERVER_LIMIT:
Vi/path/to/apache_src/src/include/httpd. h
# Define HARD_SERVER_LIMIT 2560 <= Add "0" to the original HARD_SERVER_LIMIT 256"
Apache Compilation:
./Configure -- prefix =/home/apache -- enable-shared = max -- enable-module = most
Installation of optional application modules/tools: php resin mod_gzip mod_expire and cooperation between various modules;
Mod_php installation:./configure -- with-apxs =/home/apache/bin/apxs -- enable-track-vars -- with-mysql
Mod_resin installation:./configure -- with-apxs =/home/apache/bin/apxs
Mod_gzip installation: Modify the apxs path in Makefile: Then make install
Tool: log tracking tool cronolog installation: http://www.cronolog.org
Upgrade/maintenance: see how the General and modular installation process simplifies routine upgrade/maintenance work;
Follow these steps: the roles and responsibilities of system administrators and application administrators can be clearly separated and independent from each other.
System installation: the role of the system administrator is to install a DSO-mode Apache, and then use COLON,
Application installation: The application Administrator is responsible for the modules required by the specific application, such as PHP Resin, and configures httpd. conf.
System Upgrade: System Administrator: Upgrade the operating system/Upgrade Apache
Application upgrade: Application administrator: Upgrade the application module and PHP Resin.
WEB application capacity planning

Apache is mainly a memory-consuming service application. I personally sum up the following formula:
Apache_max_process_with_good_perfermance <(total_hardware_memory/apache_memory_per_process) * 2
Apache_max_process = apache_max_process_with_good_perfermance * 1.5

Why is there an apache_max_process_with_good_perfermance and apache_max_process? The reason is that at a low load, the system can use more memory for the file system cache to further improve the response speed of a single request. Under high load, the system's single request response speed will be much slower, and beyond the apache_max_process, the system will cause a sharp reduction in system efficiency due to the use of hard disks for Virtual Memory swap space. In addition, the same service: apache_max_process of 2 GB memory machines is generally set to 1.7 times of 1 GB memory, because Apache itself will cause performance degradation due to excessive processes.

Example 1:
An apache + mod_php server: A apache process generally requires 4 MB of memory
Therefore, on a machine with 1 GB memory: apache_max_process_with_good_perfermance <(1g/4 m) * 2 = 500
Apache_max_process = 500*1.5 = 750
Therefore, plan your application to make the service run below 500 processes as much as possible to maintain high efficiency, and set the Apache soft limit to 800.

Example 2:
An apache + mod_resin server: an apache process generally requires 2 MB of memory.
On a machine with 2 GB memory:
Apache_max_process_with_good_perfermance <(2g/2 m) * 2 = 2000
Apache_max_process = 2000*1.5 = 3000

The above estimates are based on the small file service (a request is generally smaller than 20 KB ). File Download websites may also be affected by other factors, such as bandwidth.

Apache installation process

Modify the maximum number of servers HARD_SERVER_LIMIT:
The default maximum number of processes in Apache source code is 256. You need to modify apache_1.3.xx/src/include/httpd. h.
# Ifndef HARD_SERVER_LIMIT
# Ifdef WIN32
# Define HARD_SERVER_LIMIT 1024
# Elif defined (NETWARE)
# Define HARD_SERVER_LIMIT 2048
# Else
# Define HARD_SERVER_LIMIT 2560 <= Add "0" to the original HARD_SERVER_LIMIT 256"
# Endif
# Endif

Explanation:
The maximum number of Apache users by default is 256: This configuration is a very good default for the era of server memory or about MB, but with the sharp decline in memory costs, the server memory configuration for large sites is generally more than an order of magnitude higher than that at the time. Therefore, the hard limit of 256 processes is too wasteful for a machine with 1 GB of memory, and the soft upper limit of Apache max_client is limited by HARD_SERVER_LIMIT. Therefore, if the memory of the WEB server is larger than 256 MB, you should increase Apache's HARD_SERVER_LIMIT. According to my personal experience: 2560 can already meet the capacity planning of most servers with less than 2 GB of memory (for details about Apache soft-cap planning, refer to later ).

Apache Compilation: The following general compilation options can be used to install any modules in the future.
./Configure -- prefix =/another_driver/apache/-- enable-shared = max -- enable-module = most
For example:
./Configure -- prefix =/home/apache/-- enable-shared = max -- enable-module = most

Explanation:
-- Prefix =/another_driver/apache/: We recommend that you install the apache service on another driver to ensure that the hard disk is usually the device with the lowest service life of the system. Therefore: separating service data from the system not only improves data access speed, but also facilitates system upgrades, application backup, and recovery.

-- Shared-module = max: loading sub-modules in the dynamic loading mode reduces the performance by 5%, but it is more convenient than the configuration. For example, the module upgrade is convenient, reduces system upgrade risks and standardizes the installation process.

-- Enable-module = most: most can be used to compile unused modules. For example, the mod_expire mentioned later is not in the default common modules of apache.

If you do not want to build so, you can also do this:
./Configure \
"-- With-layout = Apache "\
"-- Prefix =/path/to/apache "\
"-- Disable-module = access "\
"-- Disable-module = actions "\
"-- Disable-module = autoindex "\
"-- Disable-module = env "\
"-- Disable-module = imap "\
"-- Disable-module = negotiation "\
"-- Disable-module = setenvif "\
"-- Disable-module = status "\
"-- Disable-module = userdir "\
"-- Disable-module = cgi "\
"-- Disable-module = include "\
"-- Disable-module = auth "\
"-- Disable-module = asis"

However, the results will show that such compilation can only slightly improve the service performance (about 5%), but it will lose the flexibility of system upgrade and module upgrade in the future, both the module and Apache itself must be combined with the SOURCE of Apache and PHP for re-compilation.

Apache's default configuration file is generally relatively large: You can use the method of removing comments to streamline it: and then enter the specific cultivation process to allow you to quickly customize what you need.
Grep-v "#" httpd. conf. default> httpd. conf

The following general projects need to be modified:

# Service port. The default value is 8080. We recommend that you adjust the configuration of Apache before changing the service port to the official service port.
Port 8080 => 80

# Server name: No by default
ServerName name.example.com

# Maximum number of service processes: Set Based on Service Capacity Forecast
MaxClients 256 => 800

# Number of service processes after the service is started by default: after the service is stable, you can set the number of httpd processes based on the average load.
StartServers 5 => 200

Do not modify:
Previously, it was suggested to modify:
MinSpareServers 5 = & gt; 100
MaxSpareServers 10 = & gt; 200

However, in my experience, the default value is already very optimized, and it is better for Apache to adjust the number of sub-shared processes by itself.

Special modification:
On solaris or some applications that are prone to memory leakage:
MaxRequestsPerChild 0 => 3000

Installation and configuration of application modules and tools:

Because the module dynamic loading mode is used, you can easily customize Apache into what you need through simple configuration adjustment: it is best to clear all the modules that are not commonly used (whether in security or efficiency ).
For example, for Static Page servers: no other sub-modules are loaded. For PHP applications, the PHP module is added. For JAVA applications, the Resin module is loaded. In addition, the plug-in and plug-in of various modules are very simple. In this way, you can simply comment out unnecessary modules during the debugging process without re-compiling.

Generally, the following modules are not required:
# LoadModule env_module libexec/mod_env.so
# LoadModule negotiation_module libexec/mod_negotiation.so
# LoadModule status_module libexec/mod_status.so
# Server side include is out of date
# LoadModule includes_module libexec/mod_include.so
# You do not need to list all files in the directory without the default index file
# LoadModule autoindex_module libexec/mod_autoindex.so
# Do not use CGI as much as possible: always the most critical issue to Apache Security
# LoadModule cgi_module libexec/mod_cgi.so
# LoadModule asis_module libexec/mod_asis.so
# LoadModule imap_module libexec/mod_imap.so
# LoadModule action_module libexec/mod_actions.so
# Access speed can be greatly improved without Security Authentication
# LoadModule access_module libexec/mod_access.so
# LoadModule auth_module libexec/mod_auth.so
# LoadModule setenvif_module libexec/mod_setenvif.so

It is best to retain the following:
# Used to customize the log format
LoadModule config_log_module libexec/mod_log_config.so
# Used to increase the association of file applications
LoadModule mime_module libexec/mod_mime.so
# Used for default index files: index. php, etc.
LoadModule dir_module libexec/mod_dir.so

Available and optional include:
# For example, you need ~ /Username/to debug php
LoadModule userdir_module libexec/mod_userdir.so
# For example, You need to redirect the previous URL or use CGI script-alias
LoadModule alias_module libexec/mod_alias.so

Common modules:
The most common ones are the front-end of php and JAVA application servers. In addition, in terms of performance, mod_gzip can reduce the traffic by about 40% and reduce the load of machines for transmission, mod_expires can reduce the number of duplicate requests by about 10%, so that duplicate users CACHE the specified page request results locally and do not send requests to the server at all.

We recommend that you put the configurations of all modules in the configuration of the corresponding MODULE: <IfModule some_module.c> some_module config </IfModule>

PHP installation:
/Path/to/php_src/configure -- with-apxs =/path/to/apache/bin/apxs -- with-other-modules-you-need
Configuration to be modified:
AddType application/x-httpd-php. php. php3. any_file_in_php

Resin installation settings:
/Path/to/resin/src/configure -- with-apxs =/path/to/apache/bin/apxs

The specific resin settings are stored in another file, such as/home/resin/conf/resin. conf.
<IfModule mod_caucho.c>
CauchoConfigFile/path/to/apache/conf/resin. conf
</IfModule>

Installation configuration of mod_expires:
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/gif "access plus 1 month"
ExpiresByType text/css "now plus 1 month"
ExpiresDefault "now plus 1 day"
</IfModule>

Note:
All. GIF files expire after 1 month
All files expire after 1 day by default.

Mod_gzip Installation
Http://www.chedong.com/tech/compress.html

Log rotation: Installation and setting of cronolog

Cronolog can neatly store logs by day
By default, It is compiled and installed under/usr/local/bin/. You only need to change the configuration:

CustomLog "|/usr/local/sbin/cronolog/home/apache/logs/% w/access_log" combined

Logs are truncated by day and stored in the directory named by week. For example, log/1 is Monday, log/5 is Friday, and log/0 is Sunday.

Use gzip to compress daily logs:
30 4 ***/usr/bin/gzip-f/home/apache/logs/'date-d yesterday + % W'/access_log

Regular log deletion:
30 5 ***/usr/bin/find/home/apache/logs/-name access_log.gz-mtime + 3 | xargs-r/bin/rm-f

Upgrade and maintenance:

Because Apache is installed in the dynamic module loading mode (DSO mode), the HTTPD core service of Apache and the application module are flexible. We recommend that you set the configurations of all the independent modules
<IfModule mod_name>
CONFIGURATIONS ..
</IfModule>
In this way, it is very easy to adjust the function by shielding a module: for example:
# AddModule mod_gzip.c
Mod_gzip is blocked, while other modules do not have any influence.

Installation and maintenance process:

System installation: The System Administrator is responsible for installing the system and installing Apache in DSO mode, and then COLON.
Application installation: The application Administrator is responsible for the modules required by the specific application and sets HTTPD.
System Upgrade: System Administrator: upgrade the system/Upgrade Apache
Application upgrade: Application administrator: Upgrade application module: php caucho, etc.
System Backup/recovery: If Apache is not on the default system disk, you only need to back up the Apache directory. In case of hardware problems in the system partition, use the pre-prepared system COLON, you can recover the physical disk where Apache is located.
System Administrator: the simplest way to install Apache OS + Apache (httpd core only)
APP administrator: app module custom Static Page Service
Core
PHP dynamic page
Core + so
+ Php
JAVA applications
Core + so
+ Caucho
+ Ssl
Example: www.example.com
Image.example.com
Bbs.example.com mall.example.com

Example: An Independent upgrade of the Apache and PHP modules.

If Apache is installed as follows:
./Configure -- prefix =/home/apache -- enable-shared = max -- enable-module = most
PHP is installed as follows:
./Configure -- with-apxs =/home/apache/bin/apxs -- enable-track-vars -- with-mysql

When You Upgrade Apache separately in the future, it will still be:
./Configure -- prefix =/home/apache -- enable-shared = max -- enable-module = most
Make
Su
#/Home/apache/bin/apachectl stop
# Make install

When You Upgrade php separately, it is still:
./Configure -- with-apxs =/home/apache/bin/apxs -- enable-track-vars -- with-mysql
Make
Su
#/Home/apache/bin/apachectl stop
# Make install
#/Home/apache/bin/apachectl start

Reverse Proxy-based WEB acceleration:
Both squid and mod_proxy can achieve reverse proxy acceleration. The cache-based proxy acceleration is faster than the original WEB service.

TIPS:

After Apache is installed, two files that are not available but useful in the default root directory are:

Favicon. ico: favicon. ico is a 16x16 Site icon file. If the browser finds this file, it will replace the web page icon of the browser in the address bar. Mainstream browsers such as IE6 and MOZILLA support this function.
Example: http://www.chedong.com/favicon.ico

Robots.txt: Used to tell the search engine crawler websites that pages can be indexed, but those pages cannot.
For details, see: http://www.robotstxt.org/wc/robots.html
Reference:

Apache Project
Http://httpd.apache.org

PHP
Http://www.php.net

Resin
Http://www.caucho.com

Mod_gzip
Http://sourceforge.net/projects/mod-gzip/

Cronolog
Http://www.cronolog.org

Mod_expires
Http://httpd.apache.org/docs/mod/mod_expires.html

CMS Design for search engines:
Http://www.chedong.com/tech/cms.html

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.