Nginx 0.7.x + PHP 5.2.6 (FastCGI) + MySQL 5.1 configuration optimization on M small memory VPs server [original]

Source: Internet
Author: User
Tags root access virtual private server vps server

[Author: Zhang banquet this article version: V1.0 final modification: 2008.10.28 reprinted please indicate the original article link: http://blog.s135.com/post/375/]

VPS (virtual private server) is the use of the latest virtualization technology to create multiple isolated Virtual Private hosts on a physical server. They share hardware, software licenses, and manage resources with maximum efficiency. For its users and applications, the operation and management of each VPs platform are identical to that of an independent host, each VPs can be restarted independently and has its own root access permissions, users, IP addresses, memory, processes, files, applications, system function libraries, and configuration files.

The most important indicator of a VPs server is the memory size. Multiple VPs servers can share one CPU, but cannot share the same memory. The larger the memory, the more expensive the price is.

The following uses the VPs of my blog as an example to describe how to optimize nginx 0.7.x + PHP 5.2.6 (FastCGI) + MySQL 5.1 in MB of memory.

For the installation and configuration of nginx + PHP + MySQL, see nginx 0.7.x + PHP 5.2.6 (FastCGI) build a web server that is 10 times better than Apache (version 4th).

  Optimized results:

One nginx process that provides the HTTP service occupies 11 MB of physical memory, five PHP-CGI processes occupy about 8 MB of physical memory, and one MySQL Server occupies 7 MB of physical memory, with two nginx and PHP-CGI parent processes that occupy a small amount of memory, the nginx + PHP + MySQL series only occupy 47.7% of the physical memory, that is, 62m physical memory (128 M * 47.7% ≈ 62 m ).

  

In addition, the VPs server system itself and other programs will also use some memory, but the VPs with M memory is enough. In general, after optimization, the VPs with MB of memory runs nginx + PHP + MySQL with good results. Of course, it would be better if you have money to buy VPs with larger memory.

  The optimization items are as follows:

  1. Add M swap files
1. Create and activate swap files

CD/var/
Dd If =/dev/Zero of = swapfile BS = 1024 COUNT = 262144
/Sbin/mkswap swapfile
/Sbin/Swapon swapfile

2. Add it to the fstab file to enable automatic startup during system boot.

VI/etc/fstab

Add the following content at the end:

Reference/var/swapfile swap defaults 0 0

See: http://blog.s135.com/post/374.htm

  Ii. nginx 0.7.19 main configuration file (nginx. conf) Optimization

Reference user WWW;

# Nginx each process consumes 10 MB ~ MB memory. Only one nginx process is enabled to save memory.
Worker_processes 1;

Error_log/data1/logs/nginx_error.log crit;

Pid/usr/local/webserver/nginx. PID;

# Specifies the value for maximum file descriptors that can be opened by this process.
Worker_rlimit_nofile 51200;

Events
{
Use epoll;
Worker_connections 51200;
}

HTTP
{
Include mime. types;
Default_type application/octet-stream;

# Charset gb2312;

Server_names_hash_bucket_size 128;
Client_header_buffer_size 32 K;
Large_client_header_buffers 4 32 K;

Sendfile on;
Tcp_nopush on;

Keepalive_timeout 60;

Tcp_nodelay on;

Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Fastcgi_buffer_size 64 K;
Fastcgi_buffers 4 64 K;
Fastcgi_busy_buffers_size 128 K;
Fastcgi_temp_file_write_size 128 K;

# Enable gzip compression for webpage files, CSS, JS, and XML to reduce data transmission volume and increase access speed.
Gzip on;
Gzip_min_length 1 K;
Gzip_buffers 4 16 K;
Gzip_http_version 1.0;
Gzip_comp_level 2;
Gzip_types text/plain application/X-JavaScript text/CSS application/XML;
Gzip_vary on;

# Limit_zone crawler $ binary_remote_addr 10 m;

Server
{
Listen 80;
SERVER_NAME blog.s135.com www.s135.com s135.com * .s135.com;
Index. html index.htm index. php;
Root/data0/htdocs/blog;

# Limit_conn crawler 20;

# Static rewrite for the bo-Blog system
Rewrite ^/post/(%0-9%%%%.htm $/read. php? $1 last;
Rewrite ^/post/(%0-9%%%%_(%0-9%%%%%%.htm $/read. php? $1 & page = $2 last;
Rewrite ^/post/(1_0-91_1_1_1__(1_0-91_1_1__(1_0-91_1_1_1_.htm $/read. php? $1 & page = $2 & Part = $3 last;
Rewrite ^/index_(1_0-91_1_1_1__(1_0-91_1_1_.htm $/index. php? Mode = $1 & page = $2 last;
Rewrite ^/star_(1_0-91_1_1_1__(1_0-91_1_1_.htm $/star. php? Mode = $1 & page = $2 last;
Rewrite ^/category_(%0-9%%%%%.htm $/index. php? Go = Category _ $1 last;
Rewrite ^/category_(1_0-91_1_1_1__(1_0-91_1_1_1__(1_0-91_1_1_1_.htm $/index. php? Go = Category _ $1 & mode = $2 & page = $3 last;
Rewrite ^/archive_(%0-9%%%%%_(%0-9%%%%%%.htm $/index. php? Go = Archive & CM = $1 & Cy = $2 last;
Rewrite ^/archive_(1_0-91_1_1_1__(1_0-91_1_1_1__(1_0-91_1_1_1_1__(1_0-91_1_1_1_.htm $/index. php? Go = Archive & CM = $1 & Cy = $2 & mode = $3 & page = $4 last;
Rewrite ^/showday_(1_0-91_1_1_1__(1_0-91_1_1_1__(1_0-91_1_1_1_.htm $/index. php? Go = showday _ $1-$2-$3 last;
Rewrite ^/latest $/index. php? Go = showday _ $1-$2-$3 & mode = $4 & page = $5 last;

Location ~ . * \. (PhP | PhP5 )? $
{
# Change the communication mode between nginx and FastCGI from TCP to UNIX socket. TCP is more stable than UNIX socket in High-concurrency access, but UNIX socket is faster than TCP.
Fastcgi_pass Unix:/tmp/PHP-cgi.sock;
# Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fcinclude gi. conf;
}

Location ~ /Read. php
{
# Change the communication mode between nginx and FastCGI from TCP to UNIX socket. TCP is more stable than UNIX socket in High-concurrency access, but UNIX socket is faster than TCP.
Fastcgi_pass Unix:/tmp/PHP-cgi.sock;
# Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fcinclude gi. conf;
}

# The number of blog images is large and the number of changes is small. caching them locally in the browser for 15 days can increase the loading speed of my blog page next time.
Location ~ . * \. (GIF | JPG | JPEG | PNG | BMP | SWF) $
{
Expires 15d;
}

# A blog loads a lot of JavaScript and CSS and caches them locally in the browser for one day. After reading an article or page, looking at the content of another file or another page, you do not need to download the same JavaScript and CSS from the server again, improving the page display speed.
Location ~ . * \. (JS | CSS )? $
{
Expires 1D;
}

Log_format access' $ remote_addr-$ remote_user [$ time_local] "$ request "'
'$ Status $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" $ http_x_forwarded_for ';
Access_log/data1/logs/access. Log Access;
}
}

  Iii. Configuration Optimization of PHP 5.2.6 (FastCGI)
1. eacelerator optimization in the PHP. ini configuration file. Use only 1 MB of shared memory to delete all script caches that cannot be accessed within the last 3600 seconds, and cache them with disk assistance.

Reference [eaccelerator]
Zend_extension = "/usr/local/webserver/PHP/lib/PHP/extension/ no-debug-non-zts-20060613/eaccelerator. So"
Eaccelerator. shm_size = "1"
Eaccelerator. cache_dir = "/usr/local/webserver/eaccelerator_cache"
Eaccelerator. Enable = "1"
Eaccelerator. optimizer = "1"
Eaccelerator. check_mtime = "1"
Eaccelerator. DEBUG = "0"
Eaccelerator. Filter = ""
Eaccelerator. shm_max = "0"
Eaccelerator. shm_ttl = "3600"
Eaccelerator. shm_prune_period = "3600"
Eaccelerator. shm_only = "0"
Eaccelerator. Compress = "1"
Eaccelerator. compress_level = "9"
Eaccelerator. Keys = "disk_only"
Eaccelerator. Sessions = "disk_only"
Eaccelerator. content = "disk_only"

2. Configuration Optimization of PHP-fpm.conf
Modify two items. One is to modify the following line and change the number of started PHP-CGI processes from 128 to 5:

Reference <value name = "max_children"> 5 </value>

Second, modify the following line and change the TCP mode to the UNIX socket mode:

Reference <value name = "listen_address">/tmp/PHP-cgi.sock </value>

  Iv. MySQL 5.1.26 Configuration Optimization
1. MySQL 5.1 compiled and installed using the following parameters supports four storage engines by default: CSV, mrg_myisam, memory, and MyISAM. InnoDB Storage engines are not supported. Because the memory is limited and InnoDB consumes a large amount of memory, the MyISAM storage engine is recommended here.

. /Configure -- prefix =/usr/local/webserver/MySQL/-- enable-starter er -- With-extra-charsets = Complex -- enable-thread-safe-client -- With-big-tables -- With-Readline -- With-SSL -- With-embedded-server -- enable-local-infile
Make & make install

2. MySQL 5.1 configuration file (My. CNF) Optimization

Reference [client]
Port = 3306
Socket =/tmp/MySQL. Sock

[MySQL]
Prompt = "(\ U: s135 :) [\ D]>"
No-auto-rehash

[Mysqld]
User = MySQL
Port = 3306
Socket =/tmp/MySQL. Sock
Basedir =/usr/local/webserver/MySQL
Datadir =/usr/local/webserver/MySQL/Data
Open_files_limit = 600
Back_log = 20
Max_connections = 100
Max_connect_errorrs = 200
Table_cache = 60
External-locking = false
Max_allowed_packet = 16 m
Sort_buffer_size = 128 K
Join_buffer_size = 128 K
Thread_cache_size = 10
Thread_concurrency = 8
Query_cache_size = 0 m
Query_cache_limit = 2 m
Query_cache_min_res_unit = 2 K
Default_table_type = MyISAM
Thread_stack = 192 K
Transaction_isolation = read-uncommitted
Tmp_table_size = 512 K
Max_heap_table_size = 32 m
/Usr/local/webserver/MySQL/data/slow. Log
/Usr/local/webserver/MySQL/data/error. Log
Long_query_time = 1
Log_long_format
Server-id = 1
# Log-bin =/usr/local/MySQL/data/BINLOG
Binlog_cache_size = 2 m
Max_binlog_cache_size = 4 m
Max_binlog_size = 512 m
Expire_logs_days = 7
Key_buffer_size = 4 m
Read_buffer_size = 1 m
Read_rnd_buffer_size = 2 m
Bulk_insert_buffer_size = 2 m
Myisam_sort_buffer_size = 4 m
Myisam_max_sort_file_size = 10g
Myisam_max_extra_sort_file_size = 10g
Myisam_repair_threads = 1
Myisam_recover

[Mysqldump]
Quick
Max_allowed_packet = 16 m

Related Article

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.