Adriano Cataluddi
Adriano Cataluddi

January 25, 2023

Adriano’s ability to see the big picture comes from almost 30 years in IT, managing the widest range of scenarios. He has created and led sophisticated enterprise-level SaaS solutions for household names, and is frequently called on to translate underlying technical complexity for executive boards.

Most of the web’s server-side software is built on PHP. As of November 2022, according to W3Techs, “PHP is used by 77.5% of all the websites whose server-side programming language we know.” PHP is used in many popular web stacks like LAMP (Linux, Apache, MySQL, PHP) and LEMP (Linux, NGINX, MySQL, PHP).

It is also the basis for the most popular content management systems (CMS) like WordPress, Drupal, and Joomla. PHP also powers many important e-commerce platforms like Magento. If you’re using PHP as part of your business’s critical web stack, it’s important to know some AWS PHP best practices to get the most out of it.

Abracts graphic on blue background depicting best practices for AWS on PHP

Hosting your PHP application on a reputable cloud computing platform is a best practice in itself. According to Stack Overflow’s 2022 annual survey, the most popular cloud computing platform is Amazon Web Services (AWS).

In this article, we look at some other key best practices relating to the performance and development process of PHP applications on AWS.

Performance

Improving your website’s performance is critical if you want to keep users coming to your site. A page load time of even three seconds can be long enough for a user to leave your site. We explain best practices for improving performance below.

Optimize your AWS cloud environment with InfrAssure.

Hardware

When optimizing the performance of your PHP applications, it is best to start with the hardware. Be sure to update your AWS Elastic Compute Cloud (EC2) or Relational Database Service (RDS) instances to use Graviton2 processors.

These are high-performance processors developed by Amazon for use in their AWS cloud computing platform that work particularly well with modern versions of PHP.

Amazon worked closely with the PHP community while developing its Graviton2 processors, adding optimizations to the codebase of PHP 7.4 that give huge performance improvements when run on a Graviton2 processor.

This gives Graviton2-based instances running PHP 7.4 up to 37% faster execution times compared to those running PHP 7.3. Due to these significant improvements, it’s important to make sure that your AWS instances are using Graviton2 processors as well as the latest version of PHP.

Caching

If your site has heavy traffic, you will notice large performance improvements when you use caching. Caching means storing a copy of data in a place where it can be more quickly retrieved, saving time when accessing the data again.

Many different types of data can be cached, including site assets, web pages, database queries (known as object caching), and compiled code (known as opcode caching). Object caching and opcode caching, in particular, can deliver large performance benefits for PHP applications.

Opcode Caching

Opcode caching is beneficial because of how PHP scripts are executed. PHP scripts are stored on the server as scripts, not as executables. This means that every time the server tries to run a PHP script, it must first parse and compile the script into executable form.

These compiled PHP instructions are known as opcodes. This process becomes unnecessarily time-consuming when the same scripts are executed over and over by the server. An opcode cache stores the output of the PHP compilation (opcodes) in RAM on the server.

This way, the next time that the server tries to run the same PHP script, it can first check the cache and then skip the compilation process if the PHP opcodes are already stored in memory.

OPcache, Alternative PHP Cache (APC), and XCache are three popular PHP opcode caches. OPCache, in particular, is easy to use and is recommended by AWS. On production systems, we recommend using the parameters below.

# This should be around 10% of your available RAM
opcache.memory_consumption=512

# For really complex Magento websites, for example, you may need a huge number
# like this one for max_accelerated_files.
opcache.max_accelerated_files=60000
opcache.enable=1
opcache.validate_timestamps=0
opcache.enable_cli=1

The OPcache documentation provides good explanations of these parameters. Note that these parameters should only be used on production systems and not development systems because they would make debugging impossible.

Specifically, setting validate_timestamps to 0 prevents PHP from recompiling any cached scripts if the scripts have been edited. Scripts should never be changed on a production system, so checking for changed files is unnecessary.

During development, however, you frequently change your PHP scripts, so validating timestamps must be enabled to see any updates to your files.

Xdebug, a popular PHP debugging tool, should also be disabled on production systems, even though it can be extremely helpful during development.

