Apache Learning Notes _ Server

Source: Internet
Author: User
Tags install php vars server memory
Copyright NOTICE: You can reprint, reprint, please be sure to hyperlink form to indicate the original source of the article and author information and this statement
Http://www.chedong.com/tech/apache_install.html

Tags: Apache install php resin mod_gzip mod_expire webalizer Cronolog

Content Summary:

Apache is a long and powerful Web server, but its rich features don't always know where to start for a novice. I personally feel that Apache design fully embodies the advantages of modular design, through the dynamic module loading (DSO) mode of installation, any child application module can be modified by the simple configuration file building block-type flexible configuration. The installation process can start with a simple static HTML service, a module for learning to use. From simple HTML static services (core) to complex dynamic page services (core + PHP, core + resin, core + PHP + mod_gzip, core + resin + mod_expire).


This paper introduces the planning of Web services, httpd installation/Application module configuration, upgrade/maintenance, and so on, from the angle of simplifying the installation ==> performance Tuning ==> maintenance convenience. Let the Apache and Php,resin application modules such as independent upgrades, completely unrelated.

Web application Capacity Planning: The planning of Web services and some simple estimating formulae based on the characteristics of hardware configuration and Web application;
Apache installation process: Apache's General simplified installation option, convenient for later application of the modular configuration;
Modify Hard_server_limit:
Vi/path/to/apache_src/src/include/httpd.h
#define HARD_SERVER_LIMIT 2560 <=== Add a "0" to the back of the original Hard_server_limit 256.
Apache compilation:
./configure--prefix=/home/apache--enable-shared=max--enable-module=most
Optional Application module/tool Installation: PHP resin mod_gzip mod_expire and the coordination between the 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 make install
Tools: Log Wheel tool Cronolog installation: http://www.cronolog.org
Upgrade/maintenance: See how the common and modular installation process simplifies the daily upgrade/maintenance effort;
According to the above method: the responsibilities of the system administrator and the application administrator can be clearly separated and independent.
System installation: The responsibility of the system administrator is to install a DSO mode of Apache, and then Colon can,
Application installation: The application Administrator is responsible for the specific application of the required modules, such as PHP resin, and set up httpd.conf in the relevant configuration.
System Upgrade: System administrator: Upgrade OS/upgrade Apache
Application Upgrade: Application Manager: Upgrade application module, PHP resin, etc.
Capacity Planning for Web applications

Apache is primarily a memory-consuming service application, my personal summary of the empirical 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 would there be a apache_max_process_with_good_perfermance and apache_max_process? The reason is that the system can use more memory for file system caching at low load, thus further increasing the response speed of individual requests. Under high load, the system's single request response speed will be slow, but more than apache_max_process, the system will reduce the efficiency of the system because it starts using the hard disk to do the virtual memory swap space. In addition, the same service: The apache_max_process of 2G memory machines is typically set to only 1.7 times times that of 1G memory, because Apache itself can degrade performance because of too much process.

Example 1:
An Apache + mod_php server: An Apache process typically requires 4 m of memory
So on a 1G memory machine: Apache_max_process_with_good_perfermance < (1g/4m) * 2 = 500
apache_max_process = 500 * 1.5 = 750
So plan your application so that the service runs as far as 500 processes below to maintain a higher efficiency and set Apache's soft limit at 800.

Example 2:
An Apache + mod_resin server: An Apache process typically requires 2 m of memory
On a 2G memory machine:
Apache_max_process_with_good_perfermance < (2g/2m) * 2 = 2000
apache_max_process = 2000 * 1.5 = 3000

The above estimates are based on small file services (one request is typically below 20k). For file download type sites, you may also be affected by other factors such as bandwidth.

Apache installation process

The hard upper limit of the number of servers Hard_server_limit modification:
The default maximum number of processes in Apache source code is 256, and needs to be modified 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 a "0" to the back of the original Hard_server_limit 256.
#endif
#endif

