Improving Server Performance with nginx and PHPapc - Blogging, Small Business, Web Design & Hosting Tips - A Small Orange

Improving Server Performance with nginx and PHPapc


Sometimes you need a bigger boat, and sometimes you need to make the boat you have better. While this isn’t exactly the take-away lesson from Jaws, it’s good advice for someone who wants to get the most out of their web hosting services. If you have a Cloud VPS plan or dedicated server with A Small Orange, we can help you get the most out of your server.

When you optimize a server, you generally want to maximize your use of your server’s faster components such as memory versus slower components such as hard drives. Here’s a brief guide to two of the most popular ways to maximize your server’s potential.

Use nginx (pronounced “engine-x”) to move static files into memory

nginxThe open source web server software nginx helps you optimize server performance by taking commonly-requested static files like images, static html, js, and css, and putting them into memory to serve them more quickly.

By incorporating nginx into your server software, you can reduce the need to access your server’s file system, taking a load off your disk I/O as well as saving CPU resources.
In this configuration, the risk of running into problems is relatively low and is usually related to DNS issues where a domain isn’t resolving properly. If your domain isn’t pointed to the server, such as a pre-production setup where you’re using a local hosts file modification to preview the site, nginx will break everything, so wait until your site is live before setting it up.
Additionally, nginx can be used to cache dynamic content. The risk of issues caused by such a configuration—which is not the default setting—increase dramatically, so if you’re considering such a setup proceed with caution.

For PHP Scripts, use PHPAPC (Alternative PHP Cache / Opcode Cache)

PHPWhile nginx helps you make your static files load faster, PHPAPC allows you to load and compile PHP scripts more quickly, which in turn will make your dynamic PHP-based web pages faster and more efficient.

Instead of loading the PHP script from disk and compiling it every time—thus demanding disk access and CPU cycles—it caches the compiled PHP opcodes to memory and executes them. This way, when you run the script, you’re only using CPU and other system resources for the execution, not to load and compile the script each time it is requested.

Additionally, PHP scripts which explicitly support PHPAPC can cache other objects into PHPAPC’s shared memory space, as well as things like database connections. For example, if you have a database-driven app which supports PHPAPC, you can keep one MySQL connection open and let PHPAPC “own” that connection. This way, every time the script runs it can use that MySQL connection instead of establishing a new one. Or, if you use the contents of a static file for something—like a configuration file—every time your script runs, you can cache those contents to PHPAPC and load it from memory via PHPAPC rather than having to load the file from disk.

In both cases, you’re spending memory to buy better page load times, less disk i/o, and less CPU usage. When installing PHPAPC on a Cloud VPS or dedicated server, you’ll need to fine tune it to get the maximum performance benefit. A Small Orange’s installer script rolls out a default config that is generally reasonable, but not always optimal, and errs on the side of keeping your web site up and running. Poor tuning of PHPAPC can cause the web server to start delivering errors instead of content, which is a big problem, so be careful when you customize your PHPAPC.

More Memory, Better Performance

With nginx and PHPAPC alike, you’re spending the memory that comes with your server package in order to purchase better performance such as improved page load times. If your server doesn’t have substantial free memory to work with, you’ll be doing more harm than good by implementing either or both of them, so please be aware of your available system resources to prevent overloading any component of your server. If you do decide you need a bigger boat—er, server—we’re happy to help you move up to the next level as well!

Keep in mind that our support team can help you install these on your VPS or Dedicated Server for no additional cost! Need to get in touch? Click here.

Photo By Brad Coy

Need hosting? Check out all our plans here.

Special thanks to ASO team member M. Harris for the info contained in this post!

  • tomcat fan

    I also use PHP APC and Nginx on a small RAM VPS. How to control memory allocaion of APC?

    • Hey tomcat fan,
      You can manage its memory allocation via the apc.shm_size variable in your php.ini file. By default, we scale this to your system’s total RAM to try and create a reasonable balance when installing phpapc for you, but oftentimes additional tuning is necessary to provide optimal performance.