Environments and Deployment in PHP

Ideally, the files you’re editing when you’re writing your PHP program are not the same files your web server is reading when it is responding to user requests. Editing those “live” files directly can cause many problems:

  • Your users will immediately see errors if you save a file with a typo.
  • Bad guys may be able to access backup copies that your editor saves automati­cally.
  • You don’t have a good way to test changes before real users see them.

Avoid these problems by maintaining different environments—separate contexts where your code can run. At a minimum, you need a development environment and a production environment. The development environment is where you do your work and the production environment is where you run the code that real users interact with. A typical setup is that the development environment is on your own computer and the production environment is on a server in a data center or cloud hosting pro­vider such as Amazon Web Services or Google Cloud Platform.

Like the other aspects of software engineering discussed in this chapter, there are many ways of setting up different environments, moving code between environments, and managing all the different computers involved. These tools and techniques are typically not language-specific. There are things you can do in your PHP code, how­ever, to make it easier to run seamlessly in different environments.

The most important thing is to separate environment-specific configuration informa­tion from your code so the configuration can be swapped without changing the code itself. This information includes data such as database hostnames and login creden­tials, locations of log files, other filesystem paths, and verbosity of logging. Once this information is in a separate file, PHP gives you a few methods for getting it into your program.

The parse_ini_file() function turns the contents of a key=value config file (the same format that the PHP engine’s php.ini file uses) into an associative array. For example, consider the following configuration file:

;

; Comment lines in a config file start with semicolon

;

; Database Information

; Need quotes around the dsn value because of the = inside it

dsn=”mysql:host=db.dev.example.com;dbname=devsnacks”

dbuser=devuser

dbpassword=raisins

Example 14-1 reads that configuration file (assuming it’s been saved into config.ini) and uses the configuration data to establish a database connection.

Example 14-1. Reading a configuration file

$config = parse_ini_file(‘config.ini’);

$db = new PDO($config[‘dsn’], $config[‘dbuser’], $config[‘dbpassword’]);

In Example 14-1, the array returned by parse_ini_file() contains keys and values that correspond to each key= value line in config.ini. In a different environment with different database connection information, nothing has to change in the PHP program. The only thing necessary to establish the right connection is a new config.ini file.

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *