How can I mount an S3 bucket to an EC2 instance and write to it with PHP?

How can I mount an S3 bucket to an EC2 instance and write to it with PHP?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Mounting S3 for PHP: Why FUSE Often Fails and What to Use Instead

You want your PHP application on an EC2 instance to write directly into an S3 bucket like static.example.com. The instinct is to treat that bucket as a local directory using a FUSE filesystem. You try s3fs, RioFS, or s3ql, only to hit a wall where shell commands succeed but PHP uploads silently fail or files simply disappear.

This pattern suggests a fundamental misunderstanding of how the S3 protocol works versus how local filesystems work. While mounting S3 is possible, relying on FUSE layers like s3fs introduces significant latency and permission hurdles that make them unsuitable for high-throughput web applications. The most robust solution is to bypass the filesystem abstraction entirely and interact with S3 directly via its REST API.

The Trap of FUSE Mounting

Tools like s3fs attempt to present S3 as a POSIX-compliant filesystem. You might see a command like this:

s3fs static.example.com -o use_cache=/tmp,allow_other /mnt/static.example.com

While this allows you to ls the directory and copy files via terminal commands, it fails when your PHP application attempts to write data. This isn't usually a bug in s3fs; it is a limitation of the translation layer. The FUSE interface translates HTTP requests into filesystem operations like write() and flush(). S3 does not support random writes or atomic file updates in the way a traditional disk does.

When you upload a file via PHP using functions like file_get_contents() combined with a PUT request, or frameworks that stream data to a temporary file before moving it, the FUSE layer often struggles with the metadata synchronization required by S3. You might successfully list files in the bucket after an upload, but the file content is corrupted or missing because the filesystem cache did not properly flush the data before closing the connection.

The Direct API Approach

The industry standard for serving static assets and user uploads from S3 is to stop treating it as a volume and start treating it as an object store. Instead of mounting /mnt/static.example.com, your PHP application should send HTTP requests directly to https://static.example.com.

For legacy PHP projects, the Amazon S3 PHP Class is a well-known solution. It allows you to instantiate an S3 object and use methods like $s3->putObject(). However, even with this class, you must ensure your bucket policy and CORS settings allow the specific IP address of your EC2 instance or the IAM role attached to it. If these permissions are missing, the request will fail at the network level before any filesystem logic occurs.

Modern Alternatives: Laravel and RioFS

If you are building with modern frameworks like Laravel, you should not rely on low-level S3 PHP classes at all. The official AWS SDK for PHP provides a streamlined interface that handles retries, authentication via IAM roles, and multipart uploads automatically.

Alternatively, if you absolutely require a mounted volume—for instance, to allow multiple processes to access the same directory structure without rewriting every file path—you can revisit RioFS. As noted in the source material, earlier versions of RioFS had issues where PHP writes did not propagate to S3, even though shell commands worked. That specific GitHub issue has since been resolved, making RioFS a viable candidate for scenarios where FUSE is required.

When using RioFS, ensure you include the allow_other flag in your mount command:

riofs -o allow_other http://s3.amazonaws.com static.example.com /mnt/static.example.com

However, keep in mind that RioFS still incurs the latency of translating POSIX calls to S3 requests. For a high-traffic application hosting files on EC2 behind an Elastic Load Balancer, direct API interaction is almost always more performant and easier to debug.

Implementation Trade-offs

The decision to mount or not depends on your architecture. If your PHP app needs to write to a single directory tree that multiple services must access concurrently, a FUSE mount provides a unified namespace. But if you can update your codebase to use object keys directly (e.g., user_123/avatar.jpg), the direct API approach removes the abstraction layer entirely.

Avoid using s3ql for web applications. It is designed more as a synchronization tool between local and remote storage rather than a real-time file serving solution, which explains why existing files in the bucket often do not appear in the mount immediately.

In summary, while mounting S3 adds complexity to your infrastructure setup, it is rarely the optimal choice for PHP web applications. Direct API integration or using an updated version of RioFS are the paths that offer stability and performance.

Our network