Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

PHP Programming/Setup and Installation

From Wikibooks, open books for an open world
<PHP Programming
IntroductionPHP Programming
Setup and Installation
Uses of PHP

Principle

[edit |edit source]

Since PHP is a server-side technology, you should naturally expect to invest some time in setting up a server environment for production, development or learning. To be frank, PHP is quite easy to set up compared to other monsters like J2EE.

Nevertheless, the procedures are complicated by the various combinations of different versions of web server, PHP and database (most often MySQL). That's why in a process of learning it's possible to execute some commands onhttp://phpfiddle.org/ or Tutorialspoint, without installing anything.

Below we will introduce the steps needed to set up a working PHP environment with MySQL database on one's machine.

Linux

[edit |edit source]

If your desktop runs on Linux, chances are that Apache, PHP, and MySQL are already installed for you. This wildly popular configuration is commonly referred to as LAMP, i.e.LinuxApacheMySQLPHP, orP, the latter 'P', can also refer toPerl another major player in the opensource web service arena. If some components are not installed, you will likely have to manually install the following packages:

  • Apache or Lighttpd
  • PHP
  • MySQL or Postgres
  • The PHP integration plugin for the database.

Debian or its derivatives

[edit |edit source]

On Debian or its derivatives, Ubuntu included[1], you can use the corresponding commands:

apt-getinstallphp5## Server#### If you wish to use Apacheapt-getinstallapache2libapache2-mod-php5a2enmodphp5serviceapache2restart## -or-#### If you wish to use Lighttpdapt-getinstalllighttpdphp5-cgilighttpd-enable-modfastcgifastcgi-phpservicelighttpdrestart## Database#### If you wish to use Postgresapt-getinstallpostgres-serverpostgres-clientphp5-pg## -or-#### If you wish to use Mysqlapt-getinstallmysql-servermysql-clientphp5-mysql

^ If you chose to use Ubuntu with Apache and MySQL you might wish to utilize the Ubuntu community site for such a configurationubuntu lamp wiki.

Gentoo

[edit |edit source]

For Gentoo Linux users, the gentoo-wiki has this HowTo available:Apache2 with PHP and MySQL.

In general, you'll want to do the following under Gentoo:

emergeapacheemergemysqlemergemod_php

RPM-based

[edit |edit source]

The exact procedures depend on your Linux distribution. On a Fedora system, the commands are typically as follows:

yuminstallhttpdyuminstallphpyuminstallmysqlyuminstallphp-mysql

It's impossible to cover all the variants here, so consult your Linux distribution's manual for more details, or grab a friend to do it for you.

One sure-fire way of getting PHP up and running on your *nix system is to compile it from source. This isn't as hard as it may sound and there are good instructions available in thePHP manual.

Windows

[edit |edit source]

PHP on Windows is also a very popular option. On a Windows platform, you have the option to use either the open sourceApache web server, or the native Internet Information Services (IIS) server from Microsoft, which can be installed by searching the start menu or control panel for 'Turn Windows Features On or Off' (Windows 7, 8, 10), or via Windows CD on older installations. When you have one of these servers installed, you can download and install the appropriate PHP Windows binaries distributions from thePHP download page. The installer version requires less user-interaction.

For increased performance you will want to use FastCGI. There is a wikibook that will assist you onSetting up IIS with FastCGI.

Databases

[edit |edit source]

On Microsoft Windows you must always install your own database.

Some popular choices are the open source Postgres, SQLite, and MySQL. Postgres and SQLite are more liberally licensed, and are free to use for commercial purposes.SQLite is solely an embedded database, similar to Microsoft Access, and is hosted on the same application server as the PHP instance or applications that are referencing it, whereas MySQL and Postgres are typically used a database server that may be connected to by many machines at once.

PostgreSQL

[edit |edit source]

Official Zend documentation:http://us.php.net/pgsql

Postgres is simple and easy to install, browse tohttp://www.postgresql.org/ftp/binary/v8.3.0/win32/ and download the exe and double-click.

MySQL

[edit |edit source]

Official MySQL documentation:http://us.php.net/mysql

You might wish to install the MySQL database. You can download the Windows version ofMySQL, and follow the installation instructions. If you have PHP 4, you do not need to install the equivalence of php-mysql on Linux, as MySQL support is built-in in Windows distributions of PHP. In PHP 5 you will need to uncomment the following line in your php.ini file (that is, remove the ';' at the beginning of the line):

;extension=php_mysql.dll

Bundled Package

[edit |edit source]

If you find all the above too much a hassle, you have another option. Driven by the eternal desire to do things the safe/easy way, several conveniently packaged AMP bundles of Apache/MySQL/PHP can be found on the net. One of them isPHPTriad. Or, you can tryUniform Server. It is a smallWAMP Package. (The acronymWAMP refers to a server stack where Microsoft Windows is the operating system, Apache is the Web server, MySQL handles the database components, and PHP, Python, or PERL represents the dynamic scripting languages).[1] Uniformserver is packaged as a self-extracting zip archive, and is easy to use. After trying it out you can simply delete the directory and everything is clean.XAMPP for Windows is another WAMP server that is easy to use. In addition, it has an installation option that allows you to install it on a computer if you have administration rights. XAMPP has options to run PERL and JAVA (on a tomcat server). A number of other portable Windows AMP package choices are summarized atList of portable Web Servers.