Explain:
The default maximum number of users in Apache is 256: This is a very good default setting for server memory or about 256M, but as the cost of memory drops dramatically, the server memory configuration of a large site now is generally a higher order of magnitude than it was then. So the hard limit of 256 processes is too wasteful for a 1G ram machine, and Apache's soft upper limit max_client is limited to hard_server_limit, so if the Web server has more than 256M memory, you should increase the Apache Hard_server_limit. Based on personal experience: 2560 has been able to meet the capacity planning of most servers less than 2G of memory (Apache's soft limit planning please look back).

Apache compilation: The following generic compilation options are available to meet any future installation of the module
./configure--prefix=/another_driver/apache/--enable-shared=max--enable-module=most
Like what:
./configure--prefix=/home/apache/--enable-shared=max--enable-module=most


Explain:
--prefix=/another_driver/apache/: It is recommended that the Apache service be installed on another drive device, so that the hard disk is often the lowest-life device for the system, so that a complete separation of service data and systems will not only improve the speed of data access, More importantly, greatly facilitates system upgrades, applying backup and recovery processes.

--shared-module=max: Loading a child module with dynamic loading will result in 5% performance degradation, but it is nothing compared to the convenience of configuration: For example, the module upgrades conveniently, the system upgrades the risk to reduce, the installation process standardization and so on

--enable-module=most: Most can be used to compile some of the less commonly used modules, such as the Mod_expire described later, is not in the default common Apache module

If you don't want to build so, you can 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"

But the result will be found that such a compilation of service performance can only slightly improve (5% or so), but lost the future system upgrades and module upgrade flexibility, whether the module or Apache itself upgrade must be the Apache and PHP source added together recompile.

The default configuration file for Apache is generally larger: you can use the method of removing annotations to streamline: And then go into the specific implant process to get you to customize what you need more quickly.
Grep-v "#" Httpd.conf.default >httpd.conf

There are several common items that need to be modified:

#服务端口, the default is 8080, it is recommended to adjust the entire Apache configuration and then change the service port to the official service port
Port 8080 => 80

#服务器名: No default
ServerName name.example.com

#最大服务进程数: Based on service capacity forecast settings
MaxClients 256 => 800

#缺省启动服务后的服务进程数: When the service is more stable, the httpd number can be set according to the average load
Startservers 5 => 200

Do not modify:
Previous suggestions were made to modify:
Minspareservers 5 => 100
Maxspareservers => 200

But from my experience, the defaults are already very optimized, and it's nice to have Apache adjust the number of child-sharing processes themselves.

Special Modifications:
On Solaris or some applications that are more vulnerable to memory leaks:
Maxrequestsperchild 0 =>3000

Installation configuration for application modules and tools:

Because of the use of module dynamic loading mode, so you can easily through simple configuration adjustment to make the Apache you need: It is best to eliminate the most common modules (whether in safety or efficiency).
For example: for static page Server: On what other sub modules are not loaded, PHP application plus PHP module, for Java application on the resin module load. and the various modules of the plug is very simple, so the debugging process can be simply by commenting out the unwanted modules, without recompiling.

Generally, modules that are not required include:
#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 obsolete.
#LoadModule Includes_module libexec/mod_include.so
#不需要将没有缺省index文件的目录下所有文件列出
#LoadModule Autoindex_module libexec/mod_autoindex.so
#尽量不使用CGI: It's always been the best place for 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
#不使用安全认证可以大大提高访问速度
#LoadModule Access_module libexec/mod_access.so
#LoadModule Auth_module libexec/mod_auth.so
#LoadModule Setenvif_module libexec/mod_setenvif.so

The best reservations are:
#用于定制log格式
LoadModule Config_log_module libexec/mod_log_config.so
#用于增加文件应用的关联
LoadModule Mime_module libexec/mod_mime.so
#用于缺省index文件: index.php etc.
LoadModule Dir_module libexec/mod_dir.so

Available for use are:
#比如: You need to debug PHP under ~/username/to
LoadModule Userdir_module libexec/mod_userdir.so
#比如: A previous URL needs to be turned on or a CGI Script-alias is required
LoadModule Alias_module libexec/mod_alias.so


