Mac下LAMP环境的搭建

Posted by Harid十月 - 17 - 2015 Leave comments

OS X自带有Apache和PHP的,我们需要安装的就是MySQL和PHPMyAdmin。

1. Apache

在Mac的终端里输入 apachectl -v,会显示:

[Harid@Halo:~ ]$ apachectl -v
Server version: Apache/2.4.16 (Unix)
Server built: Aug 22 2015 16:51:57

开启Apache服务,执行:sudo apachectl start

开启了Apache服务,在浏览器里面输入『http://localhost』,就会显示『It works!』。

对应的关闭命令: sudo apachectl stop

对应的重启命令: sudo apachectl restart

缺省Root Document的路径是: /Library/WebServer/Documents; 若不以虚拟机的方式配置,则可以将程序放在该目录下;若需要更改到另一个目录,推荐以虚拟机方式进行配置。进入/etc/apache2/目录,编辑httpd.conf,找到:#Include /private/etc/apache2/extra/httpd-vhosts.conf,去掉前面的注释,然后保存httpd.conf文件。

运行“sudo apachectl restart”,重启Apache后就开启了虚拟主机配置功能。

编辑/etc/apache2/extra/httpd-vhosts.conf文件,配置虚拟主机。该文件中当前已经存在两个预置的虚拟主机,将这两个虚拟主机的配置全部注释,否则会出现:

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

往该文件最后增加如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<VirtualHost *:80>
    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 *:80>
    DocumentRoot "/Users/Harid/Sites"
    ServerName t.com
    ErrorLog "/private/var/log/apache2/sites-error_log"
    CustomLog "/private/var/log/apache2/sites-access_log" common
    <Directory "/Users/Harid/Sites">
                Options FollowSymLinks MultiViews
                AllowOverride None
                Require all granted
    </Directory>
</VirtualHost>

保存,并重启Apache。创建/Users/Harid/Sites目录,并放入index.html文件。编辑/etc/hosts文件,做127.0.0.1到t.com的本地解析。这个时候就可以在浏览器本地访问http://t.com了。

untitled

2.    PHP

OS X下面自带有PHP,可以就用这个PHP。需要编辑/etc/apache2/httpd.conf文件,加载php模块。

找到:

#LoadModule php5_module libexec/apache2/libphp5.so

将其前面的注释去掉,重启Apache即可。

3. MySQL

mac不自带mysql,这里需要重新安装,方法依然很简单:

1
brew install mysql

注意在执行brew install mysql的时候,出现了如下的错误:

==> cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/Cellar/mysql/5.6.26 -DCMAKE_FIND_FRAMEWORK=LAST
==> make
==> make install
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink include/mysql
/usr/local/include is not writable.

You can try again using:
brew link mysql
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall mysql`
==> Caveats
A “/etc/my.cnf” from another install may interfere with a Homebrew-built
server starting up correctly.

To connect:
mysql -uroot

To have launchd start mysql at login:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don’t want/need launchctl, you can just run:
mysql.server start
==> Summary
???? /usr/local/Cellar/mysql/5.6.26: 9853 files, 338M, built in 33.3 minutes

需要在/usr/local目录下创建如下3个目录:

var

include

lib

并且执行sudo chown Harid:admin var include lib来更改目录属主。

然后按照出错提示依次执行:

1
2
brew link mysql
brew postinstall mysql

然后执行:

1
2
3
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
sudo chown -R your_user /usr/local/var/mysql/

然后是启动命令

1
mysql.server start

最好给mysql设个密码,方法如下:

1
mysqladmin -u root password 'xxx'

4. PHPMyAdmin

PHPMyAdmin几乎是管理mysql最容易的web应用了。去官网下载最新的版本,解压到~/Sites/phpmyadmin下,在phpmyadmin目录下创建一个可写的config目录;

打开http://localhost/phpmyadmin/setup,安装一个服务,最后保存(这里只需要输入帐号密码就够了);

将config下生成的config.inc.php移到phpmyadmin根目录下;

最后删除config;

这样就装好了,虽然可能有点小复杂,但是来一次就习惯了。这里很可能会遇到2002错误,就是找不到mysql.sock的问题,用下面方法解决:

1
2
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

打开PHPMyAdmin的界面,如果无法用root用户登录,请将config.inc.php文件中的$cfg[‘Servers’][$i][‘host’] 的值改为127.0.0.1,不用localhost。

   声明:本文采用 BY-NC-SA 协议进行授权 | 星期九
   原创文章转载请注明:转自《Mac下LAMP环境的搭建


分享按钮