Object Caching

Alongside opcode caching, object caching can also significantly improve application performance. Object caching reduces the number of database queries that your application makes by storing the results of previous queries in an in-memory cache on the server so they can be reused the next time the same query is made.

Redis and Memcached are the most popular object caches on the market, and both of these are supported by AWS ElastiCache, a managed caching service hosted by AWS. Object caching is not unique to PHP applications and should be used in all applications with significant database traffic.

Diagram highlighting AWS best practices for PHP: opcode cache vs. object cache
An opcode cache caches opcodes, which are compiled PHP; an object cache caches database queries.

PHP-FPM Tuning

In web applications, the key benefit of PHP is that content is dynamically generated from scripts rather than relying on static HTML files. This requires some protocol that allows the web server to execute a script and the script to pass the output HTML back to the web server.

This way, the web server can execute different PHP scripts depending on the incoming HTTP request from the client and send different dynamically generated HTML back to different clients.

One of the earliest popular protocols to allow web servers to interact with server-side scripts in this manner was the Common Gateway Interface (CGI). Over time, various improvements were made to CGI to speed up the execution of scripts.

One improvement, available through Apache modules like mod_php, creates a single long-running process to execute scripts rather than creating and destroying a new process for every request.

Other improvements like FastCGI use Unix sockets to execute the scripts on a different FastCGI server and pass the HTML back to the web server so that the web server can focus on handling client requests.

PHP FastCGI Process Manager (PHP-FPM) is a PHP-specific implementation of FastCGI, which allows sites with heavy loads to execute scripts more quickly.

You can make this process even faster by optimizing your PHP-FPM settings, as the default settings are not necessarily optimized for the best performance of your particular website. However, tuning your settings is typically a long and complex process.

For this reason, we’ve created a tool called PhpBoost, which can analyze your EC2 instance – whether it is standalone or part of an Auto Scaling group – for a certain amount of time and generate an output file with recommended PHP-FPM settings.

Other Applications

Other applications built with PHP can benefit from the same best practices, as well as best practices specific to those applications. In the articles below, we provide application-specific best practices for some popular PHP-based tools:

Development Process

After optimizing how the code is run, you can focus on optimizing the software development process and integration with AWS. We give some best practices specific to PHP applications below.

PHP Frameworks

Many developers and members of the PHP community recommend using a framework as a best practice, but we don’t completely agree. It’s true that outsourcing some architectural choices to your framework can allow you to focus purely on application logic, which increases development speed.

However, this may come at a performance cost later on, as the framework’s rigid structure might not be optimal for your use case.

This is why we recommend understanding any tradeoffs between performance and development speed and ensuring that you have flexibility in your architecture before committing to a framework.

Of course, choosing to maintain flexibility by not using a framework or using only a lightweight framework may come at the cost of development speed.

As a compromise, it might be beneficial to use a more flexible framework to quickly develop version 1 of your application, and later refactor your code to rely only on core components.

The AWS PHP SDK

After deciding on the architecture for your PHP application, you can begin integrating your application with different AWS services. The AWS PHP SDK allows your PHP codebase to communicate directly with different AWS services, such as Simple Email Service (SES), Identity and Access Management (IAM), and Amazon Simple Storage Service (S3).

S3 provides cloud-based file storage, storing files as objects in buckets that can be read by applications with a unique URL. AWS provides example code that shows how to use the AWS PHP SDK to read the filenames in an S3 bucket and display them in your application.

The AWS PHP SDK documentation code example page provides brief descriptions of the various services in the SDK, while the relevant code examples are hosted on its GitHub page.

Closing Thoughts

In this article, we covered some of the best practices for improving the performance and development process of your PHP applications hosted on AWS. Another key benefit of using AWS is it gives you the ability to scale your application relatively easily.

Logicata has extensive experience managing AWS cloud-based infrastructure, so please reach out if you have any questions about implementing some of these best practices for your PHP applications on AWS. You’re also welcome to book a free discovery call and we’ll talk through any questions or concerns you may have.

We provide AWS-managed services to businesses just like yours.