High performance PHP Application Development-Summary

Source: Internet
Author: User
Tags apc apc configuration benchmark php database sessions apache log elastic load balancer

Recently, "High-performance PHP application development," the book read two times, I think it has benefited, the book is more extensive, is an empirical content, suggest that you have time to look at the detailed, based on this deliberately the essence of the book's easy-to-use parts to be sorted out, the son said: "Warm so know the new, Can be a teacher. "


[chapter I benchmark tools]

One: Apache Benchmark (AB)

Install with Apache Package
Ab-n (number of requests)-C (concurrent number, cannot play-N)-T (number of seconds requested) Http://url

The most important thing in the report is the following field content:
-------------------------
Total size of content requested by HTML transferred
Requests per second number of concurrent requests/sec supported
Time per request Total times required
Time per request for one of the requests in the total send request
-------------------------

Two: Siege [can specify URL list file]
Installation:
wget ftp://ftp.joedog.org/pub/siege/siege-latest.tar.gz
Tar Xvfz siege-latest.tar.gz
CD siege-2.69/
./configure
Make && Install

SIEGE-C (number of concurrent)-T (number of times) ([s (sec)/M (min)/h (hours)]) [url]-i-f (url list file)

The most important thing in the report is the following field content:
-------------------------
The total size of data in the transferred response (excluding tag header data)
Transaction rate The total number of transactions to be satisfied per second
Longest transaction The maximum time required to satisfy a request
Shortest transaction minimum time required to meet a request
[Other instructions]
Transactions total number of transactions completed
Availability the time that a Web document can be requested
Elapsed time required to complete the test
Response time average response times throughout the testing process
Throughput total time required to process data and responses
Concurrency the average of simultaneous connections, the server performance is reduced when this number increases
Successful transactions total number of successful transactions performed during the entire test
Failed transactions total number of unsuccessful executions during the entire test
-------------------------


[chapter II improving client download and rendering performance]

One: Toolset:
Firebug/yslow/page Speed-yui compressor/closure compiler/smush.it

Firebug net tab Color meaning
-------------------------------------
Purple Browser is waiting for resources
Gray is receiving resources
Red is sending
Green is connecting
Blue DNS Lookup
-------------------------------------

II: YSlow V2 rule Set

(a): CSS optimization
1: Put style CSS files on top
2: Avoid CSS expressions
3: Compressing CSS files

(ii): Image optimization Rules
1: Use the desired image size instead of the style to resize it
2: Possible to create a screen, will be all the small icons are placed in a large map, the use of a reservation method to present

(c): JavaScript optimization rules
1: Put JS file on the layer
2: Make the JS code an external file
3: Compressed JS file size

(iv): Server optimization
1: Using CDN
2: Using GZIP/BZIP2 compression
3: Reduce DNS Lookups
4: Implementing the Etag

YSlow rating is from A=>d (High to bottom)

Three: The picture uses the general rule:

Use GIF to make logo and small icons on the page, render photos in JPEG, and all other images in PNG format.
Images can be used yslow->tools->smush.it


[Chapter III PHP code optimization]


1: Use require faster than require_once (stat calls less)
Require_once () A large number of operation state calls are made when the script is imported, if the file is located in
/data/web/html/php/a.php A stat call is made for each directory.

2: Indicates the number of times the loop was calculated

3: Use foreach as much as possible to facilitate data, it is the fastest.

4: File Read principle:
Small file (<1m) Usage: fread () Come on.
Large file (>=1m) Usage: file_get_contents () Come on.


[fourth Chapter opcode cache]

1:alternative PHP Cache (APC)

Installation Source: http://downloads.php.net/pierre/

(APC common settings)
-----------------------------------------
Apc.cache_by_default cache is enabled by default. 1: Enable 0: Disable
Apc.filters uses regular expressions to determine which files are to be cached
Apc.stat whether the request script is enabled for update checking, which is executed every time the script is invoked.
1: Enable 0: Disable, restart the Web server if there is a change to the PHP script
Apc.enabled whether to enable cache 1: Enable 0: Disable
Apc.shm_size setting the size of shared memory, in M
Apc.shm_segments total number of shared memory segments available
Apc.include_once_override whether the optimizations for include_once and require_once are enabled,
When enabled, the call to stat can be reduced. 1: Enable 0: Disable
Apc.optimization optimization level, 0: Disable the feature, set to a higher value to improve the level of optimization 1-9
Apc.num_files_hint set the number of files you think you want to cache, default is 1000, and if not, set to 0.
Setting to actual number can improve performance.
Apc.ttl sets the expiration time of the cache, which is cleared from the cache only when the expiration time is reached, in seconds
Apc.write_lock When this option is turned on, a single process is forced to cache a specific script for
Large-volume Web applications with a single file.
-----------------------------------------
(APC configuration example)
Extension=apc.so
Apc.enabled=on
Apc.shm_size=16
Apc.include_once_override=1
Apc.write_lock=1
Apc.optimization=9
Apc.stat=0
Apc.num_files_hint=5

