 |
Page 1 of 1
|
| Message |
|
ankit2
nOObee
Joined: 24 Mar 2007
Posts: 5
 405 Cash
|
 1.1 Build your own Database Driven Website using PHP & M
Before we get started, you need to gather together the tools you'll need for the job. In this first chapter, I'll guide you as you download and set up the two software packages you'll need: PHP and MySQL.
PHP is a server-side scripting language. You can think of it as a "plug-in" for your Web server that will allow it to do more than just send plain Web pages when browsers request them. With PHP installed, your Web server will be able to read a new kind of file (called a PHP script) that can do things like retrieve up-to-the-minute information from a database and insert it into a Web page before sending it to the browser that requested it. PHP is completely free to download and use.
To retrieve information from a database, you first need to have a database. That's where MySQL comes in. MySQL is a relational database management system, or RDBMS. We'll get into the exact role it plays and how it works later, but basically it's a software package that is very good at the organization and management of large amounts of information. MySQL also makes that information really easy to access with server-side scripting languages like PHP. MySQL is released under the GNU General Public License (GPL), and is thus free for most uses on all of the platforms it supports. This includes most Unix-based platforms, like Linux and even Mac OS X, as well as Windows.
If you're lucky, your current Web host may already have installed MySQL and PHP on your Web server. If that's the case, much of this chapter will not apply to you, and you can skip straight to the section called "If Your Web Host Provides PHP and MySQL" to make sure your setup is shipshape.
Everything we'll discuss in this book may be carried out on a Windows- or Unix-based server (note that from this point forward, I'll refer to all Unix-style platforms supported by PHP and MySQL, such as Linux, FreeBSD, and Mac OS X, with the collective name 'Linux'). The installation procedure will differ in accordance with the type of server you have at your disposal. The next few sections deal with installation on a Windows-based Web server, installation under Linux, and installation on Mac OS X. Unless you're especially curious, you need only read the section that applies to you.
Windows Installation
Installing MySQL
As I mentioned above, MySQL may be downloaded free of charge. Simply proceed to http://dev.mysql.com/downloads/ and choose the recommended stable release (as of this writing, it is MySQL 4.0). On the MySQL 4.0 download page, under the heading Windows downloads, select and download the release that includes the installer. After downloading the file (it's about 21MB as of this writing), unzip it and run the setup.exe program contained therein.
Once installed, MySQL is ready to roll (barring a couple of configuration tasks that we'll look at shortly), except for one minor issue that only affects you if you're running Windows NT, 2000, XP, or Server 2003. If you use any of those operating systems, you need to create a file called my.cnf in the root of your C: drive to indicate where you have installed MySQL.
To create this file, simply open Notepad and type these three lines:
[mysqld]
basedir = c:/mysql/
datadir = c:/mysql/data/
If you installed MySQL into a directory other than C:\mysql, replace both occurrences of c:/mysql in the above with the path to which you installed. Notice the use of forward slashes (/) instead of the usual backslashes (\) in the paths. For instance, on my system I edited the file to read as follows:
[mysqld]
basedir = d:/Program Files/MySQL/
datadir = d:/Program Files/MySQL/data/
Save the file as my.cnf in the root directory of C: drive.
Note: Notepad and File Name Extensions
Notepad is designed to edit text files, which normally have a file name extension of .txt. When you try to save a file with a different extension (e.g. my.cnf), Notepad will normally add a .txt extension to the end of the file name (my.cnf.txt) so that Windows will treat it as a text file.
To prevent this, simply put double quotes around the file name as you enter it in the Save As dialog box, as shown in Figure 1.1.
Figure 1.1. Save the File As .cnf in Notepad
If you don't like the idea of a MySQL configuration file sitting in the root of your C: drive, instead, you can name it my.ini and put it in your Windows directory (e.g. C:\WINDOWS or C:\WINNT if Windows is installed on drive C  .
MySQL will now run on your Windows NT, 2000, XP, or Server 2003 system! If you're using Windows 95, 98, or ME, this step is not necessary—MySQL will run just fine as installed.
Working with .cnf files in Windows
It just so happens that files ending in .cnf have a special meaning to Windows, so, even if you have Windows configured to show file extensions, the my.cnf file you created will still appear as simply my with a special icon. Windows actually expects these files to contain SpeedDial links for Microsoft NetMeeting.
Assuming you don't use NetMeeting (or at least, that you don't use its SpeedDial facility) you can remove this file type from your system, enabling you to work with these files normally:
Open the Windows Registry Editor (in Windows NT, 2000, XP, or Server 2003, click Start, Run…, and then type regedt32.exe to launch the editor; in Windows 9x/ME run regedit.exe instead).
Navigate to the HKEY_LOCAL_MACHINE\SOFTWARE\Classes branch of the registry, where you'll find a list of all the registered file types on the system.
Select the .cnf key and choose Edit, Delete from the menu to remove it.
Log out and log back in, or restart Windows for the change to take effect.
If you prefer not to mess with the file types on your system, you should still be able to open the file in Notepad to edit it as needed.
Just like your Web server, MySQL is a program that should be run in the background so that it may respond to requests for information at any time. The server program may be found in the bin subfolder of the folder into which you installed MySQL. However, to complicate matters, several versions of the MySQL server are available:
mysqld.exe This is the basic version of MySQL if you run Windows 95, 98, or ME. It includes support for all the advanced features, and includes debug code to provide additional information in the case of a crash (if your system is set up to debug programs). As a result of this code, however, the server might run a little slow, and generally I've found that MySQL is so stable that crashes aren't really a concern.
mysqld-opt.exe This version of the server lacks a few of the advanced features of the basic server, and does not include the debug code. It's optimized to run quickly on today's processors. For beginners, the advanced features are not a big concern. You certainly won't be using them while you complete the tasks in this book. This is the version of choice for beginners running Windows 95, 98, or ME.
mysqld-nt.exe This version of the server is compiled and optimized like mysqld-opt, but is designed to run under Windows NT, 2000, XP, or Server 2003 as a service. If you're using any of those operating systems, this is probably the server for you.
mysqld-max.exe This version is like mysqld-opt.exe, but contains advanced features that support transactions. You won't need these features in this book.
mysqld-max-nt.exe This version's similar to mysqld-nt.exe, in that it will run as a Windows service, but it has the same advanced features as mysqld-max.exe.
All these versions were installed for you in the bin directory. If you're running on Win9x/ME, I recommend you stick with mysql-opt for now—move to mysqld-max if you ever need the advanced features. On WinNT/2000/XP/2003, mysqld-nt is my recommendation. Upgrade to mysqld-max-nt when you need more advanced features.
Starting MySQL is also a little different under WinNT/2000/XP/2003, but this time let's begin with the procedure for Win9x/ME. If you're unfamiliar with the workings of the Command Prompt, check out my article Kev's Command Prompt Cheat Sheet to get familiar with how it works before you proceed further. Now, open an MS-DOS Command Prompt, proceed to the MySQL bin directory, and run your chosen server program:
C:\mysql\bin>mysqld-opt
Don't be surprised when you receive another command prompt. This command launches the server program so that it runs in the background, even after you close the command prompt. If you press Ctrl-Alt-Del to pull up the task list, you should see the MySQL server listed as one of the tasks that's active on your system.
To ensure that the server is started whenever Windows starts, you might want to create a shortcut to the program and put it in your Startup folder. This is just like creating a shortcut to any other program on your system.
On WinNT/2000/XP/2003, you must install MySQL as a system service. Fortunately, this is very easy to do. Simply open a Command Prompt (under Accessories in the Start Menu) and run your chosen server program with the --install option:
C:\mysql\bin>mysqld-nt --install
Service successfully installed.
This will install MySQL as a service that will be started the next time you reboot Windows. To start MySQL manually without having to reboot, just type this command (which can be run from any directory):
C:\>net start mysql
The MySQL service is starting.
The MySQL service was started successfully.
To verify that the MySQL server is running properly, press Ctrl-Alt-Del and open the Task List. If all is well, the server program should be listed on the Processes tab.
Source: http://www.sitepoint.com/artic...stallation
|
| Sat Mar 24, 2007 3:18 am |
|
 |
