Home > php教程 > php手册 > Configuring Apache + PHP + MySQL in Mac OS X

Configuring Apache + PHP + MySQL in Mac OS X

WBOY
Release: 2016-07-09 09:09:42
Original
889 people have browsed it

Respect the author: Reprinted from: http://dancewithnet.com/2010/05/09/run-apache-php-mysql-in-mac-os-x/

Configuring Apache + PHP + MySQL in Mac OS X

Mac OS X has built-in Apache and PHP, which is very convenient to use. This article takes Mac OS X 10.6.3 and 10.8.1 as examples. The main contents include:

  1. Start Apache
  2. Run PHP
  3. Install MySQL
  4. Use phpMyAdmin
  5. Configuring PHP’s MCrypt extension library
  6. Set up virtual host

Start Apache

There are two methods:

  1. Open "System Preferences" -> "Sharing" -> "Web Sharing". Note that “Web Sharing” has been removed from Mac OS X starting from 10.8.
  2. Open "terminal", and then (note that the password required for sudo is the root account password of the system)
    1. Run "sudo apachectl start" and enter your account and password, so Apache will run.
    2. Run "sudo apachectl -v", you will see the Apache version information of Mac OS X, such as in 10.8.1:
      <code>Server version: Apache/2.2.22 (Unix)
      Server built:   Jun 20 2012 13:57:09
      </code>
      Copy after login

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 This is the default root directory of Apache.

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]/" , access the "/Users/[user name]/Sites/" directory through "http://[local IP]/~[user name]". It is worth noting that when Mac OS X canceled "Web Sharing" in 10.8, it also removed the "/Users/[username]/Sites/" directory, so accessing "http: //[Local IP]/~[Username]" will display "403 Forbidden", but http://[Local IP]/ can still be accessed. You can go to "System Preferences" -> "Security" -> "Firewall", turn on the firewall, and then check "Block all incoming connections (block)" in "Firewall Options" all incoming connections)". You can also set httpd.conf to only allow localhost and 127.0.0.1 to access "/Library (resource library)/WebServer/Documents/".

<code><directory>
    <span>......
    #
    # Controls who can get stuff from this server.
    #</span>
    Order allow,deny
    <span>#Allow from all</span>
    Allow from 127.0.0.1
    Allow from localhost 

</directory>
</code>
Copy after login

Run PHP

  1. 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 after setting up, and then you can find the corresponding files and edit them as you like. Please note that some files still need to be modified. Open the root account, but in general it is safer to use sudo on the terminal to temporarily obtain root permissions)
  2. .
  3. Find "#LoadModule php5_module libexec/apache2/libphp5.so", remove the # sign in front, save (enter :w on the command line) and exit vi (enter :q on the command line).
  4. Run "sudo cp /etc/php.ini.default /etc/php.ini", so you can run <code>sudo vi /etc/php.ini</code> to edit php.ini to configure various functions. for example:
    <code><span>;通过下面两项来调整PHP提交文件的最大值,如phpMyAdmin中导入数据的最大值</span>
    upload_max_filesize = 2M
    post_max_size = 8M
    <span>;通过display_errors来控制是否显示PHP程序的报错信息,这在调试PHP程序时非常有用</span>
    display_errors = Off
    </code>
    Copy after login
  5. Run "sudo apachectl restart" and restart Apache so that PHP can be used.
  6. Run "sudo 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.
  7. Run "sudo vi /Library/WebServer/Document/info.php" in the terminal so that you can edit the info.php file in vi. Add “<?php phpinfo(); ?>” after “It’s works!” and save it. In this way, you can see information about PHP in http://localhost/info.php. For example, the built-in PHP version number in 10.8 is 5.3.13.

Install MySQL