(APC management tools)
The installation package comes with the apc.php Web tool, which can be run under the Web Services directory.


2:xcache

Installation Source: Http://xcache.lighttpd.net
[XCache]
Zend_extension_ts=php_xcache.dll (TS: indicates thread safety)

(XCache common settings)
-----------------------------------------
Xcache.admin.user (string) authenticated user name
Xcache.admin.pass (string) authentication password
Xcache.admin.enable_auth (string) whether authentication is enabled, default: ' On '
Xcache.test (String) whether the test feature is enabled
Xcache.coredump_dir (String) when a core dump is placed in a directory that requires PHP to have write permission,
Leave blank when on behalf of disabled
Xcache.cacher (Boolean) Whether caching is enabled, enabled by default
Xcache.size (int) shared cache size, if 0 the cache will not be available
Xcache.count (int) caches the number of "blocks" allocated, which defaults to 1
Xcache.slots Hash table hints that the larger the number, the faster the search is performed within the Hashtable, and the larger the value,
The more memory you need
xcache.ttl (int) opcode file lifetime, if 0 is cached indefinitely
Xcache.gc_interval (int) time interval to trigger garbage collection by default of 0
xcache.var_size (int) Variable size
Xcache.var_count (int) variable number
Xcache.var_slots variable data slot settings
Xcache.var_ttl (SEC) Time-to-live for variable data, default to 0
Xcache.var_maxttl (seconds) maximum time to live when processing variables
Xcache.var_gc_interval (SEC) time to live garbage collection
Xcache.readonly_protection (Boolean) slows down the tool while it is enabled, but is more secure.
Xcache.mmap_path (String) User-read-only protected file path. This will limit two PHP groups to share the same/tmp/cache directory
Xcache.optimizer (Boolean) Whether optimizations are enabled, default: Off
Xcache.coverager (Boolean) enables a collection of coverage data, which slows down the running process when enabled
Xcaceh.coveragedump_directory (String) directory where data collection information is placed, default:/tmp/pcovis
-----------------------------------------

3:eaccelerator (the cache can be present on disk)

Installation Source: Http://www.eaccelerator.net
wget http://bart.eaccelerator.net/source/0.9.6.1/eaccelerator-0.9.6.1.tar.bz2
Tar xvjf eaccelerator-0.9.6.1.tar.bz2
Phpize
./configure
Make && make install

(Method of caching data)
Eaccelerator.keys = [Shm_and_disk: Default value, if memory space is not sufficient, to disk Shm_only: use only memory
Disk_only: Use only HDD]

Default storage directory:/tmp/eaccelerator is emptied each time the system restarts, it is recommended to change to the following directory
Mkdir-p/var/cache/eaccelerator
chmod 0777-r/var/cache/eaccelerator

(EA configuration Item description)
----------------------------------------------------
Eaccelerator.shm_size
Shared memory size, in MB. "0″ represents the operating system default. The default value is "0″."

Eaccelerator.cache_dir
The directory for the disk cache. The default value is "/tmp/eaccelerator"

Eaccelerator.enable
Turn eaccelerator on or off. "1″ is turned on," 0″ is off. The default value is "1″."

Eaccelerator.optimizer
Turning on or off the internal optimizer can improve code execution speed. "1″ is turned on," 0″ is off. The default value is "1″."

Eaccelerator.debug
Whether the debug log is enabled, logging is in Eaccelerator.log_file. "1″ is turned on," 0″ is off. The default value is "0″."

Eaccelerator.log_file
Directory files, not specified when all logs are in stderr, if Apache is used in the Apache log

Eaccelerator.name_sapce
A prefix string for all key (keys). Setting this prefix string allows the. htaccess or master configuration file to run two identical key names on the same host.

Eaccelerator.check_mtime
Turn PHP file change checking on or off. "1″ is turned on," 0″ is off. If you want to recompile the PHP program after the modification, you need to set it to "1″." The default value is "1″."

Eaccelerator.filter
Determine which PHP files must be cached. You can specify the cache and non-cached file types (such as "*.php *.phtml", etc.). If the argument starts with "!", the file that matches these parameters is ignored by the cache. The default value is "" All PHP files will be cached.

