PHP/安裝及設定

< PHP

返回 PHP

安裝及設定编辑

由於PHP是一種服務器端技術,因此你需要設置一個服務器環境來開發或學習。

坦率地说,与其他像J2EE这样的怪物相比,PHP非常容易设置。尽管如此,这些过程由于使用不同版本的Web服务器、PHP和数据库(通常是MySQL)的各种组合所复杂化。下面将介绍使用MySQL数据库建立一个有效的PHP环境所需的步骤。

Linux编辑

如果你的電腦運行Linux,Apache、PHP與MySQL應該已經安裝。這稱為LAMP(Linux Apache MySQL PHP) 如果未安裝這些元件,你需要安裝以下套件:

  • Apache 或 Lighttpd
  • PHP
  • MySQL 或 Postgres
  • PHP與資料庫聯接的插件

Redhat架構编辑

各發行版的指令不同。使用Fedora,在shell執行

yum install httpd
yum install php
yum install mysql
yum install php-mysql

Debian架構编辑

Debian架構系統,包括Ubuntu[1],執行以下指令:

apt-get install php5

## 網頁服務器
#### Apache
apt-get install apache
## 或者
#### Lighttpd
apt-get install lighttpd

## 資料庫
#### Mysql
apt-get install mysql-server       mysql-client       php5-mysql
## 或者
#### Postgres
apt-get install postgres-server    postgres-client    php5-pg

詳細請閱讀:Ubuntu LAMP wiki

Gentoo编辑

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:

emerge apache
emerge mysql
emerge mod_php

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 the PHP manual.

Windows编辑

Contrary to what some people may think, PHP on Windows is a very popular option. On a Windows platform, you have the option to use either the open source Apache web server, or the native Internet Information Services (IIS) server from Microsoft, which can be installed from your Windows CD. When you have one of these servers installed, you can download and install the appropriate PHP Windows binaries distributions from PHP 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 on Setting up IIS with FastCGI.

Databases编辑

On Microsoft Windows you must always install your own database. Two popular choices are the open source Postgres, and MySQL. Postgres is more liberally licensed, and is free to use for commercial purposes.

Postgresql编辑

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

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

MySQL编辑

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

You might wish to install the MySQL database. You can download the Windows version of MySQL, 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编辑

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 is PHPTriad. Or, you can try Uniform Server. It is a small WAMP Package. 1 click install and also easy to use. Same with XAMPP for Windows. And after trying these out you can simply delete the directory and everything is clean. A number of portable Windows AMP package choices are summarized at List of portable Web Servers.

Also, a package installer called WAMP is available. It simply installs Apache, PHP and MySQL on windows with ease. http://www.en.wampserver.com/

Mac OS X编辑

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's Developer Connection, or you can download an automatic installer such as the ones available at Entropy. 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 the OS X installer package or use XAMPP for MacOS X, which is currently in beta testing.

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 which 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?编辑

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 favourite plain text editor (not Microsoft Word or another word processor), and type the following magical line:

 <?php phpinfo(); ?>

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 which can be very useful for finding mistakes. A free implementation of this is the powerful Notepad++, available from Sourceforge and licensed under the GPL.