DEV Community

CHENG QIAN
CHENG QIAN

Posted on

Performance Comparison of ThinkPHP 5.1 Applications on Nginx vs. Apache

Performance Comparison of ThinkPHP 5.1 Applications on Nginx vs. Apache

As PHP developers, we often face the decision of choosing a web server that best suits our application’s performance, scalability, and maintainability needs. For those using the ThinkPHP 5.1 framework—a popular PHP MVC framework in the Chinese developer community—it’s worth examining how it performs under two of the most widely used web servers: Nginx and Apache.

In this post, I’ll share benchmark results, configuration insights, and practical considerations when running ThinkPHP 5.1 on both Nginx and Apache, based on real-world testing in a controlled environment.

🔧 Test Environment
Server OS: Ubuntu 22.04 LTS
CPU: 4 vCPUs
RAM: 8 GB
PHP Version: 7.4 (with OPcache enabled)
ThinkPHP Version: 5.1.41
Web Servers:
Nginx 1.18.0 + PHP-FPM
Apache 2.4.52 + mod_php
Database: MySQL 8.0 (same dataset for both tests)
Load Testing Tool: Apache Bench (ab) and wrk
Test Endpoint: A typical controller action returning JSON data with moderate DB queries

Note: All tests were run after warm-up and repeated 3 times; averages are reported.

Key Observations

  1. Request Throughput
    Nginx consistently outperformed Apache in handling concurrent requests. Its event-driven, non-blocking architecture handles high concurrency more efficiently—especially beneficial for I/O-heavy ThinkPHP apps.

  2. Memory Efficiency
    Nginx + PHP-FPM consumes less memory per request compared to Apache’s prefork MPM with mod_php. This makes Nginx more scalable on resource-constrained servers.

  3. URL Rewriting & ThinkPHP Routing
    ThinkPHP 5.1 relies heavily on URL rewriting for its pathinfo-style routing (e.g., /index.php/index/hello).

Apache: Works out-of-the-box with .htaccess (provided mod_rewrite is enabled).
Nginx: Requires manual rewrite rules in the server block. Example:

location / {
    if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php?s=$1 last;
        break;
    }
}
Enter fullscreen mode Exit fullscreen mode

While slightly more configuration is needed for Nginx, the performance payoff is significant.

  1. Static Asset Handling
    Nginx serves static files (CSS, JS, images) faster and with lower overhead—ideal for ThinkPHP apps that include public assets.

  2. Ease of Deployment
    Apache’s .htaccess support simplifies shared hosting deployments, but in modern containerized or VPS environments, Nginx configurations are just as manageable—and more performant.

Recommendations
Choose Nginx if:
You expect high traffic or need better resource efficiency.
You’re deploying on cloud/VPS environments where you control the full stack.
Your app uses many static assets or APIs with concurrent users.
Choose Apache if:
You rely on .htaccess for per-directory config (e.g., shared hosting).
Your team is more familiar with Apache syntax and modules.
You need advanced .htaccess-based security or redirects without server-level access.
🔚 Conclusion
For most production-grade ThinkPHP 5.1 applications, Nginx paired with PHP-FPM delivers superior performance, lower memory usage, and better scalability compared to Apache with mod_php. While Apache remains a solid choice—especially in legacy or shared environments—modern deployments benefit greatly from Nginx’s architecture.

That said, always test with your specific workload. Application logic, database design, and caching strategies (e.g., Redis, Memcached) also heavily influence real-world performance.

Have you deployed ThinkPHP on Nginx or Apache? Share your experiences or optimization tips below! 👇

P.S. If you're looking for handy free utilities to assist with web development—like Base64 encoding, QR code generation, favicon creation, or text formatting—feel free to explore https://mantools.top/. It’s a lightweight, no-signup toolkit built for developers and creators. Hope it saves you a few minutes today! 😊

Top comments (0)