Mac OS X does not have built-in MySQL, so you need to install it manually. Currently, the most stable version of MySQL is 5.5. MySQL provides installation instructions for Mac OS X.

  1. 下载MySQL 5.5。选择合适版本,如这里选择了mysql-5.5.27-osx10.6-x86_64.dmg。
  2. 运行dmg,会发现里面有4个文件。首先点击安装mysql-5.5.27-osx10.6-x86_64.pkg,这是MySQL主安装包。一般情况下,安装文件会自动把MySQL安装到/usr/local下的同名文件夹下。如运行“mysql-5.5.27-osx10.6-x86_64.dmg”会把MySQL安装到“/usr/local/mysql-5.5.27-osx10.6-x86_64”中,一路默认安装完毕。(注意,从10.8开始Mac OS X的权限更加严格,直接点击会提示“mysql-5.5.27-osx10.6-x86_64.pkg can’t be opened because it is from an unidentified developer. Your security preferences allow installation of only apps from the Mac App Store and identified developers.”阻止了安装,你可以使用双指单击该安装文件,在弹出菜单中选择“用…打开(open with)”,再选择“安装(Installer)”就可以接着安装了。)
  3. 安装第2个文件MySQLStartupItem.pkg,MySQL就会自动在开机时启动了。(注意,10.8的安装方法同上。)
  4. 安装第3个文件MySQL.prefPane,就会在“系统设置偏好”中看到“MySQL”的ICON,通过它就可以控制MySQL是否开启,以及开机时是否自动运行。到这里MySQL就基本安装完毕了。(注意,10.8中用双指单击该安装文件,在弹出的菜单中选择“用…打开(open with)”,然后选择“系统偏好(System Perference)”就可以接着安装了。)
  5. 通过运行“sudo vi /etc/bashrc”,在bash配置文件中加入mysqlstartmysqlmysqladmin的别名(注意:修改完毕之后需要退出“终端(Terminal)”之后重新进入,这些命令才会生效):
    <code>#mysql
    alias mysqlstart='sudo /Library/StartupItems/MySQLCOM/MySQLCOM restart'
    alias mysql='/usr/local/mysql/bin/mysql'
    alias mysqladmin='/usr/local/mysql/bin/mysqladmin'
    </code>
    Copy after login

    这样就可以在终端中比较简单地通过命令进行相应的操作。由于开始安装MySQLStartupItem.pkg到“/Library/StartupItems/MySQLCOM/”来控制MySQL的运行、自动运行、停止、关闭之类。在MySQL没有启动时,直接运行mysqlmysqladmin命令会提示“Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)”,所以我们可以通过控制面板或者直接运行mysqlstart命令来启动MySQL,之后再运行mysqlmysqladmin命令就正常了。比如安装完毕后MySQL的root默认密码为空,如果要设置密码可以在终端运行“mysqladmin -u root password "mysqlpassword"”来设置,其中mysqlpassword即root的密码。更多相关内容可以参考B.5.4.1. How to Reset the Root Password。

注意:Mac OS X的升级或其他原因可能会导致MySQL启动或开机自动运行时,在MySQL操作面板上会提示“Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql' ”,这应该是某种情况下导致/usr/local/mysql/data的宿主发生了改变,只需要运行“sudo chown -R mysql /usr/local/mysql/data”即可。

另外,使用PHP连接MySQL可能会报错“Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’”,或使用localhost无法连接MySQL而需要127.0.0.1,原因是连接时php默认去找/var/mysql/mysql.sock了,但MAC版的MYSQL改动了文件位置,放在/tmp下了。处理办法是按如下修改php.ini:

<code>mysql.default_socket = /tmp/mysql.sock</code>
Copy after login

使用phpMyAdmin