Common modules:
The most commonly used may be the front-end of PHP and Java application Server, in addition, from the performance of the use of mod_gzip can reduce the flow of about 40%, reduce the machine for transmission load, and mod_expires can reduce about 10% of duplicate requests, Lets duplicate users cache the specified page request results locally and do not issue a request to the server at all.

It is recommended that all module configurations be placed within the configuration of the corresponding modules: <ifmodule some_module.c>some_module config </IfModule>

Installation of PHP:
/path/to/php_src/configure--with-apxs=/path/to/apache/bin/apxs--with-other-modules-you-need
Configurations that need to be modified:
AddType application/x-httpd-php. php. php3. any_file_in_php

Installation settings for resin:
/path/to/resin/src/configure--with-apxs=/path/to/apache/bin/apxs

The specific resin settings are placed in another file: for example,/home/resin/conf/resin.conf
<ifmodule mod_caucho.c>
Cauchoconfigfile/path/to/apache/conf/resin.conf
</IfModule>

Installation configuration for 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>

Comments:
All. gif files expire after 1 months
All files expire 1 days after default


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


Round robin of logs: Installation and setup of Cronolog

Cronolog can be very neat to store logs on a day-round basis
The default compilation is installed under/usr/local/bin/, just to change the configuration to:

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


The log will be truncated by day and stored in a directory named weekly directory: for example: LOG/1 is Monday, LOG/5 is Friday, log/0 is Sunday

Compress daily logs with gzip:
4 * * */usr/bin/gzip-f/home/apache/logs/' date-d yesterday +%w '/access_log

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

Upgrade Maintenance:

Since the use of dynamic Module loading mode (DSO mode) to install Apache,apache httpd Core Services and application modules and application modules have become very flexible, we recommend that all the Independent module configuration
<ifmodule mod_name>
Configurations..
</IfModule>
, this configuration is very easy to adjust by masking a module: for example:
#AddModule mod_gzip.c
It shields the mod_gzip, while the other modules do not have any effect.

Installation and maintenance process:

System installation: System Administrator's responsibility is to install the system and a according to the DSO mode installed Apache, and then colon.
Application installation: The application Administrator is responsible for the specific application of the required modules and set the httpd.
System Upgrade: System Administrator: Upgrade the system/upgrade Apache
Application Upgrades: Application Manager: Upgrade application modules: PHP Caucho, etc.
System backup/restore: If Apache is not on the default system disk, just want to backup the Apache directory, encounter system partition hardware problems directly using a prepared system colon, and then directly to the physical disk of Apache recovery on the line.
System administrator: Apache's most simplified installation OS + Apache (httpd core only)
Application Administrator: Application module to customize the Pure static page service
Core
PHP Dynamic page
Core+so
+php
Java applications
Core+so
+caucho
+ssl
Application Example: www.example.com
Image.example.com
Bbs.example.com mall.example.com




Example: Standalone upgrades for Apache and PHP modules.

If Apache is installed in the following ways:
./configure--prefix=/home/apache--enable-shared=max--enable-module=most
PHP is installed in the following ways:
./configure--with-apxs=/home/apache/bin/apxs--enable-track-vars--with-mysql

When you upgrade Apache alone later, you are still:
./configure--prefix=/home/apache--enable-shared=max--enable-module=most
Make
Su
#/home/apache/bin/apachectl stop
#make Install

When you upgrade PHP individually, you are 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


Web acceleration based on anti-phase proxies:
Squid and mod_proxy can be reversed-phase agent acceleration. and caching based agent acceleration than the original Web services, speed will be a number of levels of ascension.



Small tip:

After Apache installation, there are no but 2 files that are useful in the default root directory:

Favicon.ico:favicon.ico is a 16x16 site icon file, if the browser found this file, in the address bar will use this icon to replace the browser page icon. This feature is supported by mainstream browsers such as IE6 and Mozilla.
For example: Http://www.chedong.com/favicon.ico

Robots.txt: A crawler (spider) website that tells search engines that pages can be indexed and those that can't.
For specific instructions please refer to: http://www.robotstxt.org/wc/robots.html
Reference Documentation:

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


Search engine-oriented CMS design:
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.