ubuntu

Ubuntu Apache配置如何优化PHP性能

小樊
42
2025-10-06 11:58:16
栏目: 编程语言

Optimize PHP Performance in Ubuntu with Apache

To enhance the performance of PHP applications running on Apache in Ubuntu, you need a combination of module tuning, configuration adjustments, and caching strategies. Below are actionable steps categorized for clarity:

1. Enable Essential Apache Modules

Ensure critical modules for PHP and performance optimization are enabled. Run the following commands:

sudo a2enmod rewrite deflate expires  # Enable URL rewriting, compression, and expiration headers
sudo systemctl restart apache2         # Apply changes

These modules reduce server load by compressing content, caching responses, and optimizing request routing.

2. Install and Configure OPcache

OPcache is a must for PHP performance—it caches precompiled script bytecode to eliminate redundant parsing.

OPcache can reduce PHP execution time by up to 50% for dynamic pages.

3. Adjust Apache MPM (Multi-Processing Module)

The MPM determines how Apache handles requests. For PHP, event or prefork (for non-thread-safe PHP) are recommended.

Proper MPM tuning prevents resource exhaustion and improves concurrency.

4. Optimize PHP Configuration

Adjust PHP settings in /etc/php/7.x/apache2/php.ini to balance performance and resource usage:

memory_limit = 128M  # Increase if your app needs more memory (e.g., 256M for heavy apps)
max_execution_time = 30  # Time (seconds) before script timeout (increase for long-running tasks)
upload_max_filesize = 50M  # Max file upload size (adjust per app needs)
post_max_size = 50M  # Max POST data size
display_errors = Off  # Disable in production to avoid exposing sensitive info
log_errors = On  # Log errors to a file
error_log = /var/log/php_errors.log  # Log file location

Restart Apache after changes:

sudo systemctl restart apache2

These settings prevent memory leaks and ensure stable operation under load.

5. Enable Gzip Compression

Compress text-based responses (HTML, CSS, JS) to reduce bandwidth usage. Add to your Apache virtual host or global config (/etc/apache2/apache2.conf):

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
</IfModule>

Restart Apache to apply:

sudo systemctl restart apache2

Gzip can reduce response sizes by 50–70%, improving page load times.

6. Use PHP-FPM for Better Process Management

PHP-FPM (FastCGI Process Manager) is more efficient than mod_php for handling concurrent PHP requests.

PHP-FPM improves scalability and reduces overhead for high-traffic sites.

7. Implement Caching Mechanisms

Reduce database load and speed up dynamic content with caching.

8. Monitor and Analyze Performance

Regularly monitor your server to identify bottlenecks:

By implementing these optimizations, you can significantly improve the performance of PHP applications on Apache in Ubuntu. Remember to test changes in a staging environment before applying them to production.

0
看了该问题的人还看了