Eaccelerator.shm_max
When you use the "eaccelerator_put ()" function, it is forbidden to store too large files in shared memory. This parameter specifies the maximum value that is allowed to be stored in bytes (10240,10k,1m). "0″ is not limited. The default value is "0″."

Eaccelerator.shm_ttl
When Eaccelerator gets the shared memory size of a new script fails, it removes all script caches that cannot be accessed in the last "Shm_ttl" seconds from shared memory. The default value is "0″, which is: Do not delete any cache files from the shared inner spring."

Eaccelerator.shm_prune_period
When Eaccelerator gets the shared memory size of a new script fails, he tries to remove the cache script that is older than "shm_prune_period" seconds from shared memory. The default value is "0″, which is: Do not delete any cache files from the shared inner spring."

Eaccelerator.shm_only
Allows or disables the caching of compiled scripts on disk. This option is not valid for session data and content caching. The default value is "0″, which is: caching using disk and shared memory."

Eaccelerator.compress
Allows or disables the compression of the content cache. The default value is "1″, which is: Allow compression."

Eaccelerator.compress_level
Specifies the compression level of the content cache. The default value is "9″, which is the highest level."

Eaccelerator.keys
Eaccelerator.sessions
Eaccelerator.content
Determine which keys (keys), session data and content will be cached.
The available parameter values are:
"Shm_and_disk" – caches data in both shared memory and disk (default);
"SHM" – caches data in shared memory or on disk if the shared memory is exhausted or the data capacity is greater than "Eaccelerator.shm_max";
"Shm_only" – caches data only in shared memory;
"Disk_only" – caches data only on disk;
"None" – suppresses the caching of data.

Eaccelerator.admin.name User Name
Eaccelerator.admin.password Password
Eaccelerator.allowed_admin_path the script path allows for management information and administrative control.
----------------------------------------------------

(EA configuration example)
extension= "Eaccelerator.so"
Eaccelerator.shm_size= "16"
Eaccelerator.cache_dir= "/var/cache/eaccelerator"
eaccelerator.enable= "1"
Eaccelerator.optimizer= "1"
Eaccelerator.check_mtime= "1"
eaccelerator.debug= "0"
Eaccelerator.filter= ""
eaccelerator.shm_max= "0"
Eaccelerator.shm_ttl= "0"
eaccelerator.shm_prune_period= "0"
eaccelerator.shm_only= "0"
eaccelerator.compress= "1"
Eaccelerator.compress_level= "9"


[Fifth chapter variable cache]
Apc/memcache installation Source: http://www.memcached.org


[Sixth. Select the correct web operator]

A: The problems to be considered in the synthesis

1: Safety and stability are important
Apache is currently the most secure and stable service package.

2: It is important to find engineers with extensive knowledge and experience
APACEH is the best choice if you want to find a top-notch Web Services technical engineer

3: Whether your site is primarily static content
If the majority is static content, it is recommended to use NGINX/LIGHTTPD

4: You are hosted in Managed services
If you are in a managed service, when using dependent non-APACEH, check to see if the hosting service is not supported.

5; You are using an uncommon PHP extension
Most PHP extensions were developed at the time of development on APACEH with mod_php modules, NGINX/LIGHTTPD used
In fcgi mode, there are usually no rigorous tests, and it is important to check whether the Web services are functioning properly in fcgi mode before selecting them.

II: Classification of Web servers

(prefork/fork)
A process-based Web server that uses the Fock process to satisfy each request; Recommended single CPU or database integration/file processing
Applications use this type because most of the third-party extensions are not proven to be thread-safe.

(threaded)
A thread-based Web server that uses threads to satisfy each request;

III: Apache HTTPD

Command-line Arguments httpd-h
-----------------------------------------
-d name to set names for use in <ifdefine name> directives
-D Directory Settings ServerRoot
-F File Setting Serverconfigfile
-c "directive" handles the instruction before reading the configuration file
-C Processing of instructions after reading a configuration file
-e level displays a startup error with the log levels
-e File records startup error logging to ' file '
-V Show compilation settings
-l List compiled modules
-l list available configuration directives
-t-d dump_vhosts Show All vhost settings
-S (shortcut to-t-d dump_vhosts)
-t-d dump_modules Show loaded modules
-m (shortcut to-t-d dump_modules)
-t Check configuration file syntax
-----------------------------------------

List of MPM (multi-processing modules) available in Apache
-------------------------------------------
Prefork: Preview creates a set of sub-processes. (default installation method)
A variant of the perchild:prefork that allows individual process permissions to be set for a child process
Threadpool: Using multithreading to handle requests-most UNIX class systems are not recommended and are not guaranteed to be thread-safe.
A mix of worker:prefork and ThreadPool, where each child process supports multiple threads.
-------------------------------------------
It is recommended to use Prefork MPM, which is not recommended for any multithreaded mpm because it is much more binding to Apache and PHP interpreter library files
are not proven to be thread-safe.