phpMyAdmin是用PHP开发的管理MySQL的程序,非常的流行和实用。能够使用phpMyAdmin管理MySQL是检验前面几步效果的非常有效方式。

  1. 下载phpMyAdmin。选择合适的版本,比如这里选择phpMyAdmin-3.5.22-all-languages.tar.bz2这个版本。
  2. 把“下载(downloads)”中phpMyAdmin-3.5.22-all-languages.tar.bz2文件解压到“/Library/WebServer/Documents/”中,并改名为phpmyadmin。
    <code>sudo tar -xf ~/Downloads/phpMyAdmin-3.5.2.2-all-languages.tar.bz2 -C
                 /Library/WebServer/Documents/
    sudo mv /Library/WebServer/Documents/phpMyAdmin-3.5.2.2-all-languages
                /Library/WebServer/Documents/phpmyadmin
    </code>
    
    Copy after login
  3. 复制“/Library/WebServer/Documents/phpmyadmin/”中的config.sample.inc.php,并命名为config.inc.php
  4. 编辑config.inc.php,修改如下:
    <code><span>用于Cookie加密,随意的长字符串</span>
    $cfg['blowfish_secret'] = 'a8b7c6d';
    
    <span>当phpMyAdmin中出现“#2002 无法登录 MySQL 服务器(#2002 Cannot log in to the MySQL server)”时,
    请把localhost改成127.0.0.1就ok了,
    这是因为MySQL守护程序做了IP绑定(bind-address =127.0.0.1)造成的</span>
    $cfg['Servers'][$i]['host'] = 'localhost';
    
    <span>把false改成true,这样就可以访问无密码的MySQL了,
    即使MySQL设置了密码也可以这样设置,然后登录phpMyAdmin时输入密码</span>
    $cfg['Servers'][$i]['AllowNoPassword'] = false;
    </code>
    Copy after login
  5. 这样就可以通过http://localhost/phpmyadmin访问phpMyAdmin了。此时会看到一个提示“无法加载 mcrypt 扩展,请检查您的 PHP 配置。(The mcrypt extension is missing. Please check your PHP configuration.)”,这会涉及到下一节安装MCrypt扩展了。

配置PHP的MCrypt扩展

MCrypt是一个功能强大的加密算法扩展库,它包括有22种算法,phpMyAdmin依赖这个PHP扩展库。但在Mac OS X下的安装却不那么友善,具体如下:

  1. 下载libmcrypt-2.5.8.tar.gz。
  2. 在终端执行如下命令(注意如下命令需要安装Xcode支持,可直接去Mac App Store下载,安装完毕后可能会发现在终端运行./configure --disable-posix-threads --enable-static会报错,运行make会提示命令不存在,此时还需要打开Xcode,然后在Xcode的软件“配置(Preference…)”)-> “下载(Downloads)” 中安装 “命令行工具(Command Line Tools)”:
    <code>cd ~/Downloads
    tar -zxvf libmcrypt-2.5.8.tar.bz2
    cd libmcrypt-2.5.8
    ./configure --disable-posix-threads --enable-static
    make
    sudo make install</code>
    Copy after login
  3. 下载PHP源码文件php-5.3.13.tar.bz2,记得选择中国镜像会比较快。Mac OS X 10.6.3中预装的PHP版本是5.3.1,10.8的版本是5.3.13,而现在最新的PHP版本是5.4.6,所以需要依据自己的实际情况选择对应的版本,本文以10.8的PHP版本为例。
  4. 在终端执行如下命令,把php-5.3.13.tar.bz2,并配置autoconf(在新的Mac OS X的Xcode中需要自己配置),然后才能运行phpize命令:
    <code>cd ~/Downloads
    tar -zxvf php-5.3.13.tar.bz2
    
    cd php-5.3.13/ext/mcrypt
    curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
    tar -zxvf autoconf-latest.tar.gz
    cd autoconf-2.69
    ./configure
    make
    sudo make install
    
    cd ..
    phpize
    ./configure
    make
    sudo make install
    </code>
    Copy after login
  5. 打开php.ini
    <code>sudo vi /etc/php.ini</code>
    Copy after login

    在php.ini中加入如下代码,并保存后退出,然后重启Apache

    <code>extension=mcrypt.so</code>
    Copy after login

