How to Optimize Apache, PHP, and MySQL Performance for a 1GB RAM VPS

How to Optimize Apache, PHP, and MySQL Performance for a 1GB RAM VPS


Managing a VPS with only 1GB of RAM requires careful optimization to ensure smooth performance and avoid issues like out-of-memory (OOM) errors. While 1GB of RAM is sufficient for running a single application, tuning your stack—Apache, PHP, and MySQL or MariaDB—can maximize performance. Below is a step-by-step guide to achieving this.

Optimizing Apache

Apache can consume significant memory, so adjustments to its configuration is critical. It is recommended to use the Multi-Processing Module (MPM) prefork for 1GB RAM servers.

For CentOS/RHEL

1. Open the Apache configuration file using a text editor:

nano /etc/httpd/httpd.conf

2. Add the following lines to the end of the file:

KeepAlive Off
<IfModule prefork.c>
    StartServers        6
    MinSpareServers     5
    MaxSpareServers     15
    ServerLimit         256
    MaxClients          256
    MaxRequestsPerChild 3000
</IfModule>

3. Restart the Apache service to apply changes:

service httpd restart

For Ubuntu/Debian

1. Open the Apache configuration file:

nano /etc/apache2/apache2.conf

2. Locate the mpm_prefork_module section or add these lines if missing:

<IfModule mpm_prefork_module>
StartServers 3
MinSpareServers 5
MaxSpareServers 15
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 3000
</IfModule>

3. Restart Apache:

service apache2 restart

Optimizing MySQL/MariaDB

To reduce MySQL’s memory footprint, modify the my.cnf file under the [mysqld] section.

Configuration Changes

Add or update the following parameters:

[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

File Location

  • CentOS/RHEL: /etc/my.cnf
  • Ubuntu/Debian: /etc/mysql/my.cnf

After making the changes, restart the MySQL service:

service mysql restart

Adding Swap Memory

For additional stability, consider adding swap memory to your VPS. Swap allows your server to handle temporary memory shortages more effectively.

Steps to Add Swap: How to Create and Add Swap Space on Linux (CentOS, Ubuntu, CWP, and VestaCP)

Optimizing PHP

1. Install Performance Modules

  • Opcache: A built-in PHP extension that caches compiled scripts.
  • Memcached: A memory caching system for reducing database load. Ensure your application supports it.

2. Configure PHP Settings

Limit PHP’s memory usage to avoid overloading your server. Update your PHP configuration file (e.g., /etc/php.ini) and set:

memory_limit = 128M

Utilizing Caching Systems

Most web applications benefit from caching mechanisms. For WordPress, install and configure a plugin like WP Super Cache to reduce server load by serving static content to users.

That’s It!

By carefully tuning Apache, MySQL, and PHP configurations, and leveraging caching and swap memory, you can optimize your 1GB RAM VPS for peak performance. Regular monitoring of server metrics will help fine-tune these settings over time.