(PHP configuration with fast-cgi mode when installing other Web services)
#[fastcgi PHP]
Cgi.fix_pathinfo = 1 (fixed a bug in the previous CGI implementation)


[Seventh. Optimizing Web server and content delivery]

1: Determining the performance of the Web server

Apachetop Installation
--------------------------------------------------------------
wget http://www.webta.org/apachetop/apachetop-0.12.6.tar.gz
sudo yum install Readline-devel
sudo yum install Ncurses-devel
sudo yum install Pcre-devel
Tar xvzf apachetop-0.12.6.tar.gz
CD apachetop-0.12.6
./configure
Make
sudo make install
--------------------------------------------------------------

Use: Apachetop-f The/var/log/apache2/access_log parameters are described below:
-----------------------------------------------------------
-f:logfile to open a monitored log file
-H Hits: The window displays the most recent hits access record. Example:-H 10
-T sec: window displays the URL of the access record for the purge interval, which defaults to 30 seconds.
-D secs: The window displays the data refresh time, which is refreshed once by default of 5 seconds. Example:-D 2
Example: Apachetop-f/usr/local/nginx/logs/access_log-t 500-d 2


The interface parameters are described in detail as follows:
----------------------------------------------
First line: The time of the last click, the total time of Apachetop run, the current time
Second, four lines: represents the number of requests, the average number of requests per second, the average transmission per second, the average per request transmission;
The difference is that the second line runs the calculation from Apachetop, and the fourth line is calculated from the last time the request was emptied.
The time in parentheses at the beginning of the line, the time is related to the-T and-D parameters mentioned below, how much is t, and what is the maximum time?
The-D is how much, this time increases a few times, until and T's time equal, will not change.

The filtering mechanism operates as follows:
----------------------------------------------
Type: "F" "A" "U" and then go to: ". php", then filter the URL ending with. php

2: Optimized Apache prefork MPM
Vi/etc/apache2/conf/extra/httpd-mpm.conf
---------------------------------------------------
Instruction description
---------------------------------------------------
Startservers controls the number of clients that will be generated when the Aapache starts, and it is best to have enough clients to handle the idle traffic due to the overhead of creating the client. (Default value: 5)
Minspareservers under normal circumstances, Apache terminates some clients if the number of requests is reduced and Apache cannot provide the normal reason for owning these requests. This directive is for APACEH
Set a lower limit on the number of clients that remain active, which should not be less than startservers (default: 5)
Maxspareservers The command sets the point at which Apache starts discarding the client. If the number of active clients is more than 10 more than the number of concurrent requests currently being processed, Apache will start
Terminating and discarding the client until this number reaches the Minspareservers value, which is set too low, will cause Apache to block the client process and use this setting with caution. (Default value: 10)
MaxClients This instruction sets the maximum number of processes that Apache will produce, thereby setting the maximum number of simultaneous concurrent requests it can handle. (default value: 150)
Maxrequestsperchild This instruction sets the maximum number of requests that the child process will process before being terminated and re-generated. If set to 0, the process is permanently present and never dies. (default value: 0)

3: Optimize memory usage and prevent switching
General experience 1G of memory can support up to 65 concurrent requests. Here you can change maxclients to 65 without transitioning to use memory.
To ensure that the client is not blocked while running on the load, it should probably be startservers for about half 30 of 65. It will also
Minspareservers is set to 30, and then Maxspareservers is set to 40, which is the appropriate value to prevent the client from being blocked.

4:aapche Other Optimizations
A: If possible, disable the. htaccess file, allowoverride disable
B: Disable FollowSymLinks
C:directoryindex The first file should specify the most frequently used files
D: Turn off DNS lookups for Hostnamelookup
E: Enable keep-alive permanent connection
F: Use Mod_deflate to compress content

5: Load balancing (distributing requests to multiple servers)
A: Using Round-robin DNS
B: Using load balancer, the software usually has: Lvs,mod_proxy hardware: F5,coyotepoint,cisco,juniper and so on.
C: Load Balancer Service, Amazon Elastic load balancer, etc.

6: Shared sessions between multiple servers
A:memcache
B: Shared directory files (NFS, etc.)
C: Database

7: Server Deployment
Domino Failure Effect: Assume a server farm consisting of two servers, based on the average load and concurrency, each server's load is probably
60% of the capacity, the failure of one of the two servers causes another to attempt to handle a 120% load of the total capacity, causing it to fail.

