Keywords: Mac OS | PHP activation | MySQL installation | Apache configuration | local development environment
Abstract: This article provides a comprehensive guide to activating PHP and MySQL on Mac OS 10.6 (Snow Leopard), 10.7 (Lion), and 10.8 (Mountain Lion). By leveraging built-in Apache and PHP modules alongside the official MySQL installer, it offers a solution without third-party integrated environments like MAMP. Covering configuration file modifications, MySQL installation, service startup, and addressing common issues such as MySQL socket path configuration, it is designed for developers comfortable with command-line operations.
System Environment and Prerequisites
Mac OS X versions 10.6 through 10.8 include built-in Apache web server and PHP modules, forming the foundation for local development environments. This guide assumes familiarity with Terminal command-line operations and a preference for using native system components over third-party tools. This approach offers configuration closer to production environments while avoiding additional software dependencies.
Enabling the PHP Module
First, activate the PHP5 module in Apache. Open the Apache configuration file using a text editor (e.g., TextMate, TextWrangler, vi, or nano):
/etc/apache2/httpd.confLocate the following line in the configuration file:
#LoadModule php5_module libexec/apache2/libphp5.soRemove the comment symbol # at the beginning to enable the module. The modified line should read:
LoadModule php5_module libexec/apache2/libphp5.soThis step allows Apache to parse PHP script files.
Installing and Configuring MySQL
While PHP modules are built-in, MySQL requires separate installation. Download the latest version for Mac OS X from the official MySQL website. Select based on processor architecture:
- Most Intel Macs use the x86_64 (64-bit) version
- Early Intel MacBook/MacBook Pro (non-64-bit chips) require the x86 (32-bit) version
During installation, select all components. After installation, start the MySQL service via the MySQL panel in System Preferences. Ensure the service status shows as running.
Enabling Web Services
In the Sharing panel of System Preferences, enable Web Sharing. If Web Sharing is already enabled, it is recommended to disable and re-enable it to ensure configuration changes take effect. At this point, the Apache server will start automatically, working in conjunction with the PHP module and MySQL service.
Verification and Testing
Create a test file <code>info.php</code> in the web root directory (typically <code>/Library/WebServer/Documents/</code>) with the following content:
<?php
phpinfo();
?>Access <code>http://localhost/info.php</code> via a web browser. This should display the PHP configuration information page, including the status of the MySQL module.
Common Issues and Additional Notes
In earlier Mac OS versions (10.4-10.5), manual configuration of the <code>php.ini</code> file was required to specify the MySQL socket path. Although version 10.6 claimed to fix this issue, some users may still encounter connection errors. If MySQL connection problems arise, check the following configurations in <code>/etc/php.ini</code> or <code>/etc/php.ini.default</code>:
mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sockEnsure the path matches the actual MySQL socket file location. Use the MySQL command-line tool <code>mysql_config --socket</code> to query the current socket path.
Advanced Configuration Recommendations
For users requiring custom PHP extensions or adjustments to Apache virtual hosts, further edits to relevant configuration files are possible:
- PHP configuration: <code>/etc/php.ini</code> (may need to be created from the default file)
- Apache virtual hosts: <code>/etc/apache2/extra/httpd-vhosts.conf</code>
- Host mapping: <code>/etc/hosts</code>
After modifications, restart the Apache service: <code>sudo apachectl restart</code>.
Conclusion
By combining built-in system components with the official MySQL installer, a PHP+MySQL development environment can be quickly established on Mac OS 10.6-10.8. This method maintains system simplicity while providing full LAMP stack functionality. Key steps include enabling the PHP module, installing MySQL, and configuring service interoperability. Advanced users can further customize configurations to meet specific development needs.