Resolving PHP Error 28: No Space Left in Ubuntu

PHP Error 28, often accompanied by the message "No Space Left," can be a frustrating issue to encounter on your Ubuntu server. This error typically arises due to the PHP session storage becoming full or insufficient server storage space. In this article, we'll guide you through the process of troubleshooting and resolving this common PHP error.

Understanding PHP Error 28

PHP Error 28 occurs when the server's available storage is exhausted or when PHP fails to automatically manage session files, resulting in an overflow of session data. When sessions are not deleted or cleared properly, it can lead to excessive storage usage, eventually causing the "No Space Left" error.

Let's dive into the steps required to resolve this issue and restore your PHP session functionality.

Step 1: Check Ubuntu Storage Space

Before you delve into resolving PHP Error 28, it's essential to determine whether your Ubuntu server has enough available storage space. This can be done using the following command:

df -h

Running this command will provide an overview of your server's disk usage and available space. Ensure that there is sufficient free space to address the error. If your server is running low on storage, consider freeing up space or expanding your storage capacity.

Step 2: Check Session Storage Usage

It's crucial to evaluate the amount of storage space being consumed by PHP session files. To do this, you can use the following command:

du -hsx /var/lib/php/sessions/ | sort -rh | head -n 40

This command will display the top 40 directories/files in the /var/lib/php/sessions/ directory by size, allowing you to identify any large or unnecessary session data.

If this step reveals that there's a significant amount of space being used for sessions, it's an indication that PHP may not be automatically clearing sessions as it should.

Step 3: Identify Excessive Session Space Usage

If the previous step showed a substantial amount of storage space consumed by PHP sessions, it suggests that PHP isn't effectively clearing old sessions, leading to excessive space usage. To address this issue, proceed to the following steps.

Step 4: Create a New Session Directory

To resolve the issue of excessive session storage, create a new directory for PHP sessions. You should give this new directory the same permissions as the original session directory. Here's how:

sudo mkdir /var/lib/php/new_session

After creating the new directory, use the following command to set the permissions:

sudo chmod -R 1733 /var/lib/php/new_session

Having a separate session directory will help ensure that sessions are managed more efficiently.

Step 5: Update PHP Configuration

Now, you need to locate and edit the php.ini configuration file to point PHP to the new session directory. Follow these steps:

  1. Locate the php.ini file on your server. Typically, it's located in the /etc/php/{PHP_VERSION}/cli/ directory for command-line PHP or /etc/php/{PHP_VERSION}/apache2/ for PHP used with Apache.
  2. Edit the php.ini file with your preferred text editor. For example:
  3. sudo nano /etc/php/{PHP_VERSION}/cli/php.ini
  4. Find the line that specifies the session save path, which may look like:
  5. session.save_path = "/var/lib/php/sessions"
  6. Change the path to your new session directory:
  7. session.save_path = "/var/lib/php/new_session"
  8. Save and exit the file.

This configuration change tells PHP to save session data in the new directory you created. It's an essential step in resolving the "No Space Left" error.

Step 6: Clear Session Files

After updating the php.ini configuration to use the new session directory, it's time to clear the existing session files from the old directory. You can accomplish this with the following command:

find /var/lib/php/sessions/ -type f -exec rm {} \;

This command will remove all session files in the old session directory. It may take some time to complete, depending on the number of files in that directory. Be patient and allow the process to finish.

Once this step is done, the old session files will be removed, freeing up space on your server.

Step 7: Adjust File Ownership

After clearing the old session files, it's important to ensure the correct ownership of the session directory. By default, it may be owned by the root user. To address this, add the www-data user to the session folder group. Execute the following command:

sudo chown $USER:www-data /var/lib/php/new_session

This command assigns ownership of the new session directory to your user and adds the www-data group. It's necessary for proper PHP session management and security.

Step 8: Restore Original PHP Configuration

With the new session directory in place and session files cleared, it's time to restore the original PHP configuration. This step involves updating the php.ini file to point back to the old session directory and restarting your PHP service. Here's how:

  1. Edit the php.ini file as before, typically located in /etc/php/{PHP_VERSION}/cli/ or /etc/php/{PHP_VERSION}/apache2/.
  2. Find the line specifying the session save path:
  3. session.save_path = "/var/lib/php/new_session"
  4. Change the path back to the old session directory:
  5. session.save_path = "/var/lib/php/sessions"
  6. Save and exit the file.
  7. Finally, restart your PHP service to apply the changes:
  8. sudo systemctl restart php{PHP_VERSION}-fpm

With this step, your PHP sessions will be configured to use the original directory once again, and your server should be back to normal operation without the "No Space Left" error.

Conclusion

PHP Error 28, "No Space Left," can be a troubling issue on your Ubuntu server, but with the right steps, you can resolve it and ensure that PHP session management is efficient and error-free. To recap, we've covered the following essential steps:

  1. Checking Ubuntu server storage to ensure there is enough space available.
  2. Evaluating session storage usage to identify the extent of the problem.
  3. Identifying excessive session space usage as a root cause of the error.
  4. Creating a new session directory with the appropriate permissions.
  5. Updating the PHP configuration to use the new session directory.
  6. Clearing session files from the old directory.
  7. Adjusting file ownership to ensure proper PHP session management.
  8. Restoring the original PHP configuration after clearing session files.

By following these steps, you can effectively troubleshoot and fix the "No Space Left" error, ensuring that your PHP applications run smoothly without storage-related interruptions.

Final Words

Managing PHP sessions and server storage is a crucial aspect of maintaining a healthy web application. When errors like PHP Error 28 arise, taking swift and appropriate action is essential to keep your server running smoothly. We hope this guide has been helpful in resolving this issue, and we encourage you to regularly monitor and manage your server's resources to prevent such errors from occurring in the future.