APACHE, PHP, AND MYSQL ARE AVAILABLE FOR MANY combinations of operating systems and web servers. In this appendix, we explain how to set up Apache, PHP, and MySQL on a few server platforms. We cover the most common options available for Unix and Windows Vista.
Key topics covered in this appendix include
Running PHP as a CGI interpreter or as a module
Installing Apache, SSL, PHP, and MySQL under Unix
Installing Apache, PHP, and MySQL under Windows
Testing that it’s working using phpinfo()
Considering other configurations
Our goal in this appendix is to provide you with an installation guide for a web server that will enable you to host multiple websites. Some sites, like those in the examples, require Secure Sockets Layer (SSL) for e-commerce solutions. And most are driven via scripts to connect to a database (DB) server and extract and process data.
Many PHP users never need to install PHP on a machine, which is why this material is in an appendix rather than Chapter 1, “PHP Crash Course.” The easiest way to get access to a reliable server with a fast connection to the Internet and PHP already installed is to simply sign up for an account at one of the thousands of hosting services or hosting service resellers around the globe.
Depending on why you are installing PHP on a machine, you might make different decisions. If you have a machine permanently connected to the network that you intend to use as a live server, performance will be important to you. If you are building a development server where you can build and test your code, having a similar configuration to the live server will be the most important consideration. If you intend to run ASP and PHP on the same machine, different limitations will apply.
Depending on your needs and your level of experience with Unix systems, you might choose to do a binary install or compile the programs directly from their source. Both approaches have their advantages.
A binary install will take an expert minutes and a beginner not much longer, but it will result in a system that is probably a version or two behind the current releases and one that is configured with somebody else’s choices of options.
A source install will take some time to download, install, and configure, and such an approach is intimidating the first few times you do it. It does, however, give you complete control. You choose what to install, what versions to use, and what configuration directives to set.
Most Linux distributions include a preconfigured Apache Web Server with PHP built in. The details of what is provided depend on your chosen distribution and version.
One disadvantage of binary installs is that you rarely get the latest version of a program. Depending on how important the last few bug fix releases are, getting an older version might not be a problem for you. The biggest issue is that you do not get to choose what options are compiled into your programs.
The most flexible and reliable path to take is to compile all the programs you need from their sources. This path will take a little more time than installing RPMs, so you might choose to use RPMs or other binary packages when available. Even if binary files are not available from official sources with the configuration you need, you might be able to find unofficial ones with a search engine.
Let’s install Apache, PHP, and MySQL under a Unix environment. First, you need to decide which extra modules you will load under the trio. Because some of the examples covered in this book use a secure server for web transactions, you should install an SSL-enabled server.
For purposes of this book, the PHP configuration is more or less the default setup but also covers ways to enable the gd2 library under PHP.
The gd2 library is just one of the many libraries available for PHP. We included this installation step so that you can get an idea of what is required to enable extra libraries within PHP. Compiling most Unix programs follows a similar process.
You usually need to recompile PHP after installing a new library, so if you know what you need in advance, you can install all required libraries on your machine and then begin to compile the PHP module.
Here, we describe installation on an SuSE Linux server, but the description is generic enough to apply to other Unix servers.
Start by gathering the required files for the installation. You need these items:
Apache (http://httpd.apache.org/)—The web server
OpenSSL (http://www.openssl.org/)—Open source toolkit that implements the Secure Sockets Layer
MySQL (http://www.mysql.com/)—The relational database
PHP (http://www.php.net/)—The server-side scripting language
ftp://ftp.uu.net/graphics/jpeg/—The JPEG library, needed for PDFlib and gd
http://www.libpng.org/pub/png/libpng.html—The PNG library, needed for gd
http://www.zlib.net/—The zlib library, needed for the PNG library, above
http://www.libtiff.org/—The TIFF library, needed for PDFlib
ftp://ftp.cac.washington.edu/imap/—The IMAP c client, needed for IMAP
If you want to use the mail()
function, you will need to have an MTA (mail transfer agent) installed, although we do not go through this here.
We assume that you have root access to the server and the following tools installed on your system:
gzip
or gunzip
gcc
and GNU make
When you are ready to begin the installation process, you should start by downloading all tar
file sources to a temporary directory. Make sure you put them somewhere with plenty of space. In our case, we chose /usr/src
for the temporary directory. You should download them as root to avoid permissions problems.
In this section, we show you how to do a binary install of MySQL. This type of install automatically places files in various locations. We chose the following directories for the remainder of our trio:
/usr/local/apache2
/usr/local/ssl
You can install the applications in different directories by changing the prefix option before installing.
Let’s begin! Become root by using su
:
$ su root
Then enter the user root’s password. Next, change to the directory where you have stored the source files. For example, use
# cd /usr/src
MySQL recommends that you download a binary of MySQL instead of compiling from scratch. Which version to use depends on what you want to do. Although MySQL prerelease versions are generally very stable, you may choose not to use them on a production site. If you are learning and experimenting on your own machine, you may choose to use one of these versions.
You should download the following packages:
MySQL-server-VERSION.i386.rpm
MySQL-Max-VERSION.i386.rpm
MySQL-client-VERSION.i386.rpm
(The word VERSION
is a placeholder for the version number. For whichever version you choose, make sure that you choose a matching set.) If you intend to run the MySQL client and server on this machine and to compile MySQL support into other programs such as PHP, you need all these packages.
Enter the following commands to install the MySQL servers and client:
rpm -i MySQL-server-VERSION.i386.rpm
rpm -i MySQL-Max-VERSION.i386.rpm
rpm -I MySQL-client-VERSION.i386.rpm
The MySQL server should now be up and running.
Now it’s time to give the root user a password. Make sure you replace new-password
in the following command with a password of your choice; otherwise, new-password
will be your root password:
mysqladmin -u root password 'new-password'
When you install MySQL, it automatically creates two databases. One is the mysql
table, which controls users, hosts, and DB permissions in the actual server. The other is a test DB. You can check your database via the command line like this:
# mysql -u root -p
Enter password:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| mysql |
| test |
+--------------------+
2 rows in set (0.00 sec)
Type quit
or q
to quit the MySQL client.
The default MySQL configuration allows any user access to the system without providing a username or password. This is obviously undesirable.
The final compulsory piece of MySQL housekeeping is deleting the anonymous accounts. Opening a command prompt and typing the following lines accomplish that task:
# mysql -u root -p
mysql> use mysql
mysql> delete from user where User='';
mysql> quit
You then need to type
mysqladmin -u root -p reload
for these changes to take effect.
You should also enable binary logging on your MySQL server because you will need it if you plan to use replication. To do this, first stop the server:
mysqladmin -u root -p shutdown
Create a file called /etc/my.cnf
to be used as your MySQL options file. At the moment, you need only one option, but you can set several here. Consult the MySQL manual for a full list.
Open the file and type
[mysqld]
log-bin
Save the file and exit. Then restart the server by running mysqld_safe
.
You should still be acting as root; if not, use su
to change back to root.
Before you can install PHP, you need to have Apache preconfigured so that it knows where everything is. (We come back to this topic later when setting up the Apache server.) Change back to the directory where you have the source code:
# cd /usr/src
# gunzip -c httpd-2.2.9.tar.gz | tar xvf -
# cd httpd-2.2.9
# ./configure --prefix=/usr/local/apache2
Now you can start setting up PHP. Extract the source files and change to its directory:
# cd /usr/src
# gunzip -c php-5.2.6.tar.gz | tar xvf -
# cd php-5.2.6
Again, many options are available with PHP’s configure
command. Use ./configure --help | less
to determine what you want to add. In this case, add support for MySQL, Apache, PDFlib, and gd.
Note that the following is all one command. You can put it all on one line or, as shown here, use the continuation character, the backslash (). This character allows you to type one command across multiple lines to improve readability:
# ./configure --prefix=/your/path/to/php
--with-mysqli=/your/path/to/mysql_config
--with-apxs2=/usr/local/apache2/bin/apxs
--with-jpeg-dir=/path/to/jpeglib
--with-tiff-dir=/path/to.tiffdir
--with-zlib-dir=/path/to/zlib
--with-imap=/path/to/imapcclient
--with-gd
Next, make and install the binaries:
# make
# make install
Copy an INI file to the lib
directory:
# cp php.ini-dist /usr/local/lib/php.ini
or
# cp php.ini-recommended /usr/local/lib/php.ini
The two versions of php.ini
in the suggested commands have different options set. The first, php.ini-dist
, is intended for development machines. For instance, it has display_errors
set to On. This makes development easier, but it is not really appropriate on a production machine. When we refer to a php.ini
setting’s default value in this book, we mean its setting in this version of php.ini
. The second version, php.ini-recommended
, is intended for production machines.
You can edit the php.ini
file to set PHP options. There are any number of options that you might choose to set, but a few in particular are worth noting. You might need to set the value of sendmail_path
if you want to send email from scripts.
Now it’s time to set up OpenSSL. It is what you will use to create temporary certificates and CSR files. The --prefix
option specifies the main installation directory:
# gunzip -c openssl-0.9.8h.tar.gz | tar xvf -
# cd openssl-0.9.8h
# ./config --prefix=/usr/local/ssl
Now make it, test it, and install it:
# make
# make test
# make install
Next, configure Apache for compiling. The configuration option --enable-so
enables the use of dynamic shared objects (DSO), and --enable-ssl
enables the use of the mod_ssl module. It is strongly advised that ISPs and package maintainers use the DSO facility for maximum flexibility with the server software. Notice, however, that Apache does not support DSO on all platforms.
# cd ../httpd-2.2.9
# SSL_BASE=../openssl-0.9.8h
./configure
--prefix=/usr/local/apache2
--enable-so
--enable-ssl
Finally, you can make Apache and the certificates and then install them:
# make
If you have done everything right, you will get a message similar to the following:
+---------------------------------------------------------------------+
| Before you install the package you now should prepare the SSL |
| certificate system by running the 'make certificate' command. |
| For different situations the following variants are provided: |
| |
| % make certificate TYPE=dummy (dummy self-signed Snake Oil cert) |
| % make certificate TYPE=test (test cert signed by Snake Oil CA) |
| % make certificate TYPE=custom (custom cert signed by own CA) |
| % make certificate TYPE=existing (existing cert) |
| CRT=/path/to/your.crt [KEY=/path/to/your.key] |
| |
| Use TYPE=dummy when you’re a vendor package maintainer, |
| the TYPE=test when you’re an admin but want to do tests only, |
| the TYPE=custom when you’re an admin willing to run a real server |
| and TYPE=existing when you’re an admin who upgrades a server. |
| (The default is TYPE=test) |
| |
| Additionally add ALGO=RSA (default) or ALGO=DSA to select |
| the signature algorithm used for the generated certificate. |
| |
| Use 'make certificate VIEW=1' to display the generated data. |
| |
| Thanks for using Apache & mod_ssl. Ralf S. Engelschall |
| [email protected] |
| www.engelschall.com |
+---------------------------------------------------------------------+
Now you can create a custom certificate. This option prompts you for location, company, and a couple of other things. For contact information, it makes sense to use real data. For other questions during the process, the default answer is fine:
# make certificate TYPE=custom
Now install Apache:
# make install
If everything goes well, you should see a message similar to this:
+--------------------------------------------------------+
| You now have successfully built and installed the |
| Apache 2.2 HTTP server. To verify that Apache actually |
| works correctly you now should first check the |
| (initially created or preserved) configuration files |
| |
| /usr/local/apache2/conf/httpd.conf
| |
| and then you should be able to immediately fire up |
| Apache the first time by running: |
| |
| /usr/local/apache2/bin/apachectl start
| |
| |
| Thanks for using Apache. The Apache Group |
| http://www.apache.org/ |
+--------------------------------------------------------+
Now it’s time to see whether Apache and PHP are working. However, you need to edit the httpd.conf
file to add the PHP type to the configuration.
httpd.conf
File: SnippetsLook at the httpd.conf
file. If you have followed the previous instructions, your httpd.conf
file will be located in the /usr/local/apache2/conf
directory. The file has the addtype
for PHP commented out. You should uncomment it at this time, so it looks like this:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
Now you are ready to start the Apache server to see whether it worked. First, start the server without the SSL support to see whether it comes up. Then check for PHP support and stop and start the server with the SSL support enabled to see whether everything is working.
Use configtest
to check whether the configuration is set up properly:
# cd /usr/local/apache2/bin
# ./apachectl configtest
Syntax OK
# ./apachectl start
./apachectl start: httpd started
If it worked correctly, you will see something similar to Figure A.1 when you connect to the server with a web browser.
Now you can test for PHP support. Create a file named test.php
with the following code in it. The file needs to be located in document root path, which should be set up, by default, to /usr/local/apache/htdocs
. Note that this path depends on the directory prefix that you chose initially. However, you could change this in the httpd.conf file
:
<?php phpinfo(); ?>
The output screen should look like Figure A.2.
In Apache 2.2, all you need to do to enable SSL is uncomment the rule for the httpdssl.conf file in httpd.conf.
Instead of this in httpd.conf:
# Include conf/extra/httpd-ssl.conf
the line should read:
Include conf/extra/httpd-ssl.conf
You can make numerous configuration changes in the httpd-ssl.conf file itself; check the Apache documentation at http://httpd.apache.org/docs/2.2/mod/mod_ssl.html.
Once configuration changes have been made, simply stop and start the server:
# /usr/local/apache2/bin/apachectl stop
# /usr/local/apache2/bin/apachectl start
Test to see whether it works by connecting to the server with a web browser and selecting the https
protocol, like this:
https://yourserver.yourdomain.com
Try your server’s IP address also, like this:
https://xxx.xxx.xxx.xxx
or
http://xxx.xxx.xxx.xxx:443
If it worked, the server will send the certificate to the browser to establish a secure connection. This makes the browser prompt you to accept the self-signed certificate. If it were a certificate from a certification authority your browser already trusts, the browser would not prompt you. In this case, we created and signed our own certificates. We didn’t want to purchase one right away because we wanted to ensure that we could get everything working properly first.
If you are using Internet Explorer or Firefox, you will see a padlock symbol in the status bar. This symbol tells you that a secure connection has been established. The icon used by Firefox is shown in Figure A.3; the icon is typically in the lower corner (right or left) of your browser.
To use PHP modules you installed as shared objects, you need to complete a few more steps.
First, copy the module you have built to the PHP extensions
directory, which is probably
/usr/local/lib/php/extensions
Then add the following line to your php.ini
file:
extension = extension_name.so
You will need to restart Apache after making changes to php.ini
.
With Windows, the installation process is a little bit different because PHP is set up either as a CGI (php.exe
) script or as a SAPI module (php5apache2_2.dll
). However, Apache and MySQL are installed in a similar fashion to the way they are installed under Unix. Make sure you have the latest operating system service patches applied to the machine before you begin the Windows installation.
If you have a slow network connection, you may prefer to use the versions from the CD, but they are likely to be a version or more out of date.
The following instructions were written using Windows Vista.
Begin by setting up MySQL. You can download the Windows Essentials *.msi
installation file from http://www.mysql.com. Double-click this file to begin the installation.
The first few screens of the wizard-style installation process will contain general information regarding installation and the MySQL license. Read these screens and click the Continue button to move through them. The first important choice you will encounter is the installation type—typical, compact, or custom. A typical installation will do the job, so leave the default item selected and click the Next button to continue installing.
When the installation is complete, continue to the MySQL Configuration Wizard to create a custom my.ini
file tailored to your particular needs. To continue to the MySQL Configuration Wizard, check the Configure MySQL Server Now check box and click the Finish button.
Select the appropriate configuration options presented on the several screens in the MySQL Configuration Wizard; consult the MySQL Manual at http://dev.mysql.com/doc/refman/5.0/en/index.html for detailed explanations of these options. When you have finished your configuration—which includes the addition of a password for the root
user—the wizard will start the MySQL service.
After the server has been installed, it can be stopped, started, or set to start automatically using the Services utility (found in Control Panel). To open Services, click Start and then select Control Panel. Double-click Administrative Tools and then double-click Services.
The Services utility is shown in Figure A.4. If you want to set any MySQL options, you must first stop the service and then specify them as startup parameters in the Services utility before restarting the MySQL service. The MySQL service can be stopped using the Services utility or using the commands NET STOP MySQL
or mysqladmin shutdown
.
MySQL comes with lots of command-line utilities. None of them are easy to get at unless the MySQL binary directory is in your PATH
. The purpose of this environment variable is to tell Windows where to look for executable programs.
Many of the common commands you use at the Windows command prompt, such as dir
and cd
, are internal and built into cmd.exe
. Others, such as format
and ipconfig
, have their own executables. Having to type C:WINNTsystem32format
would not be convenient if you wanted to format a disk. Having to type C:mysqlinmysql
to run the MySQL monitor also would not be convenient.
The directory where the executables for your basic Windows commands, such as format.exe
, reside is automatically in your PATH
, so you can simply type format
. To have the same convenience with the MySQL command-line tools, you need to add it.
Click Start and choose Control Panel. Double-click System and go to the Advanced tab. If you click the Environment Variables button, you will be presented with a dialog box that allows you to view the environment variables for your system. Double-clicking PATH allows you to edit it.
Add a semicolon to the end of your current path to separate your new entry from the previous one; then add c:mysqlin
. When you click OK, your addition will be stored in the machine’s registry. The next time you restart your machine, you will be able to type mysql
rather than C:mysqlinmysql
.
Apache 2.2 runs on most Windows platforms and offers increased performance and stability over the Apache 2.0 and Apache 1.3 versions for Windows. You can build Apache from source, but because not many Windows users have compilers, this section deals with the MSI installer version.
Go to http://httpd.apache.org and download the Windows binary of the current version of Apache 2.2. We downloaded the apache_2.2.9-win32-x86-openssl-0.9.8h-r2.msi
file. It contains the current version (within the 2.2 hierarchy) for Windows, plus OpenSSL 0.9.8h, without source code, packaged as an MSI file. MSI files are the package format used by the Windows installer.
Unless you have a really elusive bug or want to contribute to the development effort, it is unlikely that you will want to compile the source code yourself. This single file contains the Apache server ready to be installed.
Double-click the file you downloaded to start the process. The installation process should look familiar to you. As shown in Figure A.5, the installer looks similar to many other Windows installers.
The install program prompts you for the following:
The network name, server name, and administrator’s email address. If you are building a server for real use, you should know the answers to these questions. If you are building a server for your own personal use, the answers are not particularly important.
Whether you want Apache to run as a service. As with MySQL, setting it up this way is usually easier.
The installation type. We recommend the Complete option, but you can choose Custom if you want to leave out some components such as the documentation.
The directory in which to install Apache. (The default is C:Program FilesApache Software FoundationApache2.2.
)
After you choose all these options, the Apache server will be installed and started.
Apache listens to port 80 (unless you changed the Port, Listen, or BindAddress directives in the configuration files) after it starts. To connect to the server and access the default page, launch a browser and enter this URL:
http://localhost/
This should respond with a welcome page similar to the one shown in Figure A.1. If nothing happens or you get an error, look in the error.log
file in the logs
directory. If your host isn’t connected to the Internet, you might have to use this URL instead:
http://127.0.0.1/
This is the IP address that means localhost.
If you have changed the port number from 80, you will need to append :port_number
on the end of the URL.
Note that Apache cannot share the same port with another TCP/IP application.
You can start and stop the Apache service from your Start menu: Apache adds itself as Apache HTTP Server under the Programs submenu. Under the Control Apache Server heading, you can start, stop, and restart the server.
After installing Apache, you might need to edit the configuration files in the conf
directory. We look at editing the configuration file httpd.conf
when we install PHP.
To install PHP for Windows, begin by downloading the files from http://www.php.net.
Two files should be downloaded for a Windows installation. One is the ZIP file containing PHP (called something similar to php-5.2.6-Win32.zip
) and one is a collection of libraries (pecl-5.2.6-Win32.zip
or similar).
Begin by unzipping the ZIP file to the directory of your choice. The usual location is c:php
, and we use this location in the following explanation.
You can install the PECL libraries by unzipping the PECL file to your extensions directory. Using c:php
as your base directory, this will be c:phpext
.
Now follow these steps:
1. In the main directory, you will see a file called php.exe
and one called php5ts.dll
. You need these files to run PHP as a CGI. If you want to run it as a SAPI module instead, you can use the relevant DLL file for your web server: php5apache2_2.dll
in this case.
The SAPI modules are faster and easier to secure; the CGI version allows you to run PHP from the command line. Again, the choice is up to you.
2. Set up a php.ini
configuration file. PHP comes with two prepared files: php.ini-dist
and php.ini-recommended
. We suggest you use php.ini-dist
while you are learning PHP or on development servers and use php.ini-recommended
on production servers. Make a copy of this file and rename it php.ini
.
3. Edit your php.ini
file. It contains many settings, most of which you can ignore for the time being. The settings you need to change now are as follows:
Change the extension_dir
directive to point to the directory where your extension DLLs reside. In the normal install, this is C:PHPext
. Your php.ini
will therefore contain
extension_dir = c:/php/ext
Set the doc_root
directive to point at the root directory from which your web server serves. This is likely to be
doc_root = "c:/Program Files/Apache Software Foundation/Apache2.2/htdocs"
if you are using Apache.
Choose some extensions to run. We suggest at this stage that you just get PHP working; you can add extensions as needed. To add an extension, look at the list under “Windows Extensions.” You will see a lot of lines such as
;extension=php_pdf.dll
To turn on this extension, you can simply remove the semicolon at the start of the line (and do the opposite to turn it off). Note that if you want to add more extensions later, you should restart your web server after you have changed php.ini
for the changes to take effect.
In this book, you will use php_pdflib.dll
, php_gd2.dll
, php_imap.dll
, and php_mysqli.dll
. You should uncomment these lines. You may find that php_mysqli.dll
is missing. If so, add it as follows:
extension=php_mysqli.dll
Close and save your php.ini
file.
4. If you are using NTFS, make sure the user that the web server runs as has permission to read your php.ini
file.
You may need to edit one of Apache’s configuration files. Open the httpd.conf
file in your favorite editor. This file is typically located in the c:Program FilesApache Software FoundationApache2.2conf
directory. Look for the following lines:
LoadModule php5_module c:/php/php5apache2_2.dll
PHPIniDir "c:/php/"
AddType application/x-httpd-php .php
If you don’t see these lines, add them to the file, save it, and restart your Apache server.
The next step is to start your web server and test to ensure that you have PHP working. Create a test.php
file and add the following line to it:
<? phpinfo(); ?>
Make sure the file is in the document root directory (typically C:Program FileApache Software FoundationApache2.2htdocs
; then pull it up on the browser, as follows:
http://localhost/test.php
http://your-ip-number-here/test.php
If you see a page similar to the one shown in Figure A.2, you know that PHP is working.
PHP5 comes with the PHP Extension and Application Repository (PEAR) package installer. If you are using Windows, go to the command line and type
c:phpgo-pear
The go-pear
script asks you a few straightforward questions about where you would like the package installer and the standard PEAR classes installed and then downloads and installs them for you. (This first step is not required under Linux, but the rest of the installation is the same.)
At this stage, you should have an installed version of the PEAR package installer and the basic PEAR libraries. You can then simply install packages by typing
pear install package
where package
is the name of the package you want to install.
To get a list of available packages, type
pear list-all
To see what you have installed currently, try
pear list
To install the MIME mail package used in Chapter 30, “Building a Mailing List Manager,” type
pear install Mail_Mime
The DB package mentioned in Chapter 11, “Accessing Your MySQL Database from the Web with PHP,” must also be installed in the same way:
pear install MDB2
If you want to check for newer versions of any installed packages, use
pear upgrade pkgname
If the preceding procedure does not work for you for whatever reason, we suggest you try downloading PEAR packages directly. To do this, go to http://pear.php.net/packages.php.
From here you can navigate through the various packages available. For example, in this book, we use Mail_Mime. Click through to the page for this package and click Download Latest to get a copy. You need to unzip the file you have downloaded and put it somewhere in your include_path
. You should have a c:phppear
or similar directory. If you are downloading packages manually, we suggest you put the packages in the PEAR directory tree. PEAR has a standard structure, so we suggest you put things in the standard location; this is the place where the installer would put them. For example, the Mail_Mime package belongs in the Mail section, so in this example, we would place it in the c:phppearMail
directory.
You can set up PHP and MySQL with other web servers such as Omni, HTTPD, and Netscape Enterprise Server. They are not covered in this appendix, but you can find information on how to set them up at the MySQL and PHP websites, http://www.mysql.com and http://www.php.net, respectively.