|
|
easydoers
Junior Developer
Joined: 09 Mar 2005
Posts: 40
1011 Cash
|
Installing PHP
The next step is to install PHP. At the time of this writing, PHP 5.0 has just been released, with numerous improvements over the previous version; however, PHP 4.3 has become well-established as the version of choice due to its track record of stability and performance. The procedures for installing these two versions are nearly identical. Although I'll focus primarily on installing PHP 5.0 in these pages, I'll note any significant differences if you happen to be working with PHP 4.3. All of the code in this book will work with both versions of PHP.
Download PHP for free from http://www.php.net/downloads.php. You'll want the PHP 5.x zip package under Windows Binaries; avoid the installer version if you can.
PHP was designed to run as a plug-in for existing Web server software such as Internet Information Services, Apache, Sambar or OmniHTTPD. To test dynamic Web pages with PHP, you'll need to equip your own computer with Web server software, so that PHP has something to plug into.
If you have Windows 2000, XP Professional (Windows XP Home Edition does not come with IIS), or Server 2003, then install IIS (if it's not already on your system): open Control Panel > Add/Remove Programs > Add/Remove Windows Components, and select Internet Information Services (IIS) from the list of components.
Now, if you're not lucky enough to have IIS at your disposal, you can use a free, third-party Web server like Apache instead. A feature-limited edition of IIS called "Personal Web Server" (PWS) was distributed on the Windows 98 Second Edition CD, and was available for earlier editions of Windows as well. While, technically, PHP can run on PWS, this Web server is somewhat unstable and has a great many known security holes. For these reasons, I highly recommend using Apache if an up-to-date version of IIS is not available for your Windows operating system. I'll give instructions for both options in detail.
First, whether or not you have IIS, complete these steps:
Unzip the file you downloaded from the PHP Website into a directory of your choice. I recommend C:\PHP and will refer to this directory from this point onward, but feel free to choose another directory if you like.
Find the files called php5ts.dll and libmysql.dll in the PHP folder and copy them to the system32 subfolder of your Windows folder (e.g. C:\WINDOWS\system32).
PHP 4.3
The first file is called php4ts.dll for PHP 4.3, and the libmysql.dll file is not needed.
Find the file called php.ini-dist in the PHP folder and copy it to your Windows folder. Once it's there, rename it php.ini.
Open the php.ini file in your favorite text editor (use WordPad if Notepad doesn't display the file properly). It's a large file with a lot of confusing options, but look for a line that begins with extension_dir, and set it so that it points to the ext subfolder of your PHP folder:
extension_dir = "C:\PHP\ext"
A little further down, you'll see a bunch of lines beginning with ;extension=. These are optional extensions, disabled by default. We want to enable the MySQL extension so that PHP can communicate with MySQL. To do this, remove the semicolon from the start of the php_mysql.dll line:
extension=php_mysql.dll
Even further down, look for a line that starts with session.save_path and set it to your Windows TEMP folder:
session.save_path = "C:\WINDOWS\Temp"
Save the changes you made and close your text editor.
Now, if you have IIS, follow these instructions:
In the Windows Control Panel, open Administrative Tools > Internet Information Services.
In the tree view, expand the entry labelled local computer, then under Web Sites look for Default Web Site (unless you have virtual hosts set up, in which case, choose the site to which you want to add PHP support). Right-click on the site and choose Properties.
Click the ISAPI Filters tab, and click Add…. In the Filter Name field, type PHP, and in the Executable field, browse for the file called php5isapi.dll in the PHP folder. Click OK.
PHP 4.3
For PHP 4.3, the file is called php4isapi.dll, and is located in the sapi subfolder of your PHP folder.
Can't click OK?
In older versions of Windows, the OK button may remain disabled even after you have used the Browse… button to fill in the Executable field. Simply make a small change to the value of the field using the keyboard and then reverse it to enable the button.
Click the Home Directory tab, and click the Configuration… button. On the Mappings tab, click Add. Again choose your php5isapi.dll file as the executable (note that the file type filter in the dialog is set to show .exe files only by default) and type .php in the extension box (including the .). Leave everything else unchanged and click OK. If you want your Web server to treat other file extensions as PHP files (.php3, .php4, and .phtml are common choices), repeat this step for each extension. Click OK to close the Application Configuration window.
Click the Documents tab, and click the Add… button. Type index.php as the Default Document Name and click OK. This will ensure that a file called index.php will be displayed as the default document in a given folder on your site. You may also want to add entries for index.php3 and index.phtml.
Click OK to close the Web Site Properties window. Close the Internet Information Services window.
Again, in the Control Panel under Administrative Tools, open Services. Look for the World Wide Web Publishing service near the bottom of the list. Right-click on it and choose Restart to restart IIS with the new configuration options. Close the Services window.
You're done! PHP is installed!
If you don't have IIS, you'll first need to install some other Web server. For our purposes, I'll assume you have downloaded and installed Apache server from http://httpd.apache.org/; however, PHP can also be installed on Sambar Server, OmniHTTPD, and others. I recommend Apache 1.3 for now, but if you want to use Apache 2.0, be sure to read the following sidebar.
PHP and Apache 2.0 in Windows
As of this writing, the PHP team continues to insist that support for PHP on Apache 2.0 is experimental only. There are a number of bugs that arise within PHP when it is run on an Apache 2.0 server and, on Windows especially, installation can be problematic. That said, many people (myself included!) are running PHP on Apache 2.0 quite successfully, and the bugs that do exist probably won't affect you if you're just setting up a low-traffic testing server.
The instructions below apply to both Apache 1.3 and Apache 2.0; however, it is possible that after configuring Apache 2.0 to use PHP, the server will fail to start. It is also possible that it will start, but that it will fail to process PHP scripts. In both cases, an error message should appear when you start Apache and/or in the Apache error log file.
This problem is caused by the fact that Apache 2.0 is a server still very much under development. With each minor release they put out, they tend to break compatibility with all server plug-in modules (such as PHP) that were compiled to work with the previous version. On Linux, this isn't such a big deal because people tend to compile PHP for themselves, so they simply recompile PHP at the same time they're compiling the new release of Apache and PHP adapts accordingly. Unfortunately, on Windows, where people are used to simply downloading precompiled files, the situation is different.
The php4apache2.dll file that is distributed with PHP will only work on versions of Apache 2.0 up to the one that was current at the time that version of PHP was released. So if you run into problems, the version of PHP you're using is probably older than the version of Apache you're using. This problem can often be fixed by downloading the very latest version of PHP; however, every time a new release of Apache 2.0 comes out, the current release of PHP will be incompatible until they get around to updating it.
Should you ever install a later version of Apache and break compatibility with the latest PHP build, you should be able to download a 'work-in-progress' version of PHP and grab only the files you need (those responsible for the PHP-Apache interface). Information about doing this can be found in the PHP bug database.
Once you've downloaded and installed Apache according to the instructions included with it, open http://localhost/ in your Web browser, to make sure it works properly. If you don't see a Web page explaining that Apache was successfully installed, then either you haven't yet run Apache, or your installation is faulty. Check the documentation and make sure Apache is running properly before you install PHP.
If you've made sure Apache is up and running, you can add PHP support:
On your Start Menu, choose Programs > Apache HTTP Server > Configure Apache Server > Edit the Apache httpd.conf Configuration File. This will open the httpd.conf file (choose Notepad if you don't have a text editor configured to edit .conf files).
All of the options in this long and intimidating configuration file should have been set up correctly by the Apache install program. All you need to do is add the following lines to the very bottom of the file:
LoadModule php5_module c:/php/php5apache.dll
AddModule mod_php5.c
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
Make sure the LoadModule line points to the appropriate file in the PHP installation directory on your system, and note the use of forward slashes (/) instead of backslashes (\).
Apache 2.0
If you're using Apache 2.0 or later, the LoadModule line needs to point to php5apache2.dll instead of php5apache.dll, and you must remove the AddModule line entirely.
PHP 4.3
For PHP 4.3, the file in the LoadModule line is called php4apache.dll (php4apache2.dll for Apache 2.0) and is located in the sapi subfolder of your PHP folder.
Next, look for the line that begins with DirectoryIndex. This line tells Apache which file names to use when it looks for the default page for a given directory. You'll see the usual index.html and so forth, but you need to add index.php to that list if it's not there already:
DirectoryIndex index.html ... index.php
ave your changes and close Notepad.
Restart Apache by restarting the Apache service in Control Panel > Administrative Tools > Services. If all is well, Apache will start up again without complaint.
You're done! PHP is installed!
With MySQL and PHP installed, you're ready to proceed to the section called "Post-Installation Setup Tasks".
Linux Installation
This section covers the procedure for installing PHP and MySQL under most current distributions of Linux. These instructions were tested under Fedora Core 2; however, they should work on other distributions such as Debian, SUSE, and Mandrake without much trouble. The steps involved will be very similar, if not identical.
As a user of one of the handful of Linux distributions available, you may be tempted to download and install packaged distributions of PHP and MySQL. Debian users will be used to installing software using the apt-get utility, while distributions like Fedora Core tend to rely on RPM packages. These prepackaged versions of software are really easy to install; unfortunately, they also limit the software configuration options available to you. If you already have MySQL and PHP installed in packaged form, feel free to proceed with those versions, and skip forward to the section called "Post-Installation Setup Tasks". If you encounter any problems, you can always return here to uninstall the packaged versions and reinstall PHP and MySQL by hand.
This section will assume that you have the Apache Web server installed on your machine already. If you don't, chances are that your distribution offers an easy way to install it (I have no objection to your using the packaged distributions of Apache). I recommend Apache 1.3 over Apache 2.0, as support for Apache 2.0 in PHP is still experimental, but I'll provide instructions for both versions here.
Building Apache Yourself
If you want to compile and install Apache by hand, the necessary downloads and ample installation instructions may be found at the Apache Website. To support the PHP installation instructions provided below, you will have to build Apache with shared module support. When you configure your copy of Apache prior to compiling it, make sure you include the --enable-module=so option.
Removing Packaged Software
Since many Linux distributions will automatically install PHP and MySQL for you, your first step should be to remove any old packaged versions of PHP and MySQL from your system. If one exists, use your distribution's graphical software manager to remove all packages with php or mysql in their names.
If your distribution doesn't have a graphical software manager, or if you didn't install a graphical user interface for your server, you can remove these packages from the command prompt. You'll need to be logged in as the root user to issue the commands to do this. Note that in the following commands, shell# represents the shell prompt, and shouldn't be typed in.
In Fedora Core, RedHat, or Mandrake, you can use the rpm command-line utility:
shell#rpm -e mysql
shell#rpm -e php
In Debian, you can use apt-get to remove the relevant packages:
shell#apt-get remove mysql-server
shell#apt-get remove mysql-client
shell#apt-get remove php4
shell#apt-get remove php5
If any of these commands tell you that the package in question is not installed, don't worry about it unless you know for a fact that it is. In such cases, it will be necessary for you to remove the offending item by hand. Seek help from an experienced user if you don't know how.
If the command(s) for removing PHP completed successfully (i.e. no error message was displayed), then you have just removed PHP from your Web server, and you should check that you haven't broken it in the process. To make sure Apache is still in working order, you should restart it without the PHP plug-in:
shell#apachectl graceful
If Apache fails to start up, you'll need to have a look through its configuration file, which is usually called httpd.conf and may be found in /etc/apache or /etc/httpd. Look for leftover commands that may be trying to load the PHP plug-in that you have just removed from the system. The Apache error log files may be of assistance in tracking these down if you can't find them. When you're finished, try restarting Apache again.
With everything neat and tidy, you're ready to download and install MySQL and PHP.
Source: http://www.sitepoint.com/artic...allation/2
|
| Sat Mar 31, 2007 2:10 am |
|
 |
easydoers
Junior Developer
Joined: 09 Mar 2005
Posts: 40
1011 Cash
|
Installing MySQL
MySQL is freely available for Linux from http://dev.mysql.com/downloads/. Download the recommended stable release (4.0 as of this writing). You should grab the Standard version under Linux (x86, libc6) in the Linux downloads section.
Once you've downloaded the program (it was about 15MB as of this writing), you should make sure you're logged in as root before proceeding with the installation, unless you want to install MySQL only in your own home directory. To begin, move to /usr/local (unless you want to install MySQL elsewhere for some reason) and unpack the downloaded file to create the MySQL directory (replace version with the full version of your MySQL download to match the downloaded file name on your system):
shell#cd /usr/local
shell#tar xfz mysql-version.tar.gz
Next, create a symbolic link to the mysql-version directory with the name mysql to make accessing the directory easier, then enter the directory:
shell#ln -s mysql-version mysql
shell#cd mysql
While you can run the server as the root user, or even as yourself (if, for example, you installed the server in your own home directory), the best idea is to set up on the system a special user whose sole purpose is to run the MySQL server. This will remove any possibility of someone using the MySQL server as a way to break into the rest of your system. To create a special MySQL user, you'll need to log in as root and type the following commands:
shell#groupadd mysql
shell#useradd -g mysql mysql
MySQL is now installed, but before it can do anything useful, its database files need to be installed, too. In the new mysql directory, type the following command:
shell#scripts/mysql_install_db --user=mysql
By default, MySQL stores all database information in the data subdirectory of the directory to which it was installed. We want to ensure that nobody can access that directory except our new MySQL user. Assuming you installed MySQL to the /usr/local/mysql directory, you can use these commands:
shell#cd /usr/local/mysql
shell#chown -R root .
shell#chown -R mysql data
shell#chgrp -R mysql .
Now everything's set for you to launch the MySQL server for the first time. From the MySQL directory, type the following command:
shell#bin/mysqld_safe --user=mysql &
safe_mysqld
Prior to MySQL 4.0, the mysqld_safe script was called safe_mysqld. If you happen to be installing an old version of MySQL, you'll have to use that file name instead.
If you see the message mysql daemon ended, then the MySQL server was prevented from starting. The error message should have been written to a file called hostname.err (where hostname is your machine's host name) in MySQL's data directory. You'll usually find that this happens because another MySQL server is already running on your computer.
If the MySQL server was launched without complaint, the server will run (just like your Web or FTP server) until your computer is shut down. To test that the server is running properly, type the following command:
shell#bin/mysqladmin -u root status
A little blurb with some statistics about the MySQL server should be displayed. If you receive an error message, something has gone wrong. Again, check the hostname.err file to see if the MySQL server output an error message while starting up. If you retrace your steps to make sure you followed the process described above, and this doesn't solve the problem, a post to the SitePoint Forums will help you pin it down in no time.
If you want your MySQL server to run automatically whenever the system is running (just like your Web server probably does), you'll have to set it up to do so. In the support-files subdirectory of the MySQL directory, you'll find a script called mysql.server that can be added to your system startup routines to do this. Let me show you how.
First of all, assuming you've set up a special MySQL user to run the MySQL server, you'll need to tell the MySQL server to start as that user by default. To do this, create in your system's /etc directory a file called my.cnf that contains these two lines:
[mysqld]
user=mysql
Now, when you run safe_mysqld or mysql.server to start the MySQL server, it will launch as user mysql automatically. You can test this by stopping MySQL, then running mysql.server with the start argument:
shell#bin/mysqladmin -u root shutdown
shell#support-files/mysql.server start
Request the server's status using mysqladmin as before, to make sure it's running correctly.
All that's left to do is to set up your system to run mysql.server automatically at startup (to launch the server) and at shutdown (to terminate the server). This is a highly operating system-dependant task. If you're not sure how to do it, you'd be best to ask someone who is. The following commands, however, will do the trick for most versions of Linux:
shell#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/
shell#cd /etc/rc2.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc3.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc5.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc0.d
shell#ln -s ../init.d/mysql.server K01mysql
That's it! To test that this works, reboot your system and request the status of the server as before.
One final thing you might like to do for the sake of convenience is to place the MySQL client programs, which you'll use to administer your MySQL server later on, in the system path. To this end, you can place symbolic links to mysql, mysqladmin, and mysqldump in your /usr/local/bin directory:
shell#ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
shell#ln -s /usr/local/mysql/bin/mysqladmin
/usr/local/bin/mysqladmin
shell#ln -s /usr/local/mysql/bin/mysqldump
/usr/local/bin/mysqldump
Installing PHP
As mentioned above, PHP is not really a program in and of itself. Instead, it's a plug-in module for your Web server (probably Apache). There are actually three ways to install the PHP plug-in for Apache:
As a CGI program that Apache runs every time it needs to process a PHP-enhanced Web page
As an Apache module compiled right into the Apache program
As an Apache module loaded by Apache each time it starts up
The first option is the easiest to install and set up, but it requires Apache to launch PHP as a program on your computer every time a PHP page is requested. This activity can really slow down the response time of your Web server, especially if more than one request needs to be processed at a time.
The second and third options are almost identical in terms of performance, but since you're likely to have Apache installed already, you'd probably prefer to avoid having to download, recompile, and reinstall it from scratch. For this reason, we'll use the third option.
To start, download the PHP Complete Source Code package from http://www.php.net/downloads.php. At the time of this writing, PHP 4 has become well-established as the version of choice; however, the newly released PHP 5 is gaining ground quickly. I'll be covering the installation of PHP 5.0 here, but the same steps should work just as well with PHP 4.
The file you downloaded should be called php-version.tar.gz. To begin, we'll extract the files it contains (the shell% prompt is included to represent that you can run these steps without being logged in as root):
shell%tar xfz php-version.tar.gz
shell%cd php-version
To install PHP as a loadable Apache module, you'll need the Apache apxs program. This comes with most versions of Apache (both versions 1.3 and 2.0), but if you're using the copy that was installed with your distribution of Linux, you may need to install the "Apache development" package to access Apache apxs. You should be able to install this package by the means provided by your software distribution. For example, on Debian Linux, you can use apt-get to install it as follows (you'll have to log in as root first):
shell#apt-get install apache-dev
By default, Fedora Core, RedHat, and Mandrake will install the program as /usr/sbin/apxs, so if you see this file, you know it's installed. If you've installed Apache by hand, it will probably be /usr/local/apache/bin/apxs.
For the rest of the install procedure, you'll need to be logged in as the root user so you can make changes to the Apache configuration files.
The next step is to configure the PHP installation program by telling it which options you want to enable, and where it should find the programs it needs to know about (such as Apache and MySQL). Unless you know exactly what you're doing, simply type the command like this (all on one line):
shell#./configure --prefix=/usr/local/php
--with-apxs=/usr/sbin/apxs
--with-mysql=/usr/local/mysql
--enable-magic-quotes
Replace /usr/sbin/apxs and /usr/local/mysql with the location of your apxs program and the base directory of your MySQL installation, respectively.
Apache 2.0
If you're using Apache 2.0 or later, you need to type --with-apxs2=… instead of --with-apxs=… to enable support for Apache 2.0. As of this writing, this support is still experimental and is not recommended for production sites. As a result of the ongoing work on this front, you may need to download the latest pre-release (unstable) version of PHP to get it working with the latest release of Apache 2.0, but it's worth trying the stable release version first.
For full instructions on how to download the latest pre-release version of PHP, see http://www.php.net/anoncvs.php.
Again, check for any error messages and install any files it identifies as missing. On Mandrake 8.0, for example, it complained that the lex command wasn't found. I searched for "lex" in the Mandrake package list and it came up with flex, which it described as a program for matching patterns of text used in many programs' build processes. Once that was installed, the configuration process went without a hitch. After you watch several screens of tests scroll by, you'll be returned to the command prompt. The following two commands will compile and then install PHP. Take a coffee break: this will take some time.
shell#make
shell#make install
Upon completion of make install, PHP is installed in /usr/local/php (unless you specified a different directory with the --prefix option of the configure script above), with one important exception—its configuration file, php.ini. PHP comes with two sample php.ini files called php.ini-dist and php.ini-recommended. Copy these files from your installation work directory to the /usr/local/php/lib directory, then make a copy of the php.ini-dist file and call it php.ini:
shell#cp php.ini* /usr/local/php/lib/
shell#cd /usr/local/php/lib
shell#cp php.ini-dist php.ini
You may now delete the directory from which you compiled PHP—it's no longer needed.
We'll worry about fine-tuning php.ini shortly. For now, we need to tweak Apache's configuration to make it more PHP-friendly. Open your Apache httpd.conf configuration file (usually under /etc/apache/ or /etc/httpd/ if you're using your Linux distribution's copy of Apache) in your favorite text editor.
Next, look for the line that begins with DirectoryIndex. In certain distributions, this may be in a separate file called commonhttpd.conf. This line tells Apache which file names to use when it looks for the default page for a given directory. You'll see the usual index.html, but you need to add index.php to the list if it's not there already:
DirectoryIndex index.html index.php
Finally, go right to the bottom of the file (again, this should go in commonhttpd.conf if you have such a file) and add these lines to tell Apache which file extensions should be seen as PHP files:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
That should do it! Save your changes and restart your Apache server. If all things go according to plan, Apache should start up without any error messages. If you run into any trouble, the helpful folks in the SitePoint Forums (myself included) will be happy to help.
Source: http://www.sitepoint.com/artic...allation/3
|
| Sat Mar 31, 2007 2:10 am |
|
 |
easydoers
Junior Developer
Joined: 09 Mar 2005
Posts: 40
1011 Cash
|
Mac OS X Installation
As of version 10.2 (Jaguar), Mac OS X distinguishes itself by being the only consumer OS to install both Apache and PHP as components of every standard installation. That said, the version of PHP provided is a little out-of-date, and you'll need to install the MySQL database as well.
In this section, I'll briefly cover what's involved in setting up up-to-date versions of PHP and MySQL on Mac OS X. Before doing that, however, I'll ask you to make sure that the Apache Web server built into your Mac OS X installation is enabled.
Click to pull down the Apple menu.
Choose System Preferences from the menu.
Select Sharing from the System Preferences panel.
If the Sharing preference panel says Web Sharing Off, click the Start button to launch the Apache Web server.
Exit the System Preferences program.
With this procedure complete, Apache will automatically be run at startup on your system from now on. You're now ready to enhance this server by installing PHP and MySQL!
Installing MySQL
Apple maintains a fairly comprehensive guide to installing MySQL on Mac OS X on its Mac OS X Internet Developer site if you want to get your hands dirty and compile MySQL yourself. It is much easier, however, to obtain the precompiled binary version directly from the MySQL Website, and follow the installation instructions in the MySQL manual. In this section, I'll attempt to boil down this information to the essentials to help you get started as quickly as possible.
First of all, if you happen to be running Mac OS X Server, MySQL is already installed for you. You can run Applications/Utilities/MySQL Manager to access it. More likely, however, you are using the client version of Mac OS X.
To install MySQL on the client version of Mac OS X, begin by going to http://dev.mysql.com/downloads/ and selecting the latest production release of MySQL (4.0 as of this writing). Scroll down to the Mac OS X downloads section, then select and download the Installer package version for your operating system. You'll have a choice of the Standard, Max, and Debug releases; choose the Standard release unless you have a special reason for choosing one of the others.
Once you've downloaded the mysql-standard-version-apple-darwinversion-powerpc.dmg file, double-click it to mount the disk image if your browser hasn't already done this for you. Inside it, you'll find the installer in .pkg format, as well as a MySQLStartupItem.pkg file. Double-click the installer, which will guide you through the installation of MySQL.
Once MySQL is installed, you can launch the MySQL server by opening a Terminal window and typing this command:
shell%sudo /usr/local/mysql/bin/mysqld_safe
Enter the administrator password if prompted. Once MySQL is running, you can switch it to background execution by typing Ctrl-Z to suspend it, and typing this command:
shell%bg
You can then close the Terminal window and MySQL will continue to run as a server on your system.
Presumably, you'll want your system automatically to launch the MySQL server at startup so that you don't have to repeat the above process whenever you restart your system. To do this, simply double-click the MySQLStartupItem.pkg file and follow the instructions.
When you're done, you can safely drag the mounted drive for the MySQL installation package to the trash, then delete the .dmg file.
Installing PHP
As with MySQL, a Mac OS X version of PHP is not available from the official Website, but from a third party. Again, Apple also maintains a Web page detailing the installation procedure, although in this case it is somewhat out of date. A better source of information is http://www.entropy.ch/software/macosx/php/, where you can download an installer package in the form of a disk image.
The latest version of PHP available for Mac OS X 10.2 is PHP 4.3.4. More recent versions of PHP (up to 5.0.1 as of this writing) are available for Mac OS X 10.3 or later only. Select the version that is right for your system and download it.
If your browser doesn't do it for you, mount the disk image by double-clicking the Entropy-PHP-version.dmg file, then double-click the installer .pkg file it contains. Simply follow the instructions, and PHP will be installed on your server. That's all there is to it!
Mac OS X and Linux
Because Mac OS X is based on the BSD operating system, much of its internals work just like any other Unix-like OS (e.g. Linux). From this point forward, owners of Mac OS X servers can follow the instructions provided for Unix/Linux systems unless otherwise indicated. No separate instructions are provided for Mac OS X unless they differ from those for other Unix-like systems.
Post-Installation Setup Tasks
No matter which operating system you're running, once PHP is installed and the MySQL server is in operation, the very first thing you need to do is assign a root password for MySQL. MySQL allows authorized users only to view and manipulate the information stored in its databases, so you'll need to tell MySQL who is an authorized user, and who isn't. When MySQL is first installed, it's configured with a user named root that has access to do pretty much anything without even entering a password. Your first task should be to assign a password to the root user so that unauthorized users can't tamper with your databases.
Why should I bother?
It's important to realize that MySQL, just like a Web server or an FTP server, can be accessed from any computer on the same network. If you're working on a computer connected to the Internet, then, depending on your security measures, that means anyone in the world could try to connect to your MySQL server! The need to pick a hard-to-guess password should be immediately obvious!
To set a root password for MySQL, open a command prompt (or Terminal window) and type the following command in the bin directory of your MySQL installation:
mysql -u root mysql
This command connects you to your newly-installed MySQL server as the root user, and chooses the mysql database. After a few lines of introductory text, you should see the MySQL command prompt (mysql>). To assign a password to the root user, type the following two commands (pressing Enter after each one):
mysql>UPDATE mysql.user SET Password=PASSWORD("new password")
->WHERE User="root";
Query OK, 2 rows affected (0.12 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql>FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.24 sec)
Be sure to replace new password with the password you want to assign to your root user.
With that done, disconnect from MySQL with the quit command:
mysql>quit
Bye
Now, to try out your new password, request that the MySQL server tell you its current status at the system command prompt:
mysqladmin -u root -p status
Enter your new password when prompted. You should see a brief message that provides information about the server and its current status. The -u root argument tells the program that you want to be identified as the MySQL user called root. The -p argument tells the program to prompt you for your password before it tries to connect. The status argument just tells it that you're interested in viewing the system status.
If at any time you want to shut down the MySQL server, you can use the command below. Notice the same -u root and -p arguments as before:
mysqladmin -u root -p shutdown
With your MySQL database system safe from intrusion, all that's left is to configure PHP. To do this, we'll use a text file called php.ini. If you installed PHP under Windows, you should already have copied php.ini into your Windows directory. If you installed PHP under Linux using the instructions above, you should already have copied php.ini into the PHP lib folder (/usr/local/php/lib), or wherever you chose to put it. The Mac OS X installation program will have placed the file in /usr/local/php/lib for you automatically.
Open php.ini in your favorite text editor and have a glance through it. Most of the settings are fairly well explained, and most of the default settings are fine for our purposes. Just check to make sure that your settings match these:
register_globals = Off
magic_quotes_gpc = On
extension_dir = the directory where you installed PHP
Note: PHP experts may tell you that you'll achieve better performance with magic_quotes_gpc set to Off, but that setting exposes you to hackers attempting SQL injection attacks on your Website if you are not very careful to write scripts that protect themselves from such malicious behavior. Until you fully understand PHP and the types of security issues that scripts must combat, leave this setting On.
Note: the directory where you installed PHP is usually c:\php on Windows, and /usr/local/php on Linux.
Save the changes to php.ini, and then restart your Web server. To restart Apache under Linux (or Mac OS X), log in as root and type this command:
shell#apachectl graceful
You're done! Now, you just need to test to make sure everything's working (see the section called "Your First PHP Script").
If Your Web Host Provides PHP and MySQL
If the host that provides you with Web space has already installed and set up MySQL and PHP for you, and you just want to learn how to use them, there really isn't a lot you need to do. Now would be a good time to get in touch with your host and request any information you may need to access these services.
Specifically, you'll need a user name and password to access the MySQL server they've set up for you. They'll probably also have provided an empty database for your use, which prevents you from interfering with the databases of other users who share the same MySQL server, and you'll want to know the name of your database.
There are two ways you can access the MySQL server directly. Firstly, you can use telnet or secure shell (SSH) to log in to the host. You can then use the MySQL client programs (mysql, mysqladmin, mysqldump) installed there to interact with the MySQL server directly. The second method is to install those client programs onto your own computer, and have them connect to your host's MySQL server. Your Web host may support one, both, or neither of these methods, so you'll need to ask.
If your host allows you to log in by telnet or SSH to do your work, you'll need a user name and password for the login, in addition to those you'll use to access the MySQL server (they can be different). Be sure to ask for both sets of information.
If they support direct access to the MySQL server, you'll want to download a program that lets you connect to, and interact with, the server. This book assumes you've downloaded from MySQL Control Center. I'd recommend getting comfortable with the basic client programs first, though, as the commands you use with them will be similar to those you'll include in your PHP scripts to access MySQL databases.
Many less expensive Web hosts support neither telnet/SSH access, nor direct access to their MySQL servers. Instead, they normally provide a management console that allows you to browse and edit your database through your Web browser (though some actually expect you to install one yourself, which I'll cover briefly in Chapter 2, Getting Started with MySQL). Although this is a fairly convenient and not overly restrictive solution, it doesn't help you learn. Instead, I'd recommend you install a MySQL server on your own system for experimentation, especially in the next chapter. Once you're comfortable working with your learning server, you can start using the server provided by your Web host with the Web-based management console. See the previous sections for instructions on installing MySQL under Windows, Linux, and Mac OS X.
Your First PHP Script
It would be unfair of me to help you get everything installed and not even give you a taste of what a PHP-driven Web page looks like until Chapter 3, Getting Started with PHP, so here's a little something to whet your appetite.
Open your favorite text or HTML editor and create a new file called today.php. Windows users should note that, to save a file with a .php extension in Notepad, you'll need to either select All Files as the file type, or surround the file name with quotes in the Save As dialogue; otherwise, Notepad will helpfully save the file as today.php.txt, which won't work (see the note earlier in this chapter for more information). Mac OS users are advised not to use TextEdit to edit .php files, as it saves them in Rich Text Format with an invisible .rtf file name extension. Learn to use the vi editor in a Terminal window or obtain an editor that can save .php files as plain text.
Whichever editor you use, type this into the file:
Example 1.1. today.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Today's Date</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>Today's Date (according to this Web server) is
<?php
echo date('l, F dS Y.');
?></p>
</body>
</html>
If you prefer, you can download this file, which, along with the rest of the code in this book, is contained in the code archive. See the Preface for details on how to download the archive.
Save the file, and place it on your Website as you would any regular HTML file, then view it in your browser. Note that if you view the file on your own machine, you cannot use the File > Open… feature of your browser, because your Web server must intervene to interpret the PHP code in the file. Instead, you must move the file into the root document folder of your Web server software (e.g. C:\inetpub\wwwroot\ in IIS, or C:\Program Files\Apache Group\Apache\htdocs\ in Apache for Windows), then load it into your browser by typing http://localhost/today.php. This process allows the Web server to run the PHP code in the file and replace it with the date before it's sent to the Web browser. Figure 1.2 shows what the output should look like.
Figure 1.2. See your first PHP script in action!
Pretty neat, huh? If you use the View Source feature in your browser, all you'll see is a regular HTML file with the date in it. The PHP code (everything between <?php and ?> in the code above) was interpreted by the Web server and converted to normal text before it was sent to your browser. The beauty of PHP, and other server-side scripting languages, is that the Web browser doesn't have to know anything about it — the Web server does all the work!
Don't worry too much about the exact code I used in this example. Before too long you'll know it like the back of your hand.
If you don't see the date, then something is wrong with the PHP support on your Web server. Use View Source in your browser to look at the code of the page. You'll probably see the PHP code there in the page. Since the browser doesn't understand PHP, it just sees <?php … ?> as one long, invalid HTML tag, which it ignores. Make sure that PHP support has been properly installed on your Web server, either in accordance with the instructions provided in previous sections of this chapter, or by your Web host.
Summary
You should now have everything you need to install MySQL and PHP on your Web Server. If the little example above didn't work (for example, if the raw PHP code appeared instead of the date), something went wrong with your setup procedure. Drop by the SitePoint Forums and we'll be glad to help you figure out the problem!
In Chapter 2, Getting Started with MySQL, you'll learn the basics of relational databases and get started working with MySQL. If you've never even touched a database before, I promise you it'll be a real eye-opener!
Source: http://www.sitepoint.com/artic...allation/4
|
| Sat Mar 31, 2007 2:11 am |
|
 |
|
|
The time now is Sat Oct 11, 2008 8:20 pm | All times are GMT - 6 Hours
|
Page 1 of 1
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|