Local PHP
Technique 1: XAMPP
The PHP 8 Solutions textbook offers a technique of running PHP locally in Chapter 2. The requirements Powers lists for a local test environment are:
- A web server
- PHP
- A database such as MySQL or MariaDB, and phpMyAdmin to administer it
Powers recommends using XAMPP, a package which installs Apache, MariaDB, PHP, and Perl. XAMPP is available for Windows, Linux, and Mac OS (Powers also recommends MAMP for Mac OS, which installs Apache, Nginx, PHP, and MySQL and is also available for Windows). Once a package has been installed, a location must be selected to store PHP files. A suitable location for XAMPP is in the document root under “C:\xampp\htdocs”. The web server’s document root for testing a PHP page is “http://localhost/”. Once installed, the PHP installation can be tested by running the phpinfo();
command. This can be done inside a new .php file or by clicking the Apache Admin button in the XAMPP control panel, then clicking the PHPInfo button. If installed correctly, this will display a long list of details and specifications on the current PHP installation. Once installed, PHP’s settings can be altered by editing the php.ini file, which contains all the details on PHP’s configuration on your computer.
Technique 2: WampServer
WampServer is another development environment utilizing Apache2, PHP, and MySQL, distributed for free under the GNU General Public License. When running, WampServer displays a small interface with options specific to the three main programs, as well as links to project files and local directories. There are also “Quick Admin” functions allowing the user to start, stop, or restart all services, as well as put the server online. WampServer’s site also contains open-source training in PHP and MySQL, and a user forum to submit questions and engage in discussions with other users.
Technique 3: PHP Built-In Web Server
PHP contains a built-in web server which can be run from the command line interpreter interface. The server looks for “index.php” or “index.html” in the directory in which PHP is running, unless -t
is used in the CLI to specify the document root. There are methods to add a php.ini file in order to change settings and configuration, as well as specify server ports to display the PHP script(s) in. A user only needs PHP installed to access the built-in server.
Summary: Pros and Cons
A local PHP environment is a useful tool for writing functional scripts and being able to view them without having to pay for a hosting service. While running PHP locally can be beneficial for testing purposes, it should not be used for large-scale development, as a public server’s PHP settings may differ from those on the local browser’s end. Pages may not display correctly, and small changes made to a project or website may take a lot of time to transfer to a live server from a local server. In short, local PHP is useful for small-scale testing and page-building, but public server-side PHP should be used for large projects and websites.
Sources
- Powers, David. PHP 8 Solutions: Dynamic Web Design and Development Made Easy. Apress Media LLC, 2022.
- Apache Friends – XAMPP
- MAMP & MAMP PRO
- WampServer
- PHP.net – Built-in web server
- dcode – Using the built-in PHP Development Server | Command Line Usage