Increasing PHP Script Execution Time with Nginx and Restricting Access with Amazon S3 Bucket Policies
Hatched by Felipe Soares Barbosa Silveira (Felipebros)
Jan 14, 2024
3 min read
12 views
Increasing PHP Script Execution Time with Nginx and Restricting Access with Amazon S3 Bucket Policies
Introduction:
When it comes to optimizing PHP script execution time and managing access to resources, Nginx and Amazon Simple Storage Service (S3) offer valuable solutions. In this article, we will explore how to increase PHP script execution time with Nginx and how to implement bucket policies in Amazon S3 to restrict access to specific HTTP indicators. By combining these two topics, we can enhance the performance and security of our web applications.
Increasing PHP Script Execution Time with Nginx:
Traditionally, in Apache, applications running PHP as a module above would have sufficed. However, when using Nginx, we need to make changes in three places: php.ini, PHP-FPM, and Nginx Config.
-
Changes in php.ini:
To increase the PHP script execution time, we need to modify the php.ini file. Locate the "max_execution_time" directive and set it to a higher value, such as 120 seconds. This allows PHP scripts to run for a longer duration before timing out. -
Changes in PHP-FPM:
Next, we need to make changes in the PHP-FPM configuration. Open the www.conf or php-fpm.conf file and locate the "request_terminate_timeout" directive. Increase its value to match the max_execution_time set in php.ini. This ensures that PHP-FPM does not terminate the script prematurely. -
Changes in Nginx Config:
Lastly, we need to configure Nginx to reflect the increased script execution time. Open the Nginx configuration file and locate the "fastcgi_read_timeout" directive. Set its value to match the max_execution_time set in php.ini. This ensures that Nginx waits for the PHP script to complete execution before closing the connection.
Restricting Access with Amazon S3 Bucket Policies:
Amazon S3 provides a flexible way to restrict access to specific resources using bucket policies. Let's explore an example of how to restrict access based on the HTTP referer.
Suppose we have a website with the domain name www.example.com or example.com, and we want to restrict access to photos and videos stored in our bucket named DOC-EXAMPLE-BUCKET. By default, all resources in Amazon S3 are private, accessible only by the AWS account that created them. However, we can add a bucket policy that grants read access to these objects for specific web pages.
Sources
Hatch New Ideas with Glasp AI 🐣
Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)
Start Hatching 🐣