Mac OS X上搭建Apache、PHP、MySQL的Web服务器
在Mac OS X上搭建Apache、PHP、MySQL的Web服务器开发环境。
方法/步骤
-
1. 如何开启Apache服务器?
开启Web服务器的方法有两种:
一种是:打开“系统设置偏好(System Preferences)” -> “共享(Sharing)” -> “Web共享(Web Sharing)”;
另一种:通过在terminal终端直接运行Apache的启动命令来打开:sudo apachectl start。
我在自己的Mac OS X 10.10.1 上并没有找到Web Sharing,因此,只能使用第二种方式打开服务器。
Apache服务器默认的web根目录在:/Library/WebServer/Documents。
Apache的配置文件在:/etc/apache2。
-
2. 开启PHP模块
因Mac OS X已经内置PHP,因此我们只需要在Apache的配置中加载PHP模块即可。方法如:
1)在终端运行:sudo vi /etc/apache2/httpd.conf,打开Apache配置文件。
2)找到#LoadModule php5_module libexec/apache2/libphp5.so类似条目,将注释符#去掉,并保存。
3)终端运行:sudo apachectl restart,重启Apache服务器。
-
3. 安装和启动MySQL
1)使用Homebrew安装MySQL:
在Mac OS X上安装软件,你可以直接找到相关img安装,也可以像Ubuntu的apt-get类似方便的,可以使用brew install进行。
当然,使用此功能,你需要安装Homebrew,安装方法是:
在终端运行命令:ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"。
安装Mysql,在终端运行:brew install mysql。
brew程序将自动安装mysql的依赖库openssl,然后安装mysql,我的安装的是:mysql-5.6.22。
2)启动MySQL:
根据上面安装结束的提示,启动MySQL,在终端运行:mysql.server start。启动成功后使用:mysql -uroot即可连接到MySQL数据库。
---------下面贴出在Mac终端执行命令的一些信息:
SunMmdeiMac:~ qianjing$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
Password:
==> Downloading and installing Homebrew...
remote: Counting objects: 225571, done.
remote: Compressing objects: 100% (59354/59354), done.
remote: Total 225571 (delta 165037), reused 225482 (delta 164969)
Receiving objects: 100% (225571/225571), 51.57 MiB | 1.19 MiB/s, done.
Resolving deltas: 100% (165037/165037), done.
From https://github.com/Homebrew/homebrew
* [new branch] master -> origin/master
HEAD is now at 82d7dc4 tomcat: update 8.0.17 bottle.
==> Installation successful!
==> Next steps
Run `brew doctor` before you install anything
Run `brew help` to get started
SunMmdeiMac:~ qianjing$
SunMmdeiMac:~ qianjing$
SunMmdeiMac:~ qianjing$
SunMmdeiMac:~ qianjing$ brew install mysql
==> Installing mysql dependency: openssl
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/openssl-1.0.1l.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.1l.yosemite.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
/usr/local/etc/openssl/certs
and run
/usr/local/opt/openssl/bin/c_rehash
This formula is keg-only, which means it was not symlinked into /usr/local.
Mac OS X already provides this software and installing another version in
parallel can cause all kinds of trouble.
Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries
Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:
LDFLAGS: -L/usr/local/opt/openssl/lib
CPPFLAGS: -I/usr/local/opt/openssl/include
==> Summary
🍺 /usr/local/Cellar/openssl/1.0.1l: 431 files, 15M
==> Installing mysql
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/mysql-5.6.22.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mysql-5.6.22.yosemite.bottle.tar.gz
==> 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
==> /usr/local/Cellar/mysql/5.6.22/bin/mysql_install_db --verbose --user=qianjing --basedir=/usr/local/Cellar/mysql/5.6.22 --d
==> Summary
🍺 /usr/local/Cellar/mysql/5.6.22: 9666 files, 339M
SunMmdeiMac:~ qianjing$
SunMmdeiMac:~ qianjing$ mysql.server start
Starting MySQL
. SUCCESS!
SunMmdeiMac:~ qianjing$ mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.22 Homebrew
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>