Summary of optimization methods after lnmp environment installation and configuration

Source: Internet
Author: User

Although the lnmp Environment says that running php mysql is much better than running windows, if our resources are not enough, we also need to maximize the related optimization. I will summarize some methods below.

Lnmp Configuration Optimization

1. Based on the xen architecture, you can increase the swap partition size. My vps is openVZ, And the swap partition cannot be changed at will. PS. The host provider has provided M swap. Yes!

The Code is as follows: Copy code
Cd/var/
Dd if =/dev/zero of = swapfile bs = 1024 count = 262144
/Sbin/mkswap swapfile
/Sbin/swapon swapfile

Then let your own swap partition automatically load when the system starts:

The Code is as follows: Copy code
Vi/etc/fstab

Add the following content as appropriate:

The Code is as follows: Copy code
/Var/swapfile swap defaults 0 0

Ii. Nginx main configuration file (nginx. conf) optimization Nginx each process consumes 10 MB ~ MB memory, only one Nginx process is enabled, saving memory.

The Code is as follows: Copy code
Worker_processes 1;

Start gzip compression for webpage files, CSS, JS, and XML to reduce data transmission volume and increase access speed.

The Code is as follows: Copy code
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;

Also:

The Code is as follows: Copy code

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;
}

In fact, the above-mentioned automatic lnmp installation package has completed these optimizations!


Process optimization after lnmp Installation

After lnmp is installed, the process is optimized to better utilize the VPS performance.

Modify php-cgi
Vi/usr/local/nginx/conf/nginx. conf

Worker_processes 1 can be changed to 2 or 3. nginx itself is very powerful and does not need to start too many processes like apache to ensure its stable operation.

6/usr/local/php/etc/php-fpm.conf

Find <value name = "max_children"> and modify the number of PHP processes based on your memory capacity. Generally, 6 PHP processes are enabled for 128 of the memory, and 16 PHP processes are enabled for 256 of the memory. This ensures that no 502 error will occur in the case of PHP high concurrency.
After optimization, run the following command to restart lnmp.

/Root/lnmp restart


How Apache server processes work and Optimization Methods


Prefork:
If httpd-l lists prefork. c, you need to configure the following segments:
StartServers 5 # Number of httpd processes started when apache is started.
MinSpareServers 5 # minimum number of idle processes maintained by the server.
MaxSpareServers 10 # maximum number of idle processes maintained by the server.
MaxClients 150 # maximum number of concurrent connections.
MaxRequestsPerChild 1000 # the number of times each sub-process is killed after it is requested for service. 0 indicates no restriction. We recommend that you set it to 1000.
In this mode, five httpd processes are started after the server is started (six parent processes are added, which can be seen through the ps-ax | grep httpd command ). When a user connects, apache uses an idle process to serve the connection, and the parent process fork a sub-process. Until the idle process in the memory reaches MaxSpareServers. This mode is used to be compatible with earlier versions of programs. My default options during compilation.

Worker:
If httpd-l lists worker. c, you need to configure the following sections:
StartServers 2 # Number of httpd processes started when apache is started.
MaxClients 150 # maximum number of concurrent connections.
MinSpareThreads 25 # minimum number of Idle threads maintained by the server.
MaxSpareThreads 75 # maximum number of Idle threads maintained by the server.
ThreadsPerChild 25 # Number of threads produced by each sub-process.
MaxRequestsPerChild 0 # the number of times each sub-process is killed after it is requested for service. 0 indicates no restriction. We recommend that you set it to 1000.
This mode is used by threads to listen to customer connections. When a new client connects, one of the Idle threads accepts the connection. The server starts two processes at startup. The number of threads produced by each process is fixed (determined by ThreadsPerChild). Therefore, there are 50 threads at startup. When 50 threads are insufficient, the server automatically forks a process and generates 25 more threads.

Perchild:
If httpd-l lists perchild. c, you need to configure the following segments:
NumServers 5 # Number of sub-processes started at server startup
StartThreads 5 # Number of threads started when each sub-process starts
MinSpareThreads 5 # minimum number of Idle threads in the memory
MaxSpareThreads 10 # maximum number of Idle threads
MaxThreadsPerChild 2000 # maximum number of requests to each thread before exiting. 0 is unrestricted
MaxRequestsPerChild 10000 # the number of times each sub-process is fork again. 0 indicates no restriction.
In this mode, the number of sub-processes is fixed and the number of threads is not limited. When the client is connected to the server, Idle threads provide services. If the number of Idle threads is insufficient, the child process automatically generates threads to serve the new connection. This mode is used for multi-site servers.

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.