Also, a package installer calledWAMPserver is available. It simply installs Apache, PHP and MySQL on windows with ease.[2]

Easy Windows Setup Instructions

[edit |edit source]

1) PHP authoring environment

  • Any text editor will do, but I recommend one with syntax coloring especially for someone new to coding or PHP. Notepad++ is my favorite so far with its ease of use, customizability and the ability to collapse tags. All editor help will be in reference to Notepad++
  • Install Notepad++. Download the binary file from here:http://notepad-plus.sourceforge.net/uk/site.htm

2) PHP running environment

  • Now that you have the ability to create and save PHP files you need an environment that can process them and generate the output that your browser displays. There are two ways to accomplish this.
  1. Get a web host that supports PHP and upload your files every time you make a change.
  2. Use your own computer as a personal server with PHP support, and only upload final versions to a web host.
  • Accomplishing 2. above is actually easier than you think (assuming you are running Windows).
  1. Download The uniform server. Here is the link to the latest version:http://sourceforge.net/projects/miniserver/files/
  2. Run the self-extractor UniServerX_Y_Z.exe. Copy the directory to your "C:" drive so the full path is "C:\UniServer".
  3. In the directory where you had it extract hit the "Start.exe" file to get the server running
  4. Place any files and subfolders you want the server to read and process in the "www" folder.
  5. Your web browser should open to "http://localhost/index.php"
  • Now you have the resources to effectively edit PHP documents and process them on your own computer.
  • Here is how to create a test page
  1. Inside "C:\UniServer\www\", create a webpage called "test.php"
  2. Edit "test.php" with Notepad++, and copy the following
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><title>Test Page</title><metaname="Description"content="Test Page"/><metaname="Keywords"content="Test,Page"/></head><body><?php  $string = 'Hello world! <br/>';  echo $string;  print $string;  printf('%s', $string);?></body></html>
  1. Save the webpage as "C:\UniServer\www\test.php"
  2. Open "http://localhost/test.php" in your web browser to view the page. You should see:

Hello world!
Hello world!
Hello world!

  1. (Optional: Follow these instructions for how to make PHP code work with webpages that end in ".html" in addition to ".php":http://www.desilva.biz/php/phpinhtml.html)

Mac OS X

[edit |edit source]

Mac OS X comes with Apache server as standard, and enabling it is as simple as checking the box next to 'Personal Web Sharing' in the 'Sharing' section of System Preferences. Once you have done this you can place files in /Library/WebServer/Documents to access them on your server. Mac OS X does come with PHP but the installation lacks any significant quantity of extensions, so if you want any you're going to have to install PHP yourself. You can do this by following the instructions in Apple'sDeveloper Connection, or you can download an automatic installer such as the ones available atEntropy. Once you've done one of those, you'll have a server with PHP running on your Mac.

To install MySQL just download and run theOS X installer package or useXAMPP for MacOS X.

If you use unix or learning it, however, compiling might be the way to go for all three, or just the ones you like. The advantage is that you can choose exactly that extensions you want for PHP and Apache. Also you can choose which versions to compile together. To do this make sure you have the Developer Tools installed. They are shipped with OS X.

How Do I Know My Setup is Working?

[edit |edit source]

After you have successfully completed the previous section, it's time to make sure that everything went well. You also get the chance to write your very first PHP scripts! Open your favouriteplaintext editor (not Microsoft Word or anotherword processor), and type the following magical line:

<?phpphpinfo();?>

Save it as phpinfo.php in your web server's root document directory. If you are using a web hosting server, upload it to the server to where you would place HTML files. Now, open up your web browser, and go to http://localhost/phpinfo.php, or http://your-web-hosting-server.com/phpinfo.php if you are using a web hosting server, and look at the output.

Now scroll down that page and make sure there is a table with the title "mysql", and the top row should read: "MySQL support: enabled". If your output does not have this, your particular installation of PHP does not have MySQL support enabled. Note that this test doesn't tell you whether MySQL server is running. You should fire up your MySQL client and check before you proceed.

Some dedicated php or script editors even have color coding of different words that can be very useful for finding mistakes. A free implementation of this is the powerful Notepad++, available fromSourceforge and licensed under the GPL.

Notes

[edit |edit source]
  1. http://www.webopedia.com/TERM/W/WAMP.html
  2. Wamp server


IntroductionPHP Programming
Setup and Installation
Uses of PHP
Retrieved from "https://en.wikibooks.org/w/index.php?title=PHP_Programming/Setup_and_Installation&oldid=4387741"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp