现在很多入门的 VPS 只有 1GB Ram, 如果在 1GB RAM 的 VPS 安装 Apache, PHP 及 MySQL / MariaDB, 为僻免出现 out of memory 的情况, 可以使用以下方法进行优化。
Apache
建议对 Apache MPM worker 进行设定:
RHEL / CentOS:
开启 /etc/httpd/conf.conf
# vi /etc/httpd/httpd.conf
最档案最后加入以下内容:
|
1 2 3 4 5 6 7 8 9 |
KeepAlive Off <ifmodule prefork.c=""> StartServers 6 MinSpareServers 5 MaxSpareServers 15 ServerLimit 256 MaxClients 256 MaxRequestsPerChild 3000 </ifmodule> |
储存盘案后,重新启动 Apache:
# systemctl restart httpd
Ubuntu :
开启 /etc/apache2/apache2.conf
# vi /etc/apache2/apache2.conf
找到 ifmodule mpm_prefork_module 的段落,改成这样:
|
1 2 3 4 5 6 7 8 |
<ifmodule mpm_prefork_module=""> StartServers 3 MinSpareServers 5 MaxSpareServers 15 ServerLimit 256 MaxClients 256 MaxRequestsPerChild 3000 </ifmodule> |
储存盘案后,重新启动 Apache:
$ sudo systemctl restart apache2
MySQL / MariaDB
1GB Ram 的 VPS,建议修改 MySQL / MariaDB 的设定,开启 my.cnf (一般在 /etc/my.cnf 及 /etc/mysql/my.cnf),在 [mysqld] 加入以下内容:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[mysqld] symbolic-links=0 skip-external-locking key_buffer_size = 32K max_allowed_packet = 4M table_open_cache = 8 sort_buffer_size = 128K read_buffer_size = 512K read_rnd_buffer_size = 512K net_buffer_length = 4K thread_stack = 480K innodb_file_per_table max_connections=100 max_user_connections=50 wait_timeout=50 interactive_timeout=50 long_query_time=5 |
PHP
PHP 的优化可以限制使用内存上限,开启 php.ini:
# vi /etc/php.ini
找到 memory_limit,改成 128MB:
memory_limit = 128M
储存后需要重新启动 Apache.
另外,也可以用装 PHP memcached, 可参考本站另一篇文章:RHEL / CentOS 7 安装 PHP Memcached 缓存