Specifying PHP Versions for Websites and Scripts in ServBay
As a web developer, you may need to handle multiple projects that require different PHP versions or test your code for compatibility with specific PHP releases. ServBay, a powerful local development tool, offers flexible solutions for managing and switching between PHP environments. This article details how to specify the PHP runtime for your projects, individual PHP scripts, or entire websites within ServBay.
Assigning a Specific PHP Version to a Project
ServBay supports setting a PHP environment at the project level. By creating a file named .servbay.config
in your project’s root directory, you can define the PHP version used by that specific project.
Here’s an example of a .servbay.config
file specifying a PHP version:
PHP_VERSION=8.0
Note: The .servbay.config
file should be placed in the root directory of your project, which is typically the folder you designate as the website root in ServBay. For instance, if your site’s root is /Applications/ServBay/www/my-project
, your .servbay.config
file should be located at /Applications/ServBay/www/my-project/.servbay.config
.
For detailed instructions and usage, please refer to the official ServBay documentation on the .servbay.config file.
Specifying a PHP Version for Individual PHP Scripts
If you don’t want to lock the PHP version for the entire project or website but wish to temporarily run a script with a specific PHP version, ServBay provides a convenient command-line method.
Within the integrated ServBay CLI environment, you can use the php-<version>
command to run PHP scripts with your desired PHP version. This is especially useful for quickly testing how a script behaves under different versions of PHP.
For example, to execute a script named version.php
using PHP 5.6, you would use:
$ php-5.6 version.php
Hello World
PHP Version: 5.6.40
$ php-7.3 version.php
Hello World
PHP Version: 7.3.33
2
3
4
5
6
7
Explanation of the Example
php-5.6 version.php
: Explicitly runs theversion.php
script with PHP 5.6 as installed in ServBay.php-7.3 version.php
: Explicitly runs theversion.php
script with PHP 7.3 as installed in ServBay.
This approach allows you to efficiently test and run scripts for compatibility across PHP versions, without modifying your project or site-level configurations.
Assigning a PHP Version for an Entire Website
If you need to set a fixed PHP runtime version for an entire website (i.e., a virtual host configured in ServBay), you can do this easily via the ServBay graphical management interface.
This method is ideal when you want a specific site to always use a designated PHP version—such as a legacy project requiring an older PHP or a site undergoing a PHP migration for compatibility purposes.
Steps
Open the ServBay Management Interface:
Launch the ServBay application.Navigate to Website Settings:
In the ServBay side navigation menu, click on theWebsites
option.Select Your Specific Website:
From the list of websites, find the one you want to configure. In the corresponding row, click on thePHP Version
dropdown menu on the right.Choose the PHP Version and Save:
In the PHP version dropdown, select the version you wish the site to use. Make sure the selected version is already installed and running in ServBay. After selecting, click the save button (usually found at the top or bottom of the list).
Important Notes
- Ensure PHP Version is Installed and Running: Before assigning a PHP version to a website, check that it is installed and running in ServBay via the "Packages" feature. If it’s not installed or running, do so before proceeding.
- Compatibility Testing: After switching a website’s PHP version, it’s highly recommended to immediately visit the site and thoroughly test all key features to ensure all components and code work properly under the new PHP version.
- Website Root Directory: When configuring websites in ServBay, it’s best practice to store your site files under
/Applications/ServBay/www/
, creating a dedicated subfolder for each site—e.g.,/Applications/ServBay/www/servbay.demo
. The examples in this document follow this directory convention.
Conclusion
ServBay offers developers a range of flexible and convenient methods to manage and specify PHP runtime environments, including:
- Locking the PHP version for specific projects by creating a
.servbay.config
file in the project root. - Running individual scripts with a designated PHP version using the
php-<version>
command-line tool. - Assigning a fixed PHP version to an entire website through ServBay’s graphical user interface.
With this multi-layered PHP version control, ServBay enables you to effortlessly meet the needs of different projects—be it maintaining legacy systems, developing new features, or performing compatibility tests—all while significantly boosting local development efficiency and flexibility.