PHP is a server-side embedded scripting language. It is a server-side, cross-platform, object-oriented, HTML-embedded scripting language. This chapter will introduce readers to the establishment of a PHP development environment, common configuration problems and solutions. By studying this chapter, readers can learn the following contents.
Change verification code
: Windows - AppserV integrated installation package to build PHP environment
: Linux - XAMPP integrated installation package to build PHP environment
: Key configuration information of PHP development environment
: Solve common configuration problems of PHP
AppServ packages server software and tools such as Apache, PHP, MySQL and phpMyAdmin after they are installed and configured. Developers only need to download the software from the website and then install it to quickly build a PHP development environment. Very suitable for beginners.
Note: When using AppServ to build a PHP development environment, you must ensure that Apache, PHP and MySQL are not installed in the system. Otherwise, uninstall these software first and then apply AppServ.
The following explains the specific steps to build a PHP development environment using the AppServ integrated installation package:
(1) Double-click the AppServ-win32-2.5.10.exe file to open the AppServ startup page as shown in Figure 1.1.
(2) Click the Next button in Figure 1.1 to open the AppServ installation agreement page as shown in Figure 1.2.
(3) Click the I Agree button in Figure 1.2 to open the page shown in Figure 1.3. Set the installation path of AppServ (the default installation path is generally: E:AppServ). After the AppServ installation is completed, Apache, MySQL, and PHP will be stored in this directory in the form of subdirectories.
(4) Click the Next button in Figure 1.3 to open the page shown in Figure 1.4. Select programs and components to install (select all by default).
Note: In the operation steps in Figure 1.4, if the MySQL database is already installed on this machine, you can uncheck the MySQL Database option here and still use the MySQL database that already exists on this machine.
(5) Click the Next button in Figure 1.4 to open the page shown in Figure 1.5. Fill in the computer name, add an email address, and set the Apache port number. The default is port 80.
Tips: The setting of the Apache server port number is directly related to whether the Apache server can start normally. If port 80 on this machine is occupied by IIS or Thunder, then you need to modify the port number of Apache, or modify the port number of IIS or Thunder to complete the configuration of the Apache server. If a port conflict occurs, the installation will fail and the Apache service will not start.
(6) Click the Next button in Figure 1.5 to open the page shown in Figure 1.6. Set the login password and character set of the MySQL database root user.
Tip: To set the character set of MySQL database, you can choose UTF-8, GBK or GB2312. Here, the character set is set to "UTF-8 Unicode", which means that the character set of the MySQL database will use UTF8 encoding.
Note: The password for the MySQL database root user set in Figure 1.6 must be remembered because the program must use this password when connecting to the database.
When executing the program, you need to change the password to connect to the database. If you forget to set the password during installation, the most direct and effective solution is to reinstall AppServ.
(7) Click the Install button in Figure 1.6 to start the installation, as shown in Figure 1.7.
(8) After the installation is completed, you can start the Apache and MySQL services in the AppServ related operation list in the start menu, as shown in Figure 1.8.
The configuration method of the PHP development environment was introduced earlier. In addition to the installation steps themselves, the configuration of PHP and the server is also very important. The following will mainly introduce the configuration of PHP and Apache server.
The setting file of the Apache server is located in the /usr/local/apache/conf/ directory in the Linux operating system (located in "/etc/httpd/conf" in the Windows operating system). Basically, the following 3 configuration files are used to configure the behavior of the Apache server.
t access.conf: used to configure server access permissions and control access restrictions for different users and computers.
t httpd.conf: used to set the basic environment for server startup.
t srm.conf: Mainly used for setting file resources.
Tips: http.conf is the configuration file of the Apache server. Its commonly used configurations include: the port number of the Apache server, the access path of the server and pseudo-static settings.
ServerName localhost:80
DocumentRoot "/xampp/htdocs"
LoadModule rewrite_module modules/mod_rewrite.so
The php.ini file is a configuration file that PHP automatically reads when it starts. php.ini is an ASCLL text file, divided into multiple parts, each part includes related parameters. The name of each part is placed in the first square bracket, followed by the name and number, each name on its own line. Use regular PHP code, which is very sensitive to parameter names and cannot contain spaces, but parameters can be numbers, strings or Boolean logical numbers. A semicolon is placed at the beginning of each line as a designation mark, which makes it easy to choose to use or not use these features of PHP without having to delete the line. Commenting out a feature (that is, adding a semicolon) will not compile and execute the line. Each time you modify the php.ini file, you must restart the Apache server for the new settings to take effect.
Tips: php.ini is the PHP configuration file, used to load various function libraries, set error levels, set server time, etc. In the Linux operating system, php.ini is stored in the /opt/lampp/etc/php.ini folder, while in the Windwos operating system, php.ini is stored in the windows file on the system disk. The basic configuration of the php.ini file is shown in Table 1.1.
Table 1.1 Basic configuration of php.ini file
|
Description | Default value | |||||||||||||||||||||||||||||||||
error_reporting | Set the level of error handling. Recommended values are E_ALL & ~E_NOTICE & ~E_STRICT, which displays all error messages except reminders and encoding standardization warnings. | E_ALL & ~E_NOTICE & ~E_STRICT | |||||||||||||||||||||||||||||||||
register_globals | Normally, this variable can be set to Off, which can provide more secure protection against script attacks through forms | register_globals = On | |||||||||||||||||||||||||||||||||
include_path | Set the PHP search path. This parameter can receive a series of directories. When PHP encounters a file prompt without a path, it will automatically detect these directories. It should be noted that when some options allow multiple values, the system list separator should be used. Under Windows, use the semicolon ";" Use colon ":" under Linux | ; UNIX: "/path1:/path2" ;include_path = ".:/php/includes" ; Windows: "path1;path2" ;include_path = ".;c:phpincludes" | |||||||||||||||||||||||||||||||||
extension_dir | Specify the directory of PHP’s dynamic link extension library | Under the "ext" directory | |||||||||||||||||||||||||||||||||
extension | Specify the dynamic link extension library loaded when PHP starts. Please refer to Table 1.2 for common PHP extension libraries and their descriptions. | PHP’s common extension libraries are commented after the initial installation and configuration, and readers need to manually change them | |||||||||||||||||||||||||||||||||
file_uploads | Set whether to allow file upload via HTTP | file_uploads=On | |||||||||||||||||||||||||||||||||
upload_tmp_dir | Set the temporary directory when uploading files via HTTP. If it is empty, the system's temporary directory will be used | upload_tmp_dir=empty | |||||||||||||||||||||||||||||||||
upload_max_filesize | Set the size of files allowed to be uploaded, such as "50M", the unit must be filled in | upload_max_filesize=2M | |||||||||||||||||||||||||||||||||
post_max_size | Control the maximum capacity that PHP can receive during a form submission using the POST method. To upload larger files, this value must be greater than the value of upload_max_filesize. If upload_max_filesize=10M, then the value of upload_max_filesize must be greater than 10M | post_max_size = 8M | |||||||||||||||||||||||||||||||||
max_input_time | Limit the time of receiving data through POST, GET and PUT in seconds | max_input_time = 60 |
Table 1.2 PHP common extension libraries and their descriptions
|
Description | ||||||||||||||||||||||||||||||||||
php_ftp.dll | Supports FTP function library, which can implement standard transfer protocol (FTP) between client and server | ||||||||||||||||||||||||||||||||||
php_gd2.dll | Supports image processing function library, supports various image formats such as .gif, .jpg, .png, etc. | ||||||||||||||||||||||||||||||||||
php_imap.dll | Support imap email processing function library | ||||||||||||||||||||||||||||||||||
php_mssql.dll | Support MsSQL database | ||||||||||||||||||||||||||||||||||
php_msql.dll | Support mSQL database | ||||||||||||||||||||||||||||||||||
php_MySQL.dll | Support MySQL database | ||||||||||||||||||||||||||||||||||
php_oracle.dll | Support Oracle database | ||||||||||||||||||||||||||||||||||
php_pdf.dll | Support PDF file processing function library | ||||||||||||||||||||||||||||||||||
php_sockets.dll | Supports Sockets processing function library | ||||||||||||||||||||||||||||||||||
php_zlib.dll | Support zlib file compression function library | ||||||||||||||||||||||||||||||||||
php_pdo.dll | Supports PDO database abstraction layer | ||||||||||||||||||||||||||||||||||
php_pdo_mysql.dll | Support MySQL database | ||||||||||||||||||||||||||||||||||
php_pdo_mssql.dll | Supports MS SQL Server database | ||||||||||||||||||||||||||||||||||
php_pdo_oci8.dll | Support Oracle database | ||||||||||||||||||||||||||||||||||
php_pdo_odbc.dll | Support ODBC database | ||||||||||||||||||||||||||||||||||
php_pdo_pgsql.dll | Support PGSQL database |
Program running errors are the most troublesome problem for many programmers. The following introduces common configuration problems of PHP. By studying this section, you can distinguish which errors are caused by improper configuration of the PHP environment, thereby avoiding unnecessary waste of time and resources and completing the development of Web applications efficiently.
The default port number of IIS is 80, which is the same as the default port number of the Apache server. Since the same port number 80 is used, a conflict will occur when running the web page.
If IIS is installed on the user's machine, you need to modify the default port of IIS, otherwise the Apache server will not work properly. To change the default listening port 80 of IIS, you can set it in the IIS manager or stop the IIS service.
Users can also change the default port number when installing the Apache server, thereby solving the conflict problem caused by two servers sharing the same port number.
Tips: If you set the Apache port number to 82 when building a PHP environment, then when accessing the project through a browser, you should enter http://127.0.0.1:82/ or http://localhost: 82/.
Due to the rewriting of the date() function in PHP 5.0, the current date and time function is 8 hours less than the system time. The default setting in the PHP language is standard Greenwich Time (that is, the zero time zone is used), so to obtain the current local time, you must change the time zone setting in the PHP language. Here’s how:
In the php.ini file, find the ";date.timezone=" option under [date], modify it to "date.timezone =Asia/Hong_Kong", and then restart the Apache server.
After the setting is completed, there will be no time difference problem when outputting the current time of the system.
Adding a PHP extension module is also called a dynamic extension, which is used to dynamically load a module. It contains an instruction: extension.
Under the Windows operating system, the method of loading modules is as follows. Open the php.ini file, navigate to the following location, remove the semicolon in front of ;tension=php_java.dll, save it and restart the Apache server to complete the loading operation of the extension module.
;tension=php_java.dll
Under the Linux operating system, the method of loading modules is as follows.
extension=php_java.so
It should be noted that loading only this line of code does not necessarily enable the relevant expansion packs. Sometimes it is also necessary to ensure that the relevant software is installed in the operating system. For example: To enable java support, you need to install JDK.