当你再访问http://localhost/phpmyadmin时,会发现“无法加载 mcrypt 扩展,请检查您的 PHP 配置。”提示没有了,这就表示MCrypt扩展库安装成功了。如果还不能加载,尝试把php.ini中的加入的extension修改为:

<code>extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/mcrypt.so</code>
Copy after login

Mac OS X下安装MCrypt扩展的确比较复杂,而且稍微不小心会有各种小问题出现,大家还可以参考How to Install mcrypt for php on Mac OSX Lion 10.8 & 10.7 Development Server和Adding MCRYPT to your OSX Lion PHP install

设置虚拟主机

  1. 在终端运行“sudo vi /etc/apache2/httpd.conf”,打开Apche的配置文件
  2. 在httpd.conf中找到“#Include /private/etc/apache2/extra/httpd-vhosts.conf”,去掉前面的“”,保存并退出。
  3. 运行“sudo apachectl restart”,重启Apache后就开启了虚拟主机配置功能。
  4. 运行“sudo vi /etc/apache2/extra/httpd-vhosts.conf”,就打开了配置虚拟主机文件httpd-vhost.conf,配置虚拟主机了。需要注意的是该文件默认开启了两个作为例子的虚拟主机:
    <code><virtualhost>
        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>
        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> </code>
    Copy after login

    而实际上,这两个虚拟主机是不存在的,在没有配置任何其他虚拟主机时,可能会导致访问localhost时出现如下提示:

    <code>Forbidden
    You don't have permission to access /index.php on this server</code>
    Copy after login

    最简单的办法就是在它们每行前面加上#,注释掉就好了,这样既能参考又不导致其他问题。

  5. 增加如下配置
    <code><virtualhost>
        DocumentRoot "/Library/WebServer/Documents"
        ServerName localhost
        ErrorLog "/private/var/log/apache2/localhost-error_log"
        CustomLog "/private/var/log/apache2/localhost-access_log" common
    </virtualhost> 
    
    <virtualhost>
        DocumentRoot "/Users/[用户名]/Sites"
        ServerName sites
        ErrorLog "/private/var/log/apache2/sites-error_log"
        CustomLog "/private/var/log/apache2/sites-access_log" common
        <directory></directory>
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride None
                    Order deny,allow
                    Allow from all
          
    </virtualhost> </code>
    Copy after login

    保存退出,并重启Apache。

  6. 运行“sudo vi /etc/hosts”,打开hosts配置文件,加入"127.0.0.1 sites",这样就可以配置完成sites虚拟主机了,可以访问“http://sites”了,在10.8之前Mac OS X版本其内容和“http://localhost/~[用户名]”完全一致。
  7. 注意,记录log的“ErrorLog "/private/var/log/apache2/sites-error_log"”也可以删掉,但记录日志其实是一个好习惯,在出现问题时可以帮助我们判断。如果保留这些log代码,一定log文件路径都是存在的,如果随便修改一个不存在的,会导致Apache无法服务而没有错误提示,这个比较恶心。

这里利用Mac OS X 10.6.3和10.8.1中原生支持的方式来实现的配置,也可以参考“Mac OS X Leopard: 配置Apache, PHP, SQLite, MySQL, and phpMyAdmin(一) ”和“Mac OS X Leopard: 配置Apache, PHP, SQLite, MySQL, and phpMyAdmin(二) ”。实际上,还可以使用XAMPP或MacPorts这种第三方提供的集成方案来实现简单的安装和使用。

您或许有兴趣:

  • MySQL&phpMyAdmin安装于Win下
  • 启用Mod Rewrite和.htaccess
  • 从MySQL4.0向MySQL5迁移数据
  • 利用phpMyAdmin把wordpress数据从MySQL4迁移到MySQL5
  • 安装Apache+PHP在Windows+IIS下
  • 升级到Wordpress2.3小结
  • Ajax的小贴士
  • SSI
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template