Nginx 0.7.x + PHP 5.2.6 (FastCGI) + MySQL 5.1 configuration optimization on the 128M Small memory VPS Server

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

Nginx 0.7.x + PHP 5.2.6 (FastCGI) + MySQL 5.1 in 128M Small memory VPS Server Configuration Optimization Large | Medium | Small [2008-10-28 16:55 | by Zhang Banquet] [Article Zhang Banquet this version: v1.0 last modified: 2008.10.28 reproduced Please specify the original link: http://blog.zyan.cc/post/375/]

The VPS (full name Virtual private server) is the use of the latest virtualization technology to create multiple isolated virtual private hosts on a single physical server. They share hardware, software licenses, and management resources with maximum efficiency. For its users and applications, each VPS platform runs and manages exactly the same as a standalone host, since each VPS can be restarted independently and has its own root access, user, IP address, memory, process, file, application, System function library, and configuration file.

VPS Server The most important indicator is the size of the memory, multiple VPS server can share a CPU, but cannot share the same block of memory. The larger the memory, the more expensive the price.

Below, take the VPS of my blog as an example, introduced in 128M memory to Nginx 0.7.x + PHP 5.2.6 (FastCGI) + MySQL 5.1 optimization.

As for Nginx + PHP + MySQL installation configuration, see: "Nginx 0.7.x + php 5.2.6 (FastCGI) build 10 times times more than Apache Web Server (4th edition)"


   Optimized effect:

1 Nginx processes that provide HTTP services occupy 11M of physical memory, 5 php-cgi processes each occupy 8M or so of physical memory, 1 MySQL servers occupy 7M of physical memory, plus two memory-intensive nginx and php-cgi parent process, Nginx + PHP + The MySQL series consumes only 47.7% of the physical memory, or 62M of physical memory (128M * 47.7%≈62m).

  

In addition, the VPS server system itself and other programs will also use some memory, but 128M of memory VPS has enough. Overall, after optimized, 128M memory VPS running Nginx + PHP + MySQL effect is good. Of course, it would be much better if you had money to buy a VPS with bigger memory.


   the optimizations are as follows:

   one, the increase of 256M swap swap files
1. Create and activate swap swap files
cd/var/
DD If=/dev/zero of=swapfile bs=1024 count=262144
/sbin/mkswap Swapfile
/sbin/swapon Swapfile

2, add to the Fstab file to let the system boot automatically start
Vi/etc/fstab
Add the following at the end:
Reference/var/swapfile swap defaults 0 0
See: Http://blog.zyan.cc/post/374.htm


   Second, the Nginx 0.7.19 main configuration file (nginx.conf) optimization
reference user www www;


#Nginx每个进程耗费10M ~12m memory, only one nginx process is opened here, saving memory.
Worker_processes 1;

Error_log/data1/logs/nginx_error.log Crit;

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

#Specifies the value for maximum file descriptors the can is 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 32k;
Large_client_header_buffers 4 32k;

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 64k;
Fastcgi_buffers 4 64k;
Fastcgi_busy_buffers_size 128k;
Fastcgi_temp_file_write_size 128k;

#对网页文件, CSS, JS, XML, such as start gzip compression, reduce the amount of data transmission, improve access speed.
gzip on;
Gzip_min_length 1k;
Gzip_buffers 4 16k;
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 10m;

Server
{
Listen 80;
server_name blog.zyan.cc www.zyan.cc zyan.cc *.zyan.cc;
Index index.html index.htm index.php;
Root/data0/htdocs/blog;

#limit_conn crawler 20;

The rewrite static of 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/([0-9]+) _ ([0-9]+) _ ([0-9]+). htm$/read.php?$1&page=$2&part=$3 last;
Rewrite ^/index_ ([0-9]+) _ ([0-9]+). htm$/index.php?mode=$1&page=$2 last;
Rewrite ^/star_ ([0-9]+) _ ([0-9]+). htm$/star.php?mode=$1&page=$2 last;
Rewrite ^/category_ ([0-9]+). htm$/index.php?go=category_$1 last;
Rewrite ^/category_ ([0-9]+) _ ([0-9]+) _ ([0-9]+). 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_ ([0-9]+) _ ([0-9]+) _ ([0-9]+) _ ([0-9]+). htm$/index.php?go=archive&cm=$1&cy=$2&mode=$3 &page=$4 last;
Rewrite ^/showday_ ([0-9]+) _ ([0-9]+) _ ([0-9]+). htm$/index.php?go=showday_$1-$2-$3 last;
Rewrite ^/showday_ ([0-9]+) _ ([0-9]+) _ ([0-9]+] _ ([0-9]+) _ ([0-9]+). htm$/index.php?go=showday_$1-$2-$3&mode=$4 &page=$5 last;

Location ~. *\. (PHP|PHP5)? $
{
#将Nginx与FastCGI的通信方式由TCP改为Unix Socket. TCP is more stable than UNIX sockets under high concurrent access, but UNIX sockets are faster than TCP.
Fastcgi_pass Unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Include fcgi.conf;
}

Location ~/read.php
{
#将Nginx与FastCGI的通信方式由TCP改为Unix Socket. TCP is more stable than UNIX sockets under high concurrent access, but UNIX sockets are faster than TCP.
Fastcgi_pass Unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Include fcgi.conf;
}

#博客的图片较多, fewer changes, cache them locally in the browser for 15 days, can improve the next time you open my blog page load speed.
Location ~. *\. (gif|jpg|jpeg|png|bmp|swf) $
{
Expires 15d;
}

#博客会加载很多JavaScript, CSS, cache them locally in the browser for 1 days, visitors after reading an article or a page, then look at another file or another page of content, no need to download the same JavaScript, CSS from the server, improve 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;
}
}


   third, the configuration optimization of PHP 5.2.6 (FastCGI)