Therefore, when designing a server cluster, it is necessary to ensure that it can tolerate one or more servers failing. If you have only two servers, you must monitor their load factors.
If the load has been up to 50%, you should plan to add additional servers. Otherwise easy to enter Domino failure effect.

When multiple servers are deployed, consider setting up an internal DNS domain name for each server for authentication, such as www1.demo.com,www2.demo.com.


[eighth. Database Optimization-mysql]
------------------------------------
MyISAM Advantages:
1: Can quickly query unique values
2: Full Text Search support
3: SELECT COUNT (*) fast
4: Less disk space consumption
------------------------------------
MyISAM Disadvantages:
1: Table level Lock, table lock slows program speed if the program writes more than 5% of the total time of the operation Point
2: Ability not to support transactions
3: Persistent problem, table crash requires lengthy repair operation to recover
----------------------------------

------------------------------------
InnoDB Advantages:
1: Support transactional capabilities
2: Row-level locking capability, which is not serialized when writing to the same table concurrently
3: Multiple online backup strategies are supported.
4: Improve the program in high load, high concurrency under the ability.
------------------------------------
InnoDB Disadvantages:
1: SELECT COUNT (*) is slow, it needs to calculate the line.
2: No full-text search.
3: The self-increment field must be the first field and may cause problems when migrating.
4: Point to use more disk space.
5: Simple check table speed is not as MyISAM, but complex, the query speed of multiple tables has been myisam.
----------------------------------

Summary of the selection engine:
1: The program executes most of the time is read operation (more than 95%) should choose MyISAM.
2: When transactional and consistency are important, you should choose InnoDB.
3: When you have a complex pattern that contains many connection tables. You should choose InnoDB.
4: When uninterrupted operation is very important, it is recommended to select InnoDB if the 24*7 is running all day.

(MySQL memory usage comparison)

Amount of memory used by each connection (thread):
Per_connection_memory =
Read_buffer_size//memory for sequential table scans
+read_rnd_buffer_size//Memory for Buffering reads
+sort_buffer_size//Memory for in mem sorts
+thread_stack//per connection memory
+join_buffer_size//Memory for in mem table joins

Memory data used per server (fixed):
Per_server_memory =
Tmp_table_size//memory for all temp tables
+max_heap_table_size//MAX size of single temp table
+key_buffer_size//Memory allocated for index blocks
+innodb_buffer_pool_size//main cache for INNODB data
+innodb_additional_mem_pool_size//INNODB record structure cache
+innodb_log_buffer_size//log file Write buffer
+query_cache_size//Compiled statement cache

The maximum memory that MySQL can use is defined as follows:
Max_memory = (Per_connection_memory * max_connections) + per_server_memory


Memory tool for optimizing database server: mysqltuner.pl

1: Installation
wget Mysqltuner.pl-o mysqltuner.pl
chmod +x mysqltuner.pl
./mysqltuner.pl

(Optimized InnoDB)
The following assumptions are based on 16G of memory:
Innodb_file_per_table:
By default, InnoDB creates a file for each database and uses this file to manage the data tables. This means that if the size of the table grows first and then shrinks,
It is difficult to recover disk space. Setting this value causes each table to use a separate data store file. If you want to change this setting on the current database, you should
Back up the database, then delete it, change the options, and then restore the data from the backup after the new new service is started.
Innodb_buffer_pool_size=:
If you use only the InnoDB table, you can set it to about 70% of the available memory, and if you mix with MyISAM, reduce the amount to myisam space.
INNODB_LOG_BUFFER_SIZE=4M:
4M can meet the needs of most records, masks provide reasonable performance. If you have a large text field or BLOB field, or if the record is very large, you can set a high point.
innodb_log_file_size=256m
This is the recommended value to strike a good balance between recovering the database and running high runtime performance.
innodb_flush_log_at_trx_commit=2
This controls how often log files are flushed to disk. If you can tolerate the loss of some records in the event of a crash, you can set it to two to reduce the disk write.
Selectable items: 0: Synchronous Write per second 1: Write in time 2: Write cache, written by operating system control

(Slow query logging)
[Mysqld]
Log-slow-queries=/var/log/mysql/mysqld-slow.log
Long_query_time=1

(Analysis of problematic queries)
Explain tools

(Recommendations for PHP database applications)
1: From the outset to consider the use of M/s reading and writing programs for future upgrades to add convenience.
2: The UTF8 character set is used by default,
3: Use UTC date Format
3.1: Install the time zone database
Mysql_tzinfo_to_sql/usr/share/zoneinfo | Mysql-u Root MySQL
[Mysqld]
Default-time-zone=utc

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.