The server operating system I use is Ubuntu 12.04 64-bit LTS with 512M memory and no virtual memory enabled. After optimization, server memory generally occupies between 256m-378m, and stable operation. So, I should like to share my configuration process.
Determine if free memory and virtual memory are turned on
Before you start configuring your server, take a look at server memory usage. You can view this by using the following command:
Free-m
Of course, you can also use the following command to see where the memory is used (a list sorted by the size of each process memory)!
Ps-eo Pmem,pcpu,rss,vsize,args | Sort-k 1-r | Less
Turn off unused services and put lamp in a low memory footprint
Before shutting down unused services, we need to know which service items are not necessary. I found a boot service management software: sysv-rc-conf, diagram open operation interface, very practical. Its installation commands are as follows:
sudo apt-get install sysv-rc-conf
The following command is then used to open the Sysv-rc-conf graphical operation interface (how to operate it in the image interface), as follows
sudo sysv-rc-conf
Its graphical interface is as follows:
Configure Linux Startup applications with sysv-rc-conf
The services shown in the figure below are closed by me and you can retire to the actual situation:
SendMail: This is a mail service, because I am using 163 mailbox Stmp Service, so there is no need to open. If you need your server and can send mail via the PHP mail () function, do not close it.
Dns-clean: Clears DNS information when using dial-up Internet access. This is generally not available on the server. So directly shut off its startup.
For more information about Linux services, you can go to: Ubuntu Service optimization
Configure Apache
In memory footprint, Apache is the largest memory user. After installing the lamp environment, the default related configuration is not suitable for small memory service, so it is necessary to make some changes. I will use the following technical points to enable Apache to occupy a lower memory, to maintain the original speed.
Handling a small number of concurrent requests
Load the necessary modules
Log information necessary for logging
Reduce the number of Apache child processes
The Prefork mode is implemented by a multi-channel module (MPM) with a non threaded, pre-derived Web server. It has a strong self-regulation ability, as long as a very small configuration can be. The most important thing is to set the maxclients to a suitable value to handle the potential access spikes, but its value cannot be too large to exceed the physical memory size. The following are my configuration contents:
<ifmodule mpm_prefork_module>
Startservers 1
Minspareservers 1
Maxspareservers 3
MaxClients 15
Maxrequestsperchild 3000
</IfModule>
When Apache is started, Apache automatically creates startservers processes and tries to keep the number of idle processes between Minspareservers and maxspareservers. When the idle process is less than minspareservers, Apache creates the process in approximately 1s clock time, and when less than Maxspareservers, Apache deletes the extra idle process and frees the server resources. The maximum number of processes is determined by maxclients. Maxrequestsperchild is used to control how many requests are processed by each process and then automatically destroyed, if the value of this parameter is 0 for infinity (that is, the process is not destroyed).
Worker mode is a multi-channel module (MPM) that enables a network server to support mixed multithreaded multiple processes. Because of the threading process, a large number of requests can be processed while the system resources are less occupied than the process based MPM. My control finger configuration is as follows:
<ifmodule mpm_worker_module>
Startservers 1
Minsparethreads 5
Maxsparethreads 15
Threadlimit 25
Threadsperchild 5
MaxClients 25
Maxrequestsperchild 200
</IfModule>
The startservers directive sets the number of child processes that are established when the server is started. Minsparethreads the minimum number of idle processes. Maxsparethreads Configure the maximum number of idle threads. Threadlimit the maximum number of threads that can be configured per child process. MaxClients the maximum number of Access requests (maximum number of threads) that allow simultaneous servos. Threadsperchild the number of resident execution threads established by each child process. Maxrequestsperchild configures the maximum number of requests that each subprocess allows for the servo during its lifetime.
In addition to changing the above configuration, I changed the KeepAliveTimeout (connection lifetime) to adjust the value to 15 and the default value of 5.
Load only the necessary modules
In Apache configuration, a lot of unnecessary modules are loaded, we need to clean out unnecessary modules. You can check which modules your Apache has loaded by using the following commands:
Apache2ctl-m
The image below is the Apache-loaded module; Of course, yours may be different.
Default Apache Modules
In order to make WordPress normal operation, the following modules are essential.
LoadModule Dir_module modules/mod_dir.so
LoadModule Log_config_module modules/mod_log_config.so
LoadModule Mime_module modules/mod_mime.so
LoadModule Setenvif_module modules/mod_setenvif.so
LoadModule Alias_module modules/mod_alias.so
LoadModule Authz_host_module modules/mod_authz_host.so
LoadModule Rewrite_module modules/mod_rewrite.so
You can note out unnecessary modules to save memory, or turn the module on/off by following the command. Such as:
sudo a2enmod module_name #开启模块
sudo a2dismod module_name #关闭模块
Of course, after the change, you must overload the Apache service to take effect.
Sudo/etc/init.d/apache2 Reload
Less log processing
You can reduce log file information if you want to optimize performance to the fullest extent. On the server, I set the error log level to errors. In addition, if you can not record some of the statistics that do not care (such as: User-ageng, Http-referer).
ErrorLog ${apache_log_dir}/error.log
#
# Loglevel:control The number of messages logged to the Error_log.
# Possible values Include:debug, info, notice, warn, error, crit,
# Alert, Emerg.
#
LogLevel Error
This is how I deal with, the specific configuration depends on you.
Optimizing My SQL Service
Tuning my SQL memory footprint is easy to handle. I'll tell you why instead of telling you what to do. After reading the following, you will hopefully be able to do some optimization on your server. We can understand the following aspects of MySQL setup:
Event handling that can be turned off
Adjust the MySQL parameter configuration
Third-party configuration assistant using MySQL
Edit MySQL configuration file (/etc/mysql/my.conf) to optimize MySQL performance.
Event handling that can be turned off
MySQL allows us to specify different storage engines for the tables in the database, and the two most common types are InnoDB and MyISAM. These two storage engines differ in their specific performance as follows:
MyISAM supports table-level locking, which means that when data is to be written to a database table, the entire table is locked. This means that if there are other data to be written to the database table, it must wait until the last operation completes.
InnoDB provides row-level locking, which means that when you write data to a row in a table, only that row is locked and other rows in the table are writable.
Table-level locking is only shown when the database is operating at a high rate. For general Web sites, MyISAM is usually able to provide better performance, lower service costs.
If you decide to use only the MyISAM table, you must add the following configuration items to the MySQL configuration file:
Default-storage-engine=myisam
Default-tmp-storage-engine=myisam
If the tables in your database are MyISAM types, you can turn off the InnoDB engine to save memory. Please include the following in the MySQL configuration file:
Skip-innodb
If your database used to be InnoDB, you can convert a InnoDB type of table to a MyISAM type using the following simple script.
#!/bin/bash
Mysqlcmd=mysql
For DB in ' Echo Show databases | $MYSQLCMD | Grep-v Database '; Todo
For table in ' Echo Show tables | $MYSQLCMD $db | Grep-v Tables_in_ '; Todo
Table_type= ' echo show create TABLE $table | $MYSQLCMD $db | Sed-e ' S/.*engine=\ ([[: Alnum:]\]\+\) [[: space:]].*/\1/' |grep-v ' Create Table ']
if [$TABLE _type = "InnoDB"]; Then
Mysqldump $db $table > $db. $table. sql
echo "ALTER TABLE $table ENGINE = MyISAM" | $MYSQLCMD $db
Fi
Done
Done
Adjust the MySQL parameter configuration
Here are a few parameters that allow you to adjust the MyISAM so that it can run faster and more stably.
Size of the index buffer
This could be one of the most critical configurations affecting MySQL memory footprint and performance. MySQL is always trying to put all the indexes into the index buffer, so this can be a huge performance boost. SQL Lookup is read directly from memory. You should allow your keywords to be put into the buffer, I can't say how much better to set, because I don't know how much free memory you have.
Cache of Lookup Results
If you make the same reference two consecutive times, MySQL does not need to make a second lookup. If you want to improve performance, you can meet your needs, but it is very consuming memory. So you have to set him up not high or low.
The following 3 parameters are used to set the MySQL query set cache size:
Query_cache_size
Query_cache_limit
Query_cache_type
Maximum number of connections
About the maximum database connection is related to the number of processes in Apache. So if you have a limit on Apache, it shouldn't be much of a deal.
Table Caching
Whenever you visit a table, MySQL always loads a reference table in the cache table as a portal. This has a great performance boost for concurrent requests and a slight increase in memory footprint. You can keep adding tables to the cache, but the operating system can open at the same time a limited number of files, please keep this in mind. If you set the table cache too low, my SQL also strikes, and you should not want to.
Here's what I do with my SQL configuration for the Lightweight cloud server:
[Mysqld]
Port = 3306
Socket =/var/lib/mysql/mysql.sock
Skip-locking
Key_buffer = 16K
Max_allowed_packet = 1M
Table_cache = 4
Sort_buffer_size = 64K
Read_buffer_size = 256K
Read_rnd_buffer_size = 256K
Net_buffer_length = 2K
Thread_stack = 64K
# for the low memory, InnoDB should is used so keep skip-innodb uncommented unless required
Skip-innodb
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir =/var/lib/mysql/
#innodb_data_file_path = Ibdata1:10m:autoextend
#innodb_log_group_home_dir =/var/lib/mysql/
#innodb_log_arch_dir =/var/lib/mysql/
# you can set ... _buffer_pool_size up to 50-80%
# of RAM But beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set ... _log_file_size to% of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
[Mysqldump]
Quick
Max_allowed_packet = 16M
[MySQL]
No-auto-rehash
# Remove The next comment character if you are not familiar with SQL
#safe-updates
[Isamchk]
Key_buffer = 8M
Sort_buffer_size = 8M
[Myisamchk]
Key_buffer = 8M
Sort_buffer_size = 8M
[Mysqlhotcopy]
Interactive-timeout
Third-party Configuration wizard for my SQL
I found Percona, who offers free MySQL configuration Wizard to keep your database server in top condition.
My SQL Service Monitoring
My SQL storage statistics determine the best value you use. In addition, there are two practical tools to easily read statistics and display them in a graphical format: tuning-primer.sh and mysqltuner.pl.
These scripts will control your database service, and in the end, he will give you a suggested report that you can refer to for some optimization on your server.
Optimizing PHP and its caching
PHP programs don't suddenly take up a lot of memory, so you don't have to worry about the memory footprint unless your application does. I've done some research on this and found some small tweaks to reduce PHP's footprint on server memory.
; Limit the memory to 40M should is fine for barebones WordPress
Memory_limit = 48M
realpath_cache_ttl=300
realpath_cache_size=1m
Optional PHP cache
Install the PHP buffers, such as alternative PHP Cache. The PHP cache can store the results of PHP execution so that you do not have to repeat the PHP script in duplicate data requests.
PECL Install APC
Add the following configuration to the php.ini:
[APC]
Extension=apc.so
Apc.enabled=1
Apc.shm_segments=1
32M per WordPress Install
apc.shm_size=128m
; Relative to the number of cached files (your may need to watch your stats for A/two to find out a good number)
apc.num_files_hint=7000
; Relative to the size of WordPress
apc.user_entries_hint=4096
; The number of seconds a cache entry is allowed to idle in a slot before APC dumps the cache
apc.ttl=7200
apc.user_ttl=7200
apc.gc_ttl=3600
; Setting this to 0 would give you the best performance, as APC would
; not have to check the IO for changes. However, you must clear
The APC cache to recompile already cached files. If you are still
;d eveloping, updating your site daily in Wp-admin, and running W3TC
; Set this to 1
Apc.stat=1
; This is must be 0, WP can have errors otherwise!
Apc.include_once_override=0
; only set to 1 while debugging
Apc.enable_cli=0
; Allow 2 seconds after a file is created before it are cached to prevent users from seeing Half-written/weird pages
apc.file_update_protection=2
; Leave at 2M or lower. WordPress does ' t have any file sizes close to 2M
Apc.max_file_size=2m
; Ignore files
Apc.filters = "/var/www/apc.php"
Apc.cache_by_default=1
Apc.use_request_time=1
Apc.slam_defense=0
APC.MMAP_FILE_MASK=/VAR/WWW/TEMP/APC. Xxxxxx
Apc.stat_ctime=0
Apc.canonicalize=1
Apc.write_lock=1
Apc.report_autofilter=0
Apc.rfc1867=0
Apc.rfc1867_prefix =upload_
Apc.rfc1867_name=apc_upload_progress
Apc.rfc1867_freq=0
apc.rfc1867_ttl=3600
Apc.lazy_classes=0
Apc.lazy_functions=0
Static caching
Another very good suggestion is to install an HTTP accelerator, such as varnish. It's really effective, and installing and configuring Varnis is a complex and lengthy topic, so it's no longer an issue.
Final results
I shared the server configuration so that I could get the best performance on the minimum configuration. I use the Ubuntu 12.04 LTS, LAMP, varnish, APC Cache, support 3 WordPress website, can guarantee the 1W person's visit amount per day of the blog. Let's look at Blitz.io's Test report:
Blitz-test-result
As you can see, when web site traffic reaches 42,735,587, only 0.23% user connections are timed out. Maybe there's something I can do, but I'm happy about it.
If you feel trouble following the above, you can try Puphpet or vagrant.