1, php.ini configuration file on the Eacelerator optimization. Use only 1M of shared memory, delete all script caches that are inaccessible in the last 3,600 seconds, and use disk-assisted caching.
References [Eaccelerator]
zend_extension= "/usr/local/webserver/php/lib/php/extensions/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, php-fpm.conf Configuration optimization
Modify the two items, one is to modify the following line, the number of php-cgi processes started from the original 128 changed to 5:
Reference <value name= "Max_children" >5</value>
The second is to modify the following line to change the TCP mode to UNIX socket mode:
Reference <value name= "Listen_address" >/tmp/php-cgi.sock</value>


   iv. MySQL 5.1.26 configuration Optimization
1. Using the following parameters to compile the installed MySQL 5.1 default support 4 storage engines: CSV, Mrg_myisam, memory, MYISAM, does not support InnoDB storage engine. The MyISAM storage engine is recommended here because memory is limited and InnoDB consumes large memory.
./configure--prefix=/usr/local/webserver/mysql/--enable-assembler--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
References [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_errors = 200
Table_cache = 60
external-locking = FALSE
Max_allowed_packet = 16M
Sort_buffer_size = 128K
Join_buffer_size = 128K
Thread_cache_size = 10
Thread_concurrency = 8
Query_cache_size = 0M
Query_cache_limit = 2M
Query_cache_min_res_unit = 2k
Default_table_type = MyISAM
Thread_stack = 192K
Transaction_isolation = read-uncommitted
Tmp_table_size = 512K
Max_heap_table_size = 32M
/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 = 2M
Max_binlog_cache_size = 4M
Max_binlog_size = 512M
Expire_logs_days = 7
Key_buffer_size = 4M
Read_buffer_size = 1M
Read_rnd_buffer_size = 2M
Bulk_insert_buffer_size = 2M
Myisam_sort_buffer_size = 4M
Myisam_max_sort_file_size = 10G
Myisam_max_extra_sort_file_size = 10G
Myisam_repair_threads = 1
Myisam_recover

[Mysqldump]
Quick
Max_allowed_packet = 16M

Nginx 0.7.x + PHP 5.2.6 (FastCGI) + MySQL 5.1 configuration optimization on the 128M Small memory VPS Server

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.