Build a php development environment on MAC of the Apple system (this article is taken from Baidu Library)

Source: Internet
Author: User
Tags php development environment zts
Set up a php development environment on Mac. MacOSX has built-in Apache and PHP, which is very convenient to use. This article takes MacOSX10.6.3 as an example. Main content package

Set up a php development environment for Mac OS X with built-in Apache and PHP, which is very convenient to use. This article takes Mac OS X 10.6.3 as an example. The main content includes:
Start Apache
Run PHP
Install MySQL
Use phpMyAdmin
Configure the MCrypt extension library of PHP
Set VM
There are two ways to start Apache:
Choose System Preferences> Sharing> Web Sharing )"
Open "terminal" and (note: The password required by sudo is the root account password of the system)
Run "sudo apachectl start" and then enter the account password to run Apache.
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
In this way, enter "http: // localhost" in the browser, and you can see that the content is "It works !" Is located under "/Library (resource Library)/WebServer/Documents/", which is the default root directory of Apache.
Note: If Apache is enabled, "Web Sharing" is enabled, and connected users access "/Library (Resource Library)" through "http: // [local IP address) /WebServer/Documents/"directory, through" http: // [local IP address]/~ [User name] to access the "/Users/[user name]/Sites/" directory. you can set "Firewall" in "Security" of "system preference settings) to prohibit such access.
Run PHP

Run "sudo vi/etc/apache2/httpd. conf" on the terminal to open the Apache configuration file. (If you are not familiar with operating terminal and vi, you can set to display all system hidden files in the Finder. remember to restart the Finder so that you can find the corresponding file and edit it as you like, note that you need to enable the root account to modify some files, but it is safer to use sudo on the terminal to temporarily obtain the root permission .)
Find "# LoadModule php5_module libexec/apache2/libphp5.so", remove the # sign, save it (enter w in the command line), and exit vi (enter q in the command line ).
Run "sudo cp/etc/php. ini. default/etc/php. ini" to configure various php functions through PHP. ini. For example:
Use the following two items to adjust the maximum value of the submitted PHP file, for example, the maximum value of imported data in phpMyAdmin.
Upload_max_filesize = 2 M
Post_max_size = 8 M
For example, display_errors is used to control whether the PHP program reports errors.
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", copy the index.html. en file in the apacheroot directory, and rename it info. php.
Run "vi/Library/WebServer/Document/info. php" on the terminal to edit the info. php file in vi. In "It's works !" Add" And save it. In this way, you can see information about php in http: // localhost/info. PHP, for example, the version number is 5.3.1.
Install MySQL

Because MySQL is not pre-installed in Mac OS X, you need to manually install it. Currently, the most stable MySQL version is 5.1. MySQL provides installation instructions for Mac OS X.
Download MySQL 5.1. Select the appropriate version, for example, the mysql-5.1.46-osx10.6-x86_64.dmg here.
Run dmg and you will find four files in it. First click install mysql-5.1.46-osx10.6-x86_64.pkg, this is the MySQL main installation package. Generally, the installation file automatically installs MySQL to a folder with the same name under/usr/local. For example, clicking 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 installation is complete by default.
Click to install the 2nd file MySQLStartupItem. pkg, so that MySQL will automatically start at startup.
Click to install MySQL 3rd files. prefPane. in this way, the ICON "MySQL" is displayed in "system setting preferences", through which you can set whether MySQL starts or stops and whether it runs automatically at startup. MySQL is basically installed.
Run "sudo vi/etc/bashrc" and add the alias of mysql and mysqladmin to the bash configuration file.
# Mysql
Alias mysql = '/usr/local/mysql/bin/mysql'
Alias mysqladmin = '/usr/local/mysql/bin/mysqladmin'
In this way, you can perform operations through commands in the terminal. for example, after installation, the default MySQL root password is blank, to set the password, run "mysqladmin-u root password" mysqlpassword "on the terminal. mysqlpassword is the root password. For more information, see B .5.4.1. How to Reset the Root Password.
Note: The upgrade of Mac OS X or other causes may cause MySQL to start or automatically run upon startup. the "Warning: the/usr/local/mysql/data directory is not owned by the 'mysql' or '_ mysql '", or, the command line prompts "Can't connect to local MySQL server through socket"/tmp/mysql. sock '(2) ", which should be caused by a change in the/usr/local/mysql/data host, run "sudo chown-R mysql/usr/local/mysql/data.
In addition, if you use PHP to connect to MySQL, the error "Can't connect to local MySQL server through socket"/var/mysql may be reported. sock ', or use localhost to connect to MySQL and 127.0.0.1 is required because/var/mysql/MySQL is used by default in php connection. sock, But MYSQL of MAC version changes the file location, put it under/tmp. The solution is to modify php. ini as follows:
Mysql. default_socket =/tmp/mysql. sock
Use phpMyAdmin

