Join Jon Peck for an in-depth discussion in this video Configuring PHP for development, part of Up and Running with Linux for PHP Developers.
Let's see how the development environment handles errors. Go to php, and copy errors.php, and paste it into the sandbox. Then, from a browser, navigate to errors.php. This file intentionally creates three PHP errors, a notice, warning, and a fatal error. However, right now it's just showing headings and no errors because PHP's configured to be relatively conservative and safe by default, and doesn't report errors to the user.
That's not very useful for development, so we'll need to make a few changes. Let's open up a terminal. Similar to Apache, PHP's configuration is also in etc. So, we'll change directory to etc/php5. Let's see what's in there. Three directories are shown. There are distinct configurations for PHP for web requests and the command line, in Apache2 and CLI, respectively. A third directory, mods-available, is similar to Apache's sites-available structure. Let's see what's in there, ls -la mods-available.
Each one of these files can be included in the configuration of PHP. Let's see how they're included by taking a look at the Apache2 configuration, ls -la apache2. There are two entries. Php.ini is the primary configuration file, but there's also a conf.d. What's in there? Ls -la apache2/conf.d. Symbolic links, similar to the Apache sites-enabled that we looked at earlier. While we can individually configure both the command line and Apache PHP, it's more logical to create a shared configuration.
To do that, we'll create a single file and include it for both configurations. Sudoedit /etc/php5/mods-available/custom.ini. A copy of this file is available in the conf folder in the exercise files. However, PHP configuration is important enough, I'm going to go through this one manually. We're going to start with some metadata used by Linux to describe what this configuration is for and what order it's included in.
So, start with a semi colon, Custom shared config. New line. Semicolon and space, priority equals 01. The default PHP error level is to show everything except deprecated and strict. Let's increase the verbosity to show every single error. So, error_reporting equals E_ALL. In a development environment, and it's also logical, to see what PHP errors exist in the browser.
By default, that's turned off, so let's turn it on. Display_errors equals On. Given that this is a development server, coding mistakes are going to be made, and it's useful to have a record of what went wrong, for debugging. Therefore, we're going to enable error logging to a file, error_log equals /var/log/php_errors.log. By default, the PHP error log truncates error messages to 1024 bytes.
I prefer to know everything that I did wrong, so we'll change it to log the entire problem, log_errors_max_len, for length, base equals 0. Let's increase PHP's memory limit to handle the requirements of modern frameworks. So, memory_limit equals 256M, for megs. PHP applications often allow uploads and by default, the upload size is set pretty low. Let's increase the maximum allowable upload size to 100 megabytes.
As a word of caution, this may be higher than your production environment, so be aware of what your host's limits are. Post_max_size equals 100M, and then upload_max_filesize equals 100M. When ready, press Ctrl+X to exit, Y to save, and then Enter. Now that the configuration's been created, let's enable it. Similar to Apache, there's a built-in command for enabling custom configurations. Php5enmod enables and php5dismod disables PHP modules.
Again, similar to Apache, we'll need to restart the Apache service after making a change to the PHP module configuration. Let's enable the custom module, sudo php5enmod custom. If you're curious to see what configurations are enabled, the php5query command -M will list them. We're also going to have to create the PHP error log file. To do that, we'll use a command named touch, which creates new file or updates the modified date on an existing file.
So, sudo touch /var/log/php_errors.log. This will create the file as root, but the web server doesn't have permission to write to root files. The chown command changes a file's owner and group. Group permissions on a file will allow anybody who's in the group to be able to access the file in question. Therefore, let's change the ownership of the PHP error file using chown. Right now, it's owned by root, so we'll need sudo to chown, and then we'll specify the www-data user, then colon, which will specify the group, as well.
So, a space, then finally, the file in question, which is /var/log/php_errors.log. While we're at it, let's add support for the internationalization extension and SQLite, which is used by a number of frameworks. We should also include support for encryption with mcrypt. So, sudo apt-get install php5-intl php5-sqlite, then php5-mcrypt.
Yes, I'd like to continue, and the packages are downloaded and installed. Support for encryption algorithms are added with mcrypt, which is installed but not enabled by default. So, sudo php5enmod mcrypt. Changes to the PHP configurations will only take place when the server is restarted. So, restart Apache, sudo service apache2 restart. Let's see what the difference is. Go back to the browser, then reload the errors page. This time, debugging information is shown onscreen, which is much more useful.
Going back to the terminal, let's see the logged errors. So, tail /var/log/php_errors.log. Three errors are displayed, along with the timestamp. Now that PHP is ready, let's configure MySQL.
The demonstrations are performed with the Ubuntu LTS distribution of Linux, but the skills taught here are also applicable to other Linux distributions. Every command is described in detail in context, and a comprehensive quick reference is provided for convenience.
- What is Linux, and why should I use it?
- What's a LAMP, and why does it matter?
- Creating and configuring a virtual machine
- Working with the Linux command line
- Configuring the servers, including Apache virtual hosts
- Building a development server dashboard
- Using PHP package managers like Composer and PEAR
- Installing Drupal, WordPress, and more on the server
- Self-hosting Git repositories, including a web interface
- Enhancing the server with debugging and profiling
- Exporting a virtual appliance to use on another machine
- Server troubleshooting techniques
Skill Level Beginner
Q: The pecl installation of uploadprogress fails, saying it is not a valid package archive. How can I install uploadprogress?
A: There is a bug in Ubuntu's pecl that was introduced after the course was recorded; the workaround command is "sudo pecl install -Z uploadprogress"
Q: Where can I get the exact versions of the software used in this course?
Q: In Windows, git operations ask for a password. Why?
A: Make sure that pagent (PuTTY agent) is running and has the private key loaded. See Chapter 3, movie 4 for details.