Linux Apache installation notes

Source: Internet
Author: User
Tags server memory

Keywords: Apache install PHP resin mod_gzip mod_expire Webalizer cronolog

Summary:

From the perspective of simplified installation ==> Performance Tuning ==> convenient maintenance, we will discuss web service planning ==> httpd installation/application module configuration ==> upgrade/Maintenance

.

The upgrade of Apache and application modules such as PHP resin does not affect 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:
/Path/to/apache_src/configure -- prefix =/another_driver/Apache -- enable-shared = max -- enable-module = most
Installation of optional application modules/tools: PhP resin mod_gzip mod_expire and cooperation between various modules;

PHP installation:
/Path/to/php_src/configure -- With-apxs =/path/to/Apache/bin/apxs -- enable-track-vars -- With-other-modules-you-need

Mod_resin installation:
/Path/to/resin/src/configure -- With-apxs =/path/to/Apache/bin/apxs

Mod_gzip installation:
/Path/to/Apache/bin/apxs-I-a-c mod_gzip.c

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 System Administrator is responsible for installing the system => installing a DSO-mode Apache, 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: System Administrator: Upgrade application module
Specific instructions:

Web application capacity planning

Apache is mainly a memory-consuming service application. I personally sum up the following formula:

Apache_max_processerials_with_good_perfermance <(total_hardware_memory/apache_memory_per_process) * 2
Apache_max_process = apache_max_processerials_with_good_perfermance * 1.5

Why is there an apache_max_processerials_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 crash due to the use of the hard disk 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 managing more 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_processerials_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 Apache as much as possible, 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_processerials_with_good_perfermance <(2g/2 m) * 2 = 2000
Therefore, 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:
In UNIX operating systems such as FreeBSD and Linux, the default maximum number of Apache processes is 256.

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: General Compilation options can standardize the installation process
./Configure -- prefix =/another_driver/Apache/-- enable-shared = max -- enable-module = most

Explanation:
-- Prefix =/another_driver/Apache/: Hard Disks are generally used for the lowest service life of a system. Therefore, completely separating service data from the system not only improves data access speed, more importantly, it greatly facilitates system upgrades, backup, and recovery.

-- Shared-module = max: using the dynamic loading method will bring about a 5% performance reduction, but it is nothing more than the benefits: for example, the module upgrade is convenient and the risk of system upgrade is reduced, installation process standardization

-- 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, all sources must be combined for both modules and Apache upgrades.

Apache's default configuration file is generally relatively large: We 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 to let Apache adjust the number of 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 module is 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.

Generally, the following modules are not required:
# Loadmodule env_module libexec/mod_env.so
# Loadmodule negotiation_module libexec/mod_negotiation.so
# Loadmodule statuserials_module libexec/mod_status.so
# Server side include is out of date
# Loadmodule includeserials_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 asiserials_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 verification
# Loadmodule accesserials_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 aliaserials_module libexec/mod_alias.so

Common modules:
The most commonly used wrapper is for PHP and Java Web applications. In addition, in terms of performance, mod_gzip can reduce the traffic by about 40%, thus reducing the load of machines for transmission, mod_expires can reduce the number of duplicate requests by about 10%, so that duplicate user requests are cached locally and no requests are sent to the server.

We recommend that you put the configurations of all modules in

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

Generally, the specific resin settings are placed in another file:
<Ifmodule mod_caucho.c>
Cauchoconfigfile/path/to/Apache/CONF/resin. conf
</Ifmodule>

Installation configuration of mod_expires:
<Ifmodule mod_expires.c>
Expiresactive on
All. GIF files expire after 1 month
Expiresbytype image/GIF "access plus 1 month"
# All files expire after 1 day by default
Expiresdefault "now plus 1 day"
</Ifmodule>

Mod_gzip installation:
/Path/to/Apache/bin/apxs-I-a-c mod_gzip.c

Configuration of mod_gzip and PHP together
<Ifmodule mod_gzip.c>
Mod_gzip_on Yes
Mod_gzip_minimum_file_size 1000
Mod_gzip_maximum_file_size 300000
Mod_gzip_item_include file. htm $
Mod_gzip_item_include file. html $
Mod_gzip_item_include file. php $
Mod_gzip_item_include file. php3 $
Mod_gzip_item_include mime text /.*
Mod_gzip_item_include mime httpd/Unix-directory
# Do not use the same temporary directory for mod_gzip and PHP sessions: php_session needs to be set through PHP. ini session. save_path =/tmp/php_sess
Mod_gzip_temp_dir/tmp/mod_gzip
Mod_gzip_dechunk Yes
Mod_gzip_keep_workfiles No
</Ifmodule>
Mod_gzip and mod_php: Do not use the same temporary directory for mod_gzip and mod_php;

Mod_gzip and resin: Make mod_gzip load after mod_caucho; otherwise, mod_gzip does not work.
... OTHR modules
Addmodule mod_so.c
Addmodule mod_caucho.c
# Notice: mod_gzip must load after mod_caucho
Addmodule mod_gzip.c
Addmodule mod_expires.c
...

<Ifmodule mod_gzip.c>
Mod_gzip_on Yes
Mod_gzip_dechunk Yes
Mod_gzip_keep_workfiles No
Mod_gzip_minimum_file_size 3000
Mod_gzip_maximum_file_size 300000
Mod_gzip_item_include file. html $
Mod_gzip_item_include mime text /.*
Mod_gzip_item_include mime httpd/Unix-directory
Mod_gzip_item_include handler 'caucho-request'
</Ifmodule>

Installation and Setup of cronolog: cronolog can store logs in a neat manner 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/path/to/Apache/logs/% W/accesserials_log" combined

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

Upgrade and maintenance:

Because Apache is installed in the standard DSO mode, the httpd core service of Apache is flexible with the application modules and application modules. We recommend that you set the configurations of all 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, and 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, restore the physical disk where Apache is located.
System Administrator: the simplest way to install Apache OS + Apache (httpd core only)
Application administrator: application module customization + so
+ PHP
+ So
+ Caucho
+ SSL
Application: Pure Static Page service:
Image.example.com
Www.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

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.
Robots.txt: Used to tell the search engine crawler websites that pages can be indexed, but those pages cannot. For more information, see:
Http://www.robotstxt.org/wc/robots.html

Reference:
Apache http://httpd.apache.org
PHP http://www.php.net
Resin http://www.caucho.com
Mod_gzip http://www.remotecommunications.com/apache/mod_gzip/
Cronolog http://www.cronolog.org
Mod_expires http://httpd.apache.org/docs/mod/mod_expires.html

Previous Article: USB device detection in Linux
Next article: Linux Samba Configuration

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.