Home > Article > Backend Development > PHP development under Mac system
Mac OS X has 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 PHP's MCrypt extension library
Set up a virtual host
Start Apache
There are two methods:
Open "System Preferences (System Preferences)" -> "Sharing" -> "Web Sharing"
Open "terminal", and then (note: the password required for sudo is the root account password of the system)
Run "sudo apachectl start", and then enter Account password, so Apache will run.
Run "sudo apachectl -v", you will see the Apache version number in Mac OS In this way, enter "http://localhost" in the browser, you can see a page with the content "It works!", which is located under "/Library (resource library)/WebServer/Documents/", this is Apache the default root directory.
Note: Turning on Apache means turning on "Web Sharing". At this time, Internet users will access the "/Library (resource library)/WebServer/Documents/" directory through "http://[local IP]/". To access the "/Users/[username]/Sites/" directory through "http://[local IP]/~[username]", you can set the "Security" in "System Preferences" "Firewall" to prevent such access.
Run PHP
Run "sudo vi /etc/apache2/httpd.conf" in the terminal to open the Apache configuration file. (If you are not used to operating the terminal and vi, you can set it to display all system hidden files in Finder. Remember to restart Finder so that you can find the corresponding files and edit them as you like. However, it should be noted that the modification of some files requires root to be turned on. account, but overall it is safer to use sudo on the terminal to temporarily obtain root permissions)
Find "#LoadModule php5_module libexec/apache2/libphp5.so", remove the # sign in front, and save (enter: w on the command line. ) and exit vi (enter: q at the command line).
Run "sudo cp /etc/php.ini.default /etc/php.ini" so that you can configure various PHP functions through php.ini. For example:
;Adjust the maximum value of PHP submission files through the following two items, such as the maximum value of imported data in phpMyAdmin
upload_max_filesize = 2M
post_max_size = 8M
;For example, use display_errors to control whether to display errors of the PHP program
display_errors = Off
Run "sudo apachectl restart" to 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
In Run "vi /Library/WebServer/Document/info.php" in the terminal, so that you can edit the info.php file in vi. Add “
” after “It’s works!” 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
Since MySQL is not pre-installed in Mac OS X, you need to install it manually. Currently, the most stable version of MySQL is 5.1. MySQL provides installation instructions for Mac OS X.
Download MySQL 5.1. Select the appropriate version, for example, mysql-5.1.46-osx10.6-x86_64.dmg is selected here.
Run dmg and you will find 4 files in it. First click to install mysql-5.1.46-osx10.6-x86_64.pkg, which is the main installation package of MySQL. Under normal circumstances, the installation file will automatically install MySQL into the folder of the same name under /usr/local. For example, clicking 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 is complete all the way.
Click to install the second file MySQLStartupItem.pkg, so that MySQL will automatically start at boot.
Click to install the third file MySQL.prefPane, and you will see the ICON named "MySQL" in the "System Settings Preferences". Through it, you can set whether MySQL starts or stops, and whether it runs automatically when the computer is turned on. At this point MySQL is basically installed.
By running "sudo vi /etc/bashrc", add the aliases 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 corresponding operations relatively simply through commands in the terminal. For example, after the installation is completed, the default password for MySQL root is empty. If you want to set a password, you can run "mysqladmin -u in the terminal. root password "mysqlpassword"", where mysqlpassword is the root password. For more related information, please refer to B.5.4.1. How to Reset the Root Password.
Note: The upgrade of Mac OS '_mysql'", or the command line prompts "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)". This should be caused by /usr/local/mysql/ under certain circumstances. The host of data has changed, just run "sudo chown -R mysql /usr/local/mysql/data".
In addition, when using PHP to connect to MySQL, the error "Can't connect to local MySQL server through socket '/var/mysql/mysql.sock'" may be reported, or when using localhost, the MySQL cannot be connected and 127.0.0.1 is required. The reason is that PHP is used when connecting. By default, it looks for /var/mysql/mysql.sock, but the MAC version of MYSQL changes the file location and places it under /tmp. The solution is to modify php.ini as follows:
mysql.default_socket = /tmp/mysql.sock
Use phpMyAdmin
phpMyAdmin is a program developed with PHP to manage MySQL. It is very popular and practical. Being able to use phpMyAdmin to manage MySQL is a very effective way to test the results of the previous steps.
Download phpMyAdmin. Choose the appropriate version. For example, I chose the phpMyAdmin-3.3.2-all-languages.tar.bz2 version.
Copy the phpMyAdmin-3.32-all-languages folder in "Downloads" to "/Users/[username]/Sites" and change the name to phpmyadmin.
Copy config.sample.inc.php in "/Users/[username]/Sites/phpmyadmin/" and name it config.inc.php
Open config.inc.php and make the following modifications:
For cookies Encrypted, arbitrary long string
$cfg['blowfish_secret'] = '';
When "#2002 Unable to log in to the MySQL server" appears in phpMyAdmin,
Please change localhost to 127.0.0.1 and it will be ok,
This It is caused by the MySQL daemon doing IP binding (bind-address =127.0.0.1)
$cfg['Servers'][$i]['host'] = 'localhost';
Change false to true , so that you can access passwordless MySQL,
Even if MySQL has set a password, you can set it like this, and then enter the password when logging in to phpMyAdmin
$cfg['Servers'][$i]['AllowNoPassword'] = false;
In this way, you can access phpMyAdmin through http://localhost/~[username]/phpmyadmin. At this time, you will see a prompt "Unable to load the mcrypt extension, please check your PHP configuration." This involves installing the MCrypt extension in the next section.
Configuring MCrypt extension for PHP
MCrypt is a powerful encryption algorithm extension library, which includes 22 algorithms. phpMyAdmin relies on this PHP extension library. However, its installation under Mac OS X is not so friendly, as follows:
Download and unzip libmcrypt-2.5.8.tar.bz2.
Execute the following command in the terminal (note that the following command requires xcode support to be installed):
cd ~/Downloads/libmcrypt-2.5.8/
./configure --disable-posix-threads --enable-static
make
sudo make install
Download and unzip the PHP source code file php-5.3.1.tar.bz2. The pre-installed PHP version in Mac OS
Execute the following command in 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 and exit, and then restart Apache
extension=/usr/lib/php/ extensions/no-debug-non-zts-20090626/mcrypt.so
When you visit http://localhost/~[username]/phpmyadmin, you will find "Unable to load the mcrypt extension, please check your PHP configuration ." prompt is gone, which means the MCrypt extension library is installed successfully.
Set up a virtual host
Run "sudo vi /etc/apache2/httpd.conf" in the terminal and open Apche's configuration file
Find "#Include /private/etc/apache2/extra/httpd-vhosts in httpd.conf. conf", remove the leading "#", save and exit.
Run “sudo apachectl restart” and restart Apache to enable its virtual host configuration function.
Run “sudo vi /etc/apache2/extra/httpd-vhosts.conf” to open the virtual host configuration file httpd-vhost.conf and configure the virtual host you need. It should be noted that this file opens two virtual hosts as examples 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 host is configured, the following prompt may appear when accessing localhost:
Forbidden
You don't have permission to access /index.php on this server
The easiest way is to add them in front of each line Just put # and comment it out, so that it can be referenced without causing other problems.
Add the following configuration
DocumentRoot "/Users/[username]/Sites"
ServerName sites
ErrorLog "/private/var/log/apache2/sites-error_log"
CustomLog "/private/var/log/apache2/ sites-access_log" common
Save, exit, and restart Apache.
Run "sudo vi /etc/hosts", open the hosts configuration file, and add "127.0.0.1 sites", so that the sites virtual host can be configured, and its content can be accessed using "http://sites" Exactly the same as "http://localhost/~[username]".
This is a configuration implemented using native support in Mac OS X 10.6.3. You can also refer to "Mac OS : Configuring Apache, PHP, SQLite, MySQL, and phpMyAdmin(2)”. In fact, you can also use third-party integration solutions such as XAMPP or MacPorts to achieve simple installation and use.