Tag: PHP development

  • Setting Up User-Specific PHP Configuration in Linux

    Overview

    PHP configuration is typically managed through the php.ini file and additional configuration files. While system-wide settings are stored in /etc/php/[version]/, you may want to add your own user-specific settings without modifying system files.

    This guide demonstrates how to set up user-specific PHP configuration that works alongside the system configuration.

    Default PHP Configuration Locations

    In most Linux distributions, PHP configuration files are located in:

    • Main configuration file: /etc/php/[version]/cli/php.ini (for command-line PHP)
    • Additional configuration directory: /etc/php/[version]/cli/conf.d/ (contains module-specific settings)

    You can check your current PHP configuration paths with:

    php --ini

    Creating User-Specific PHP Configuration

    Step 1: Create a Personal Configuration Directory

    Create a directory in your home folder to store your custom PHP settings:

    mkdir -p ~/php_config

    Step 2: Create Custom Configuration Files

    Create .ini files in your personal directory with your preferred settings. For example, to set your timezone:

    echo 'date.timezone = "America/Phoenix"' > ~/php_config/timezone.ini
    

    Other common settings you might want to customize in ~/php_config/php.ini:

    ; Memory limit
    memory_limit = 256M
    
    ; Maximum execution time
    max_execution_time = 120
    
    ; Error reporting
    error_reporting = E_ALL
    display_errors = On
    

    Step 3: Configure PHP to Use Your Settings

    To make PHP recognize your custom settings alongside the system settings, set the PHP_INI_SCAN_DIR environment variable:

    version=8.4
    export PHP_INI_SCAN_DIR=/etc/php/$version/cli/conf.d:~/php_config/
    

    Replace version=8.4 with your PHP version.

    Step 4: Make the Configuration Permanent

    Add the export command to your shell configuration file (.bashrc, .zshrc, or .profile):

    version=8.4
    echo "export PHP_INI_SCAN_DIR=/etc/php/$version/cli/conf.d:~/php_config" >> ~/.bashrc
    

    Then reload your shell configuration:

    source ~/.bashrc
    

    Verifying Your Configuration

    To verify that your custom settings are being applied:

    php -r "echo 'Current timezone: ' . date_default_timezone_get() . PHP_EOL;"
    

    You can also check all loaded configuration files:

    php --ini
    

    Benefits of User-Specific Configuration

    1. Non-destructive: Doesn’t modify system files
    2. Portable: Your settings follow your user account
    3. Isolated: Doesn’t affect other users
    4. Maintainable: Easier to track your custom settings
    5. Upgradable: System updates won’t overwrite your settings

    Important Notes

    • The main php.ini file is always loaded first
    • Settings in your custom files will override the same settings from the main configuration
    • Some settings can only be set in the main php.ini file and cannot be overridden
    • Web server PHP (Apache, Nginx) uses different configuration files than CLI PHP

    Troubleshooting

    If your settings aren’t being applied:

    1. Ensure the path in PHP_INI_SCAN_DIR is correct
    2. Check that your .ini files have the correct syntax
    3. Verify file permissions (should be readable)
    4. Restart your terminal session

    For web server PHP, you’ll need to restart the web server after changing PHP settings.

    PHP Configuration in Containerized Development Environments

    Containerized development environments like Lando and Local by Flywheel use isolated PHP configurations separate from your host system. The user-specific configuration approach described above won’t automatically apply to these environments.

    Lando Configuration

    Lando provides several ways to customize PHP settings:

    Option 1: Using .lando.yml Configuration

    services:
      appserver:
        config:
          php: path/to/custom/php.ini
    

    Option 2: Inline PHP Settings in .lando.yml

    services:
      appserver:
        config:
          php: |
            date.timezone = "America/Phoenix"
            memory_limit = 256M
            display_errors = 1
            error_reporting = E_ALL
    

    Option 3: Custom php.ini with Volume Mounting

    services:
      appserver:
        volumes:
          - ./php-custom.ini:/usr/local/etc/php/conf.d/custom.ini
    

    Local by Flywheel Configuration

    Local by Flywheel also offers ways to customize PHP:

    Option 1: Using the UI

    1. Open the site’s dashboard in Local
    2. Click on “Site” → “PHP”
    3. Modify PHP settings in the interface
    4. Restart the site for changes to take effect

    Option 2: Custom php.ini File

    You can add a custom php.ini file to your site’s configuration:

    1. Locate your site’s configuration folder (typically in Local’s site directory)
    2. Add a custom php.ini file
    3. Restart the site

    Best Practices for Containerized Environments

    1. Project-specific configuration: Store a standard php-custom.ini in your project repository
    2. Documentation: Include setup instructions in your project README
    3. Version control: Track PHP configuration changes alongside code changes
    4. Consistency: Ensure all team members use the same PHP settings

    This approach ensures consistent behavior across different development environments and makes it easier to onboard new team members.

  • Tracking Down Deprecated Functions

    You may be seeing warnings like this if you have WordPress Debug mode enabled:

    “Notice: wpdb::escape is deprecated since version 3.6! Use wpdb::prepare() or esc_sql() instead. in /home/USERNAME/public_html/wp-includes/functions.php on line 2913”

    I am getting this with WordPress version 3.8.1, and the Domain Mapping plugin wordpress-mu-domain-mapping version 0.5.4.3 but “deprecated” messages happen with many, many plugins.

    The wp-includes/functions.php is a WordPress core file. The function at the line mentioned is reporting the error, not the line with the error. To fix the error, you need to know where the error originates. I’m going to show you how to find that. (more…)

  • PHP Unzip bug makes WordPress Updates hang

    I have been having problems with WordPress Updates for a while. No error messages, just the WordPress Update, or WordPress Plugin Update, hangs.

    Then I found out that the WordPress backup plugin I am using, BackWPUp, failed if I configured it to use any compression.

    Then I found out that the problems I had been having with random-seeming 500 Server Errors was from the WP-Super-Cache plugin configured with, hmm, compression.

    WordPress Updates would create new folders, but no files in them.

    I wrote a PHP program, outside of WordPress, to test if the problem was in WordPress, or in the version of PHP used on my server.

    The problem is in PHP. The PHP unzip routines lock up. No error message on screen, in the PHP log, or in the system log.

    WordPress 3.6.0 and 3.6.1, Multi-Site, Apache, PHP Version 5.2.17, [HTTP_ACCEPT_ENCODING] => gzip, deflate

    PHP itself has a bug in Zip/Unzip in old versions. Hangs without any error message displayed or in logs. Affects WordPress Update, and Plugin/Theme Updates, and any plugins that use compression (e.g. WP-Super-Cache or BackWPup)

    PHP Change Log http://www.php.net/ChangeLog-5.php#5.3.4 says
    PHP Version 5.3.4 released 09-Dec-2010 – Fixed crash in zip extract method

    Note: PHP versions are numbered so version 5.3.10 is higher than 5.3.9 (5.3.1 is lower)

    (more…)

  • PHP Unzip bug makes WordPress Updates hang

    I have been having problems with WordPress Updates for a while. No error messages, just the WordPress Update, or WordPress Plugin Update, hangs.

    Then I found out that the WordPress backup plugin I am using, BackWPUp, failed if I configured it to use any compression.

    Then I found out that the problems I had been having with random-seeming 500 Server Errors was from the WP-Super-Cache plugin configured with, hmm, compression.

    WordPress Updates would create new folders, but no files in them.

    I wrote a PHP program, outside of WordPress, to test if the problem was in WordPress, or in the version of PHP used on my server.

    The problem is in PHP. The PHP unzip routines lock up. No error message on screen, in the PHP log, or in the system log.

    WordPress 3.6.0 and 3.6.1, Multi-Site, Apache, PHP Version 5.2.17, [HTTP_ACCEPT_ENCODING] => gzip, deflate

    PHP itself has a bug in Zip/Unzip in old versions. Hangs without any error message displayed or in logs. Affects WordPress Update, and Plugin/Theme Updates, and any plugins that use compression (e.g. WP-Super-Cache or BackWPup)

    PHP Change Log http://www.php.net/ChangeLog-5.php#5.3.4 says
    PHP Version 5.3.4 released 09-Dec-2010 – Fixed crash in zip extract method

    Note: PHP versions are numbered so version 5.3.10 is higher than 5.3.9 (5.3.1 is lower)

    (more…)

  • PHP Development on your laptop

    Setting up a PHP development environment

    Setting up my laptop to be a PHP/MySQL server was easier than I would have predicted. Download the XAMPP package from Apache Friends at http://www.apachefriends.org and unzip it to c:\xampp (keeping the folder hierarchy intact). There are Windows and Linux versions, with Mac OS X version being developed. I’m writing about the Windows version.

    Run c:\xampp\setup_xampp.bat and you have a functioning web server. Start it by loading c:\xampp\xampp_start.exe and you can load http://localhost/xampp/ in your web browser. The //localhost root is c:\xampp\htdocs

    To have Xampp run automatically, set it up as a Windows Service. Run c:\xampp\xampp-control.exe and put check marks next to Apache and MySQL.

    Xampp Control Panel


    Since any PHP developer is likely to develop multiple websites, make a subfolder of htdocs for each website. There are some simple changes to make so each subfolder works as a subdomain (i.e. html links to /images work as expected for the site). Edit your C:\xampp\apache\conf\extra\httpd-vhosts.conf adding lines like the following (substitute your folder name instead of lernerc):

    <VirtualHost local.lernerc.com:80>
    DocumentRoot “C:/xampp/htdocs/lernerc/”
    ServerName local.lernerc.com
    ServerAdmin youremail@yourserver.com
    <Directory “C:/xampp/htdocs/lernerc/”>
    Options Indexes FollowSymLinks
    AllowOverride FileInfo
    Order allow,deny
    Allow from all
    </Directory>
    </VirtualHost>

    If you have a site in c:\xampp\htdocs\ot, you would use lines like this:

    <VirtualHost local.ot.com:80>
    DocumentRoot “C:/xampp/htdocs/ot/”
    ServerName local.ot.com
    ServerAdmin youremail@yourserver.com
    <Directory “C:/xampp/htdocs/ot/”>
    Options Indexes FollowSymLinks
    AllowOverride FileInfo
    Order allow,deny
    Allow from all
    </Directory>
    </VirtualHost>

    Then you need to tell your operating system about new “domains”. In Windows, edit the C:\Windows\system32\drivers\etc\hosts file with lines like this:

    127.0.0.1 local.lernerc.com
    127.0.0.1 local.ot.com