Under the Mac Build PHP Development Environment Tutorial _ Server Other

Source: Internet
Author: User
Tags bz2 mysql in php and php development environment socket sqlite zts phpmyadmin

Mac OS X is built with Apache and PHP, which makes it easy to use. This article takes Mac OS X 10.6.3 as an example. The main contents include:

Start Apache
Running PHP
installing MySQL
Using phpMyAdmin
Configure PHP's MCrypt extension library
Setting up a virtual host

Start Apache

There are two ways of doing this:

Open System settings preferences (systems Preferences)-> share (sharing)-> Web share (web sharing)
Open terminal (terminal), and then (note: sudo needs the password is the system's root account password)
Run "sudo apachectl start", and then enter the account password, so that Apache is running.

Run "sudo apachectl-v" and you will see the Apache version number in Mac OS X 10.6.3:

Server version:apache/2.2.14 (Unix)
Server Built:feb 11 2010 14:40:31

by typing "http://localhost" in the browser, you can see that one of the contents is "It works!" Page, which is located under "/library/webserver/documents/", which is the default root directory for Apache.

NOTE: When Apache is turned on, web sharing is turned on, and networked users will access the/library (resource pool)/webserver/documents/directory via "http://[local ip]/" via "http://[local IP" /~[user Name] "To access the/users/[username]/sites/directory, you can prohibit this access by setting up firewall (Firewall) in security on system preferences.

Running PHP

Run "sudo vi/etc/apache2/httpd.conf" in the terminal to open the Apache configuration file. (If you are not accustomed to operating terminals and VI can be set in the Finder to display all the system hidden files, remember that you need to restart the Finder, so you can find the corresponding file, arbitrary editing, but it should be noted that some of the file changes need to open the root account, But overall it is safer to use sudo on the terminal to temporarily get root permissions. )

Find the "#LoadModule php5_module libexec/apache2/libphp5.so", remove the front #, save (enter at the command line: W) and Exit VI (enter at the command line: Q).

Run "sudo cp/etc/php.ini.default/etc/php.ini" so that you can configure a variety of PHP features through php.ini. Like what:

, use the following two items to adjust the maximum value of the PHP submission file, such as the maximum value of the imported data in phpMyAdmin

Upload_max_filesize = 2M
Post_max_size = 8M

, such as through display_errors to control whether to show the PHP program error

Display_errors = Off

Run "sudo apachectl restart" and restart Apache so that PHP can be used.

Run "cp/library/webserver/documents/index.html.en/library/webserver/documents/info.php", That is, copy the Index.html.en file in the root directory of Apache and rename it to info.php

Run "vi/library/webserver/document/info.php" in the terminal so that you can edit the info.php file in VI. In "It ' s works!" Followed by the words "<?php phpinfo ();?>" and then saved. This allows you to see information about PHP in http://localhost/info.php, such as the version number is 5.3.1.

installing MySQL

Since MySQL is not pre-installed in Mac OS X, it needs to be installed manually, and the most stable version of MySQL is currently 5.1. MySQL provides installation instructions under Mac OS X.
Download MySQL 5.1. Choose the right version, such as MYSQL-5.1.46-OSX10.6-X86_64.DMG.
Run DMG, you will find 4 files inside. First click on the install Mysql-5.1.46-osx10.6-x86_64.pkg, which is the MySQL main installation package. Typically, the installation file automatically installs MySQL under the same folder under/usr/local. For example, click to run "MYSQL-5.1.46-OSX10.6-X86_64.DMG" will install mysq into "/usr/local/mysql-5.1.46-osx10.6-x86_64". The default installation can be completed all the way.

Click to install the 2nd file mysqlstartupitem.pkg, so that MySQL will automatically start automatically when the boot.

Click on the installation of the 3rd file Mysql.prefpane, this will be in the "system Preferences" to see the name "MySQL" icon, through it can be set to start or stop MySQL, and whether the boot automatically run. MySQL is basically installed here.

Add MySQL and mysqladmin aliases to bash's configuration file by running "sudo vi/etc/bashrc"

Copy Code code as follows:

#mysql
Alias mysql= '/usr/local/mysql/bin/mysql '
Alias mysqladmin= '/usr/local/mysql/bin/mysqladmin '

This can be more simple in the terminal through the command to do the appropriate operation, such as after the installation of the MySQL root default password is empty, if you want to set the password can run in the terminal "Mysqladmin-u root password" Mysqlpassword "" to set, Where Mysqlpassword is the password of root. For more information, refer to b.5.4.1. You to Reset the Root Password.

Note: Mac OS x Upgrades or other reasons may cause MySQL to start or run automatically, and on the operating panel of MySQL, you will be prompted "Warning:the/usr/local/mysql/data directory isn't owned by The ' MySQL ' or ' _mysql ', or prompt at the command line "Can ' t connect to a local MySQL server through socket '/tmp/mysql.sock ' (2)", which should be caused in some way/ The host of Usr/local/mysql/data has changed, just run "sudo chown-r mysql/usr/local/mysql/data".

In addition, using PHP to connect MySQL may error "can" T connect to the local MySQL server through socket '/var/mysql/mysql.sock ', Or use localhost cannot connect MySQL and need 127.0.0.1, because the connection time PHP default to find/var/mysql/mysql.sock, but the Mac version of MySQL changed the location of the file, placed in/tmp. The approach is to modify the php.ini as follows:

Mysql.default_socket =/tmp/mysql.sock

Using phpMyAdmin

phpMyAdmin is a PHP-developed program for managing MySQL, which is very popular and practical. The ability to manage MySQL in a practical phpmyadmin is a very effective way to test the results of previous steps.

Download phpMyAdmin. Choose the right version, such as the one I chose for phpmyadmin-3.3.2-all-languages.tar.bz2.

Copy the Phpmyadmin-3.32-all-languages folder in download (downloads) to the/users/[username]/sites, renamed phpMyAdmin.

Copy the config.sample.inc.php in the "/users/[username]/sites/phpmyadmin/" and name it config.inc.php

Open the config.inc.php and make the following modifications:

Used for cookie encryption, random long strings

$cfg [' blowfish_secret '] = ';

When "#2002 cannot log on to the MySQL server" appears in phpMyAdmin,

Please change the localhost to 127.0.0.1.

This is because the MySQL daemon did the IP binding (bind-address =127.0.0.1) caused by the

$cfg [' Servers '] [$i] [' host '] = ' localhost ';

Change false to True, so you can access the password-free MySQL.

You can set this up even if MySQL has a password, and then enter the password when you log in to phpMyAdmin

$cfg [' Servers '] [$i] [' allownopassword '] = false;

This allows you to access phpMyAdmin by http://localhost/~[user name]/phpmyadmin. You'll see a hint this time. "The mcrypt extension cannot be loaded, please check your PHP configuration." "This involves the next section to install the MCrypt extension.

Configure MCrypt Extensions for PHP

MCrypt is a powerful encryption algorithm extension library, which includes 22 algorithms, phpMyAdmin relies on this PHP extension library. But its installation under Mac OS X is less friendly, as follows:

Download and extract the libmcrypt-2.5.8.tar.bz2.

Execute the following command at the terminal (note that the following command requires installation of Xcode support):

Copy Code code as follows:

CD ~/downloads/libmcrypt-2.5.8/
./configure--disable-posix-threads--enable-static
Make
sudo make install

Download and extract the PHP source file php-5.3.1.tar.bz2. The PHP version preinstalled in Mac OS X 10.6.3 is 5.3.1, and now the latest PHP version is 5.3.2, you need to choose the version according to your actual situation.

Execute the following command at the terminal:

Copy Code code as follows:

CD ~/downloads/php-5.3.1/ext/mcrypt
Phpize
./configure
Make
CD modules
sudo cp mcrypt.so/usr/lib/php/extensions/no-debug-non-zts-20090626/

Open php.ini

sudo vi/etc/php.ini

Add the following code to the php.ini, save and exit, and then restart Apache

Extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/mcrypt.so

When you visit the http://localhost/~[username]/phpmyadmin, you will find that "the mcrypt extension cannot be loaded, please check your PHP configuration." "The hint is gone, which means that the MCrypt extension Library has been installed successfully.

setting up a virtual host

Run "sudo vi/etc/apache2/httpd.conf" on the terminal and open the Apche configuration file

Find the "#Include/private/etc/apache2/extra/httpd-vhosts.conf" in the httpd.conf, remove the "#" from the front, and save and exit.

Run "sudo apachectl restart" and restart Apache to turn on its virtual host configuration feature.

Run "sudo vi/etc/apache2/extra/httpd-vhosts.conf", which opens the configuration of the virtual host file httpd-vhost.conf, configure the virtual host you need. Note that the file opens two virtual hosts as an example by default:

Copy Code code as follows:

<virtualhost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
Customlog "/private/var/log/apache2/dummy-host.example.com-access_log" common
</VirtualHost>
<virtualhost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/usr/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
Customlog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>

In fact, these two virtual hosts do not exist, and when no other virtual host is configured, the following prompts may occur when accessing localhost:

Forbidden

You don ' t have permission to access/index.php on this server

The easiest way to do this is to add a # to the front of each line and comment it out so that you can refer to it without causing any other problems.

Add the following configuration

Copy Code code as follows:

<virtualhost *:80>
DocumentRoot "/users/[username]/sites"
ServerName Sites
ErrorLog "/private/var/log/apache2/sites-error_log"
Customlog "/private/var/log/apache2/sites-access_log" common
</VirtualHost>

Save exit and restart Apache.

Run "sudo vi/etc/hosts", open the hosts configuration file, and join "127.0.0.1 sites" so that you can configure the sites virtual host so that you can use "http://sites" to access the contents and "http:// localhost/~[user Name] "exactly the same.

This is a configuration that is implemented using Mac OS X 10.6.3 native support, or Mac OS X Leopard: Configure Apache, PHP, SQLite, MySQL, and phpMyAdmin (i) and Mac OS X L Eopard: Configure Apache, PHP, SQLite, MySQL, and phpMyAdmin (ii). In fact, you can also use XAMPP or MacPorts, a third-party-provided integration solution to achieve simple installation and use.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.