PhpMyAdmin is a MySQL management program developed with PHP. it is very popular and practical. Using phpMyAdmin to manage MySQL is a very effective way to verify the results of the previous steps.
Download phpMyAdmin. Select the desired version, for example, phpmyadmin-3.3.2-all-languages.tar.bz2.
Copy the phpMyAdmin-3.32-all-languages folder in downloads to/Users/[user name]/Sites with the name phpmyadmin.
Copy config. sample. inc. php from "/Users/[user name]/Sites/phpmyadmin/" and name it config. inc. php.
Open config. inc. php and make the following changes:
Used for Cookie encryption, random long string
$ Cfg ['blowfish _ secret'] = ";

When "#2002 cannot log on to MySQL server" appears in phpMyAdmin,
Change localhost to 127.0.0.1,
This is because the MySQL daemon is bound to an IP address (bind-address = 127.0.0.1 ).
$ Cfg ['servers'] [$ I] ['host'] = 'localhost ';

Change false to true, so that you can access MySQL without a password,
You can set the password even if the password is set for MySQL, and then enter the password when logging on to phpMyAdmin.
$ Cfg ['servers'] [$ I] ['allownopassword'] = false;
In this way, you can use http: // localhost /~ [User name]/phpmyadmin has accessed phpMyAdmin. At this time, you will see a prompt saying "the mcrypt extension cannot be loaded. Please check your PHP configuration .", This involves installing the MCrypt extension in the next section.
Configure the MCrypt extension of PHP

MCrypt is a powerful Extension Library for encryption algorithms. It includes 22 algorithms. phpMyAdmin depends on this PHP Extension Library. However, it is not so friendly to install on Mac OS X, as follows:
Download and decompress libmcrypt-2.5.8.tar.bz2.
Run the following command on the terminal (note that xcode is required for the following command ):
Cd ~ /Downloads/libmcrypt-2.5.8/
./Configure-disable-posix-threads-enable-static
Make
Sudo make install
Download and decompress the PHP source code file php-5.3.1.tar.bz2. In Mac OS X 10.6.3, the pre-installed PHP version is 5.3.1, and the latest PHP version is 5.3.2. you need to select the corresponding version based on your actual situation.
Run the following command on the terminal:
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 php. ini, save the code, exit, and restart Apache.
Extension =/usr/lib/php/extension/ no-debug-non-zts-20090626/mcrypt. so
When you access http: // localhost/~ again /~ [Username]/phpmyadmin, you will find that "the mcrypt extension cannot be loaded. Please check your PHP configuration ." The prompt "no" indicates that the MCrypt Extension Library has been successfully installed.
Set VM

Run "sudo vi/etc/apache2/httpd. conf" on the terminal to open the configuration file of Apche.
Find "# Include/private/etc/apache2/extra/httpd-vhosts.conf" in httpd. conf, remove the previous "#", save and exit.
Run "sudo apachectl restart" and restart Apache to enable its VM configuration function.
Run sudo vi/etc/apache2/extra/httpd-vhosts.conf to open the file httpd-vhost.conf for configuring the VM and configure the VM you need. Note that two virtual hosts are enabled for this file by default:

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


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

In fact, these two virtual hosts do not exist. when no other virtual hosts are configured, the following prompt may appear when accessing localhost:
Forbidden
You don't have permission to access/index. php on this server
The simplest way is to add # in front of each line and comment it out, so that it can be referenced without causing other problems.
Add the following configuration

DocumentRoot "/Users/[user name]/Sites"
ServerName sites
ErrorLog "/private/var/log/apache2/sites-error_log"
CustomLog "/private/var/log/apache2/sites-access_log" common

Save and exit, and restart Apache.
Run "sudo vi/etc/hosts", open the hosts configuration file, and add "127.0.0.1 sites". In this way, you can configure the sites virtual host and use "http: // sites "accessed, its content and" http: // localhost /~ [User name] "is completely consistent.
This is a configuration that is supported by Mac OS x 10.6.3. For more information, see "Mac OS X Leopard: Configure Apache, PHP, SQLite, MySQL, and phpMyAdmin (1) "and" Mac OS X Leopard: Configure Apache, PHP, SQLite, MySQL, and phpMyAdmin (2 )". In fact, you can also use an integrated solution provided by a third party such as XAMPP or